Sha256: 6d80e35fdef69cf649e7408ed0a6df097eec6282b25149017bb29d857ddfdb01

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

module MethodOrProcHelper

  # 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 takes care of this functionality.
  #
  #   call_method_or_proc_on(@my_obj, :size) same as @my_obj.size
  # OR
  #   proc = Proc.new{|s| s.size }
  #   call_method_or_proc_on(@my_obj, proc)
  #
  def call_method_or_proc_on(obj, symbol_or_proc, options = {})
    exec = options[:exec].nil? ? true : options[:exec]
    case symbol_or_proc
    when Symbol, String
      obj.send(symbol_or_proc.to_sym)
    when Proc
      if exec
        instance_exec(obj, &symbol_or_proc)
      else
        symbol_or_proc.call(obj)
      end
    end
  end

  # 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.
  #
  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activeadmin-0.5.1 lib/active_admin/view_helpers/method_or_proc_helper.rb
activeadmin-0.5.0 lib/active_admin/view_helpers/method_or_proc_helper.rb
activeadmin-0.5.0.pre1 lib/active_admin/view_helpers/method_or_proc_helper.rb
activeadmin-0.5.0.pre lib/active_admin/view_helpers/method_or_proc_helper.rb
activeadmin-0.4.4 lib/active_admin/view_helpers/method_or_proc_helper.rb
activeadmin-0.4.3 lib/active_admin/view_helpers/method_or_proc_helper.rb
activeadmin-0.4.2 lib/active_admin/view_helpers/method_or_proc_helper.rb