Module: MethodOrProcHelper

Included in:
ActiveAdmin::BaseController::Authorization, ActiveAdmin::MenuItem, ActiveAdmin::Resource, ActiveAdmin::ViewHelpers
Defined in:
lib/active_admin/view_helpers/method_or_proc_helper.rb

Instance Method Summary collapse

Instance Method Details

#call_method_or_exec_proc(symbol_or_proc, *args) ⇒ Object

This method will either call the symbol on self or instance_exec the Proc within self. Any args will be passed along to the method dispatch.

Calling with a Symbol:

call_method_or_exec_proc(:to_s) #=> will call #to_s

Calling with a Proc

my_proc = Proc.new{ to_s }
call_method_or_exec_proc(my_proc) #=> will instance_exec in self


15
16
17
18
19
20
21
22
# File 'lib/active_admin/view_helpers/method_or_proc_helper.rb', line 15

def call_method_or_exec_proc(symbol_or_proc, *args)
  case symbol_or_proc
  when Symbol, String
    send(symbol_or_proc, *args)
  when Proc
    instance_exec(*args, &symbol_or_proc)
  end
end

#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 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)
  options = { :exec => true }.merge(args.extract_options!)

  symbol_or_proc = args.shift

  case symbol_or_proc
  when Symbol, String
    receiver.send(symbol_or_proc.to_sym, *args)
  when Proc
    if options[:exec]
      instance_exec(receiver, *args, &symbol_or_proc)
    else
      symbol_or_proc.call(receiver, *args)
    end
  end
end

#render_in_context(context, obj, *args) ⇒ Object

This method is different from the others in that it calls ‘instance_exec` on the reciever, passing it the proc. This evaluates the proc in the context of the reciever, thus changing what `self` means inside the proc.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_admin/view_helpers/method_or_proc_helper.rb', line 82

def render_in_context(context, obj, *args)
  context ||= self # default to `self`
  case obj
  when Proc
    context.instance_exec *args, &obj
  when Symbol
    context.send obj, *args
  else
    obj
  end
end

#render_or_call_method_or_proc_on(obj, string_symbol_or_proc, options = {}) ⇒ Object

Many configuration options (Ex: site_title, title_image) could either be static (String), methods (Symbol) or procs (Proc). This helper takes care of returning the content when String or call call_method_or_proc_on when Symbol or Proc.



70
71
72
73
74
75
76
77
# File 'lib/active_admin/view_helpers/method_or_proc_helper.rb', line 70

def render_or_call_method_or_proc_on(obj, string_symbol_or_proc, options = {})
  case string_symbol_or_proc
  when Symbol, Proc
    call_method_or_proc_on(obj, string_symbol_or_proc, options)
  when String
    string_symbol_or_proc
  end
end