1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix HWIA#to_hash behavior with array of hashes.

This commit is contained in:
kennyj 2013-05-15 19:59:02 +09:00
parent 877920ba55
commit f9c82f586c
2 changed files with 7 additions and 2 deletions

View file

@ -231,7 +231,7 @@ module ActiveSupport
def to_hash def to_hash
_new_hash= {} _new_hash= {}
each do |key, value| each do |key, value|
_new_hash[convert_key(key)] = convert_value(value,true) _new_hash[convert_key(key)] = convert_value(value, true)
end end
Hash.new(default).merge!(_new_hash) Hash.new(default).merge!(_new_hash)
end end
@ -246,7 +246,7 @@ module ActiveSupport
_convert_for_to_hash ? value.to_hash : value.nested_under_indifferent_access _convert_for_to_hash ? value.to_hash : value.nested_under_indifferent_access
elsif value.is_a?(Array) elsif value.is_a?(Array)
value = value.dup if value.frozen? value = value.dup if value.frozen?
value.map! { |e| convert_value(e) } value.map! { |e| convert_value(e, _convert_for_to_hash) }
else else
value value
end end

View file

@ -506,6 +506,11 @@ class HashExtTest < ActiveSupport::TestCase
def test_indifferent_hash_with_array_of_hashes def test_indifferent_hash_with_array_of_hashes
hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access
assert_equal "1", hash[:urls][:url].first[:address] assert_equal "1", hash[:urls][:url].first[:address]
hash = hash.to_hash
assert_not hash.instance_of?(HashWithIndifferentAccess)
assert_not hash["urls"].instance_of?(HashWithIndifferentAccess)
assert_not hash["urls"]["url"].first.instance_of?(HashWithIndifferentAccess)
end end
def test_should_preserve_array_subclass_when_value_is_array def test_should_preserve_array_subclass_when_value_is_array