Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Do not mutate given options hash in number helpers

  • Loading branch information...
commit 74fabdf6c4c00827c657b4d6cef9ae6b96d3d0d2 1 parent c753e3a
@carlosantoniodasilva carlosantoniodasilva authored
View
16 actionpack/lib/action_view/helpers/number_helper.rb
@@ -57,7 +57,7 @@ def initialize(number)
# # => +1.123.555.1234 x 1343
def number_to_phone(number, options = {})
return unless number
- options.symbolize_keys!
+ options = options.symbolize_keys
parse_float(number, true) if options[:raise]
@@ -118,8 +118,7 @@ def number_to_phone(number, options = {})
# # => 1234567890,50 £
def number_to_currency(number, options = {})
return unless number
-
- options.symbolize_keys!
+ options = options.symbolize_keys
currency = translations_for('currency', options[:locale])
currency[:negative_format] ||= "-" + currency[:format] if currency[:format]
@@ -181,8 +180,7 @@ def number_to_currency(number, options = {})
# number_to_percentage("98a", :raise => true) # => InvalidNumberError
def number_to_percentage(number, options = {})
return unless number
-
- options.symbolize_keys!
+ options = options.symbolize_keys
defaults = format_translations('percentage', options[:locale])
options = defaults.merge!(options)
@@ -227,7 +225,7 @@ def number_to_percentage(number, options = {})
#
# number_with_delimiter("112a", :raise => true) # => raise InvalidNumberError
def number_with_delimiter(number, options = {})
- options.symbolize_keys!
+ options = options.symbolize_keys
parse_float(number, options[:raise]) or return number
@@ -271,7 +269,7 @@ def number_with_delimiter(number, options = {})
# number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
# # => 1.111,23
def number_with_precision(number, options = {})
- options.symbolize_keys!
+ options = options.symbolize_keys
number = (parse_float(number, options[:raise]) or return number)
@@ -338,7 +336,7 @@ def number_with_precision(number, options = {})
# number_to_human_size(1234567890123, :precision => 5) # => "1.1229 TB"
# number_to_human_size(524288000, :precision => 5) # => "500 MB"
def number_to_human_size(number, options = {})
- options.symbolize_keys!
+ options = options.symbolize_keys
number = (parse_float(number, options[:raise]) or return number)
@@ -448,7 +446,7 @@ def number_to_human_size(number, options = {})
# number_to_human(0.34, :units => :distance) # => "34 centimeters"
#
def number_to_human(number, options = {})
- options.symbolize_keys!
+ options = options.symbolize_keys
number = (parse_float(number, options[:raise]) or return number)
View
25 actionpack/test/template/number_helper_test.rb
@@ -268,6 +268,31 @@ def test_number_helpers_should_return_nil_when_given_nil
assert_nil number_to_human(nil)
end
+ def test_number_helpers_do_not_mutate_options_hash
+ options = { 'raise' => true }
+
+ number_to_phone(1, options)
+ assert_equal({ 'raise' => true }, options)
+
+ number_to_currency(1, options)
+ assert_equal({ 'raise' => true }, options)
+
+ number_to_percentage(1, options)
+ assert_equal({ 'raise' => true }, options)
+
+ number_with_delimiter(1, options)
+ assert_equal({ 'raise' => true }, options)
+
+ number_with_precision(1, options)
+ assert_equal({ 'raise' => true }, options)
+
+ number_to_human_size(1, options)
+ assert_equal({ 'raise' => true }, options)
+
+ number_to_human(1, options)
+ assert_equal({ 'raise' => true }, options)
+ end
+
def test_number_helpers_should_return_non_numeric_param_unchanged
assert_equal("+1-x x 123", number_to_phone("x", :country_code => 1, :extension => 123))
assert_equal("x", number_to_phone("x"))
Please sign in to comment.
Something went wrong with that request. Please try again.