aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG.md14
-rw-r--r--actionpack/lib/action_view/helpers/number_helper.rb2
-rw-r--r--actionpack/test/template/number_helper_test.rb5
3 files changed, 20 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 80c0def5b3..8c7e3327f3 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,5 +1,19 @@
## 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.
+ Backport #9347.
+
+ *Michael Hoffman*
+
* Include I18n locale fallbacks in view lookup.
Fixes GH#3512.
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb
index 4519230fda..ad86d13456 100644
--- a/actionpack/lib/action_view/helpers/number_helper.rb
+++ b/actionpack/lib/action_view/helpers/number_helper.rb
@@ -593,7 +593,7 @@ module ActionView
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
diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb
index 8d679aac1d..37ce3cf6b6 100644
--- a/actionpack/test/template/number_helper_test.rb
+++ b/actionpack/test/template/number_helper_test.rb
@@ -251,6 +251,11 @@ class NumberHelperTest < ActionView::TestCase
assert_equal '4.5 tens', number_to_human(45, :units => {:unit => "", :ten => ' tens '})
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"}