Skip to content

Commit

Permalink
Merge pull request #9804 from samsonasu/backport_number_to_human_fixes
Browse files Browse the repository at this point in the history
Backport #9347 to rails 3.2
  • Loading branch information
rafaelfranca committed Mar 19, 2013
2 parents 2cf38ae + 1b12d08 commit 493c10e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions 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.

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/number_helper.rb
Expand Up @@ -593,7 +593,7 @@ def number_to_human(number, options = {})

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
Expand Down
5 changes: 5 additions & 0 deletions actionpack/test/template/number_helper_test.rb
Expand Up @@ -251,6 +251,11 @@ def test_number_to_human_with_custom_units
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"}
Expand Down

0 comments on commit 493c10e

Please sign in to comment.