Skip to content

Commit

Permalink
Implemented :helper_options for :display_with
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Campos committed Dec 25, 2011
1 parent 9013f21 commit 0c4f968
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/best_in_place/display_methods.rb
Expand Up @@ -8,7 +8,12 @@ def render_json(object)
when :model
{:display_as => object.send(opts[:method])}.to_json
when :helper
{:display_as => BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]))}.to_json
value = if opts[:helper_options]
BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]), opts[:helper_options])
else
BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]))
end
{:display_as => value}.to_json
else
{}.to_json
end
Expand All @@ -26,8 +31,8 @@ def add_model_method(klass, attr, display_as)
@@table[klass.to_s][attr.to_s] = Renderer.new :method => display_as.to_sym, :type => :model
end

def add_helper_method(klass, attr, helper_method)
@@table[klass.to_s][attr.to_s] = Renderer.new :method => helper_method.to_sym, :type => :helper, :attr => attr
def add_helper_method(klass, attr, helper_method, helper_options = nil)
@@table[klass.to_s][attr.to_s] = Renderer.new :method => helper_method.to_sym, :type => :helper, :attr => attr, :helper_options => helper_options
end
end
end
8 changes: 6 additions & 2 deletions lib/best_in_place/helper.rb
Expand Up @@ -67,8 +67,12 @@ def build_value_for(object, field, opts)
object.send(opts[:display_as]).to_s

elsif opts[:display_with]
BestInPlace::DisplayMethods.add_helper_method(object.class.to_s, field, opts[:display_with])
BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field))
BestInPlace::DisplayMethods.add_helper_method(object.class.to_s, field, opts[:display_with], opts[:helper_options])
if opts[:helper_options]
BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field), opts[:helper_options])
else
BestInPlace::ViewHelpers.send(opts[:display_with], object.send(field))
end

else
object.send(field).to_s.presence || ""
Expand Down
7 changes: 7 additions & 0 deletions spec/helpers/best_in_place_spec.rb
Expand Up @@ -151,6 +151,13 @@
it "should raise an error if the given helper can't be found" do
lambda { helper.best_in_place @user, :money, :display_with => :fk_number_to_currency }.should raise_error(ArgumentError)
end

it "should call the helper method with the given arguments" do
out = helper.best_in_place @user, :money, :display_with => :number_to_currency, :helper_options => {:unit => "º"}
nk = Nokogiri::HTML.parse(out)
span = nk.css("span")
span.text.should == "º150.00"
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/integration/double_init_spec.rb
Expand Up @@ -10,7 +10,8 @@
:zip => "25123",
:country => "2",
:receive_email => false,
:description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
:description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem.",
:money => 100
end

it "should be able to change a boolean value" do
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/js_spec.rb
Expand Up @@ -364,6 +364,17 @@
text.should == "40"
end

it "should show the money in euros" do
@user.save!
visit double_init_user_path(@user)

within("#alt_money") { page.should have_content("€100.00") }

bip_text @user, :money, 58

within("#alt_money") { page.should have_content("€58.00") }
end

end
end

7 changes: 7 additions & 0 deletions test_app/app/views/users/double_init.html.erb
Expand Up @@ -51,6 +51,13 @@
<td id="description">
<%= best_in_place @user, :description, :type => :textarea, :sanitize => false %>
</td>
</tr>
<tr>
<td>Alternative Money</td>
<td id="alt_money">
<%= best_in_place @user, :money, :display_with => :number_to_currency, :helper_options => {:unit => "€"} %>
</td>
</tr>
</table>
<br />
<hr />
Expand Down

0 comments on commit 0c4f968

Please sign in to comment.