Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ability to pass number helpers (minus number_) to show() to apply…

… those helpers to the value
  • Loading branch information...
commit 9bfe2e188ccd88c5c86838a4fcbe7a0bc7def03d 1 parent 01f52ed
@phallstrom phallstrom authored
View
4 README.rdoc
@@ -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 %>
@@ -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:
View
3  lib/simple_show/base.rb
@@ -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
View
1  test/test_helper.rb
@@ -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
View
7 test/test_simple_show_value.rb
@@ -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
)
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.