diff --git a/lib/active_admin/view_helpers/method_or_proc_helper.rb b/lib/active_admin/view_helpers/method_or_proc_helper.rb index 93172ce3ff5..578216b975a 100644 --- a/lib/active_admin/view_helpers/method_or_proc_helper.rb +++ b/lib/active_admin/view_helpers/method_or_proc_helper.rb @@ -23,4 +23,16 @@ def call_method_or_proc_on(obj, symbol_or_proc, options = {}) 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 diff --git a/spec/unit/renderer_spec.rb b/spec/unit/renderer_spec.rb index 50d78e37e88..9731a8fa02d 100644 --- a/spec/unit/renderer_spec.rb +++ b/spec/unit/renderer_spec.rb @@ -105,4 +105,22 @@ def to_html renderer.send(:call_method_or_proc_on, obj, p).should == obj.size end end + + describe "#render_or_call_method_or_proc_on" do + let(:renderer){ Renderer.new(action_view) } + let(:obj){ "Hello World" } + it "should return nil if no symbol or proc given" do + renderer.send(:render_or_call_method_or_proc_on, obj, 1).should == nil + end + it "should return the string if a string is given" do + renderer.send(:render_or_call_method_or_proc_on, obj, "Hello!").should == "Hello!" + end + it "should call the method if a symbol is given" do + renderer.send(:render_or_call_method_or_proc_on, obj, :size).should == obj.size + end + it "should call the proc with the object if a proc is given" do + p = Proc.new{|string| string.size } + renderer.send(:render_or_call_method_or_proc_on, obj, p).should == obj.size + end + end end