Skip to content

Commit

Permalink
Add ability to pass number helpers (minus number_) to show() to apply…
Browse files Browse the repository at this point in the history
… those helpers to the value
  • Loading branch information
phallstrom committed Aug 11, 2011
1 parent 01f52ed commit 9bfe2e1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
Expand Up @@ -36,7 +36,7 @@ In our show action we can then do this:
<%= s.show :born_on, :value => '**censored**' %>
<%= s.show :updated_at, :format => '%B %d' %>
<%= s.show :created_at, :format => :short # Assumes you've defined a DATE_FORMAT for :short %>
<%= s.show :cash_in_wallet, :format => '$%.2f' %>
<%= s.show :cash_in_wallet, :to_currency => true %>
<%= s.show :height, :if => :tall %>
<%= s.show :weight, :unless => :sensitive %>
<% end %>
Expand Down Expand Up @@ -85,7 +85,7 @@ if you don't want that BR tag then set SimpleShow.clear_on_close to false.
== TODO:

- Add support for associations.
- Add options for formatting and booleans.
- Add options for booleans.

== License:

Expand Down
3 changes: 3 additions & 0 deletions lib/simple_show/base.rb
Expand Up @@ -54,6 +54,9 @@ def value(attr, options = {}, &block)
else
value = options[:format] % value
end
elsif (helper = %w[to_currency to_human to_human_size to_percentage to_phone with_delimiter with_precision].detect{|e| options[e.to_sym].present?})
options[helper.to_sym] = {} unless options[helper.to_sym].is_a? Hash
value = @binding.send("number_#{helper}", value, options[helper.to_sym])
end

@binding.content_tag(SimpleShow.value_tag, :class => SimpleShow.value_class) do
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Expand Up @@ -48,6 +48,7 @@ class SimpleShowTestCase < ActiveSupport::TestCase
include ActionController::RecordIdentifier
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::NumberHelper
attr_accessor :output_buffer

def setup
Expand Down
7 changes: 7 additions & 0 deletions test/test_simple_show_value.rb
Expand Up @@ -15,6 +15,8 @@ def setup
o += s.show(:html) {|o| '<b>html</b>' }
o += s.show :handicap, :format => '%.3f'
o += s.show :name, :format => '%20s'
o += s.show :to_currency, :value => '12345.67', :to_currency => true
o += s.show :with_delimeter, :value => '12345.67', :with_delimiter => true
o
end
)
Expand Down Expand Up @@ -43,4 +45,9 @@ def setup
assert_equal ' Philip Hallstrom', @doc.css('span.value')[7].text
end

test 'number helpers' do
assert_equal '$12,345.67', @doc.css('span.value')[8].text
assert_equal '12,345.67', @doc.css('span.value')[9].text
end

end

0 comments on commit 9bfe2e1

Please sign in to comment.