Skip to content

Commit

Permalink
Add render_or_call_method_or_proc method.
Browse files Browse the repository at this point in the history
I'd love to find a better name for it.
  • Loading branch information
pcreux committed Feb 17, 2012
1 parent 32ad220 commit 0d91f8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/active_admin/view_helpers/method_or_proc_helper.rb
Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/unit/renderer_spec.rb
Expand Up @@ -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

0 comments on commit 0d91f8d

Please sign in to comment.