Method: MethodOrProcHelper#call_method_or_proc_on
- Defined in:
- lib/active_admin/view_helpers/method_or_proc_helper.rb
#call_method_or_proc_on(receiver, *args) ⇒ Object
Many times throughout the views we want to either call a method on an object or instance_exec a proc passing in the object as the first parameter. This method wraps that pattern.
Calling with a String or Symbol:
call_method_or_proc_on(@my_obj, :size) same as @my_obj.size
Calling with a Proc:
proc = Proc.new{|s| s.size }
call_method_or_proc_on(@my_obj, proc)
By default, the Proc will be instance_exec’d within self. If you would rather not instance exec, but just call the Proc, then pass along ‘exec: false` in the options hash.
proc = Proc.new{|s| s.size }
call_method_or_proc_on(@my_obj, proc, exec: false)
You can pass along any necessary arguments to the method / Proc as arguments. For example:
call_method_or_proc_on(@my_obj, :find, 1) #=> @my_obj.find(1)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_admin/view_helpers/method_or_proc_helper.rb', line 49 def call_method_or_proc_on(receiver, *args) = { exec: true }.merge(args.) symbol_or_proc = args.shift case symbol_or_proc when Symbol, String receiver.send(symbol_or_proc.to_sym, *args) when Proc if [:exec] instance_exec(receiver, *args, &symbol_or_proc) else symbol_or_proc.call(receiver, *args) end end end |