diff options
author | Steve Klabnik <steve@steveklabnik.com> | 2013-02-22 14:00:24 -0800 |
---|---|---|
committer | Steve Klabnik <steve@steveklabnik.com> | 2013-02-22 14:00:24 -0800 |
commit | 4ffcd83dea4248652aa2bd92ada3b603ac179450 (patch) | |
tree | 4a175bbd86485f55e6647f82f1bc305536c2f550 | |
parent | e4c2a13b1be2aa22c98b9581110aa72b39176c47 (diff) | |
parent | 4e3ceedeba4f95d6f51380277c5562049ccdc264 (diff) | |
download | rails-4ffcd83dea4248652aa2bd92ada3b603ac179450.tar.gz rails-4ffcd83dea4248652aa2bd92ada3b603ac179450.tar.bz2 rails-4ffcd83dea4248652aa2bd92ada3b603ac179450.zip |
Merge pull request #9347 from hoffm/number_to_human_bug
Address edge case for number_to_human with units option.
-rw-r--r-- | actionpack/test/template/number_helper_test.rb | 5 | ||||
-rw-r--r-- | activesupport/CHANGELOG.md | 13 | ||||
-rw-r--r-- | activesupport/lib/active_support/number_helper.rb | 2 |
3 files changed, 19 insertions, 1 deletions
diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index d8fffe75ed..6a2d5aef18 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -266,6 +266,11 @@ class NumberHelperTest < ActionView::TestCase assert_equal '100<script></script>000 Quadrillion', number_to_human(10**20, :delimiter => "<script></script>") end + def test_number_to_human_with_custom_units_that_are_missing_the_needed_key + assert_equal '123', number_to_human(123, :units => {:thousand => 'k'}) + assert_equal '123', number_to_human(123, :units => {}) + end + def test_number_to_human_with_custom_format assert_equal '123 times Thousand', number_to_human(123456, :format => "%n times %u") volume = {:unit => "ml", :thousand => "lt", :million => "m3"} diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index df8cf5f8ec..16000d21b3 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,18 @@ ## Rails 4.0.0 (unreleased) ## +* `ActiveSupport::NumberHelper#number_to_human` returns the number unaltered when + the units hash does not contain the needed key, e.g. when the number provided is less + than the largest key proivided. + + Examples: + + number_to_human(123, :units => {}) # => 123 + number_to_human(123, :units => {:thousand => 'k'}) # => 123 + + Fixes #9269. + + *Michael Hoffman* + * Added beginning_of_minute support to core_ext calculations for Time and DateTime *Gagan Awhad* diff --git a/activesupport/lib/active_support/number_helper.rb b/activesupport/lib/active_support/number_helper.rb index 2191471daa..cc935e6cb9 100644 --- a/activesupport/lib/active_support/number_helper.rb +++ b/activesupport/lib/active_support/number_helper.rb @@ -580,7 +580,7 @@ module ActiveSupport unit = case units when Hash - units[DECIMAL_UNITS[display_exponent]] + units[DECIMAL_UNITS[display_exponent]] || '' when String, Symbol I18n.translate(:"#{units}.#{DECIMAL_UNITS[display_exponent]}", :locale => options[:locale], :count => number.to_i) else |