aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorLarry Sprock <larry@lucidbleu.com>2010-11-14 15:01:14 +0800
committerJosé Valim <jose.valim@gmail.com>2010-11-14 17:14:47 +0800
commitf43e5d160bf9708ad50b58c8168e38579769e024 (patch)
tree6e62b75009fd7b8fc4eb6541005c7ce7cab2df10 /activesupport
parent293c8a4de5bfe3274e0fda7dfab1b6ec296bb829 (diff)
downloadrails-f43e5d160bf9708ad50b58c8168e38579769e024.tar.gz
rails-f43e5d160bf9708ad50b58c8168e38579769e024.tar.bz2
rails-f43e5d160bf9708ad50b58c8168e38579769e024.zip
HashWithIndifferentAccess should not change the subclass of an array
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb2
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb17
2 files changed, 18 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 06dd18966e..320f5c1c92 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -144,7 +144,7 @@ module ActiveSupport
when Hash
self.class.new_from_hash_copying_default(value)
when Array
- value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e }
+ value.dup.replace(value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e })
else
value
end
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index d7b77e43bd..370f26b0d7 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -8,6 +8,9 @@ require 'active_support/core_ext/object/conversions'
class HashExtTest < Test::Unit::TestCase
class IndifferentHash < HashWithIndifferentAccess
end
+
+ class SubclassingArray < Array
+ end
def setup
@strings = { 'a' => 1, 'b' => 2 }
@@ -251,6 +254,20 @@ class HashExtTest < Test::Unit::TestCase
hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access
assert_equal "1", hash[:urls][:url].first[:address]
end
+
+ def test_should_preserve_array_subclass_when_value_is_array
+ array = SubclassingArray.new
+ array << { "address" => "1" }
+ hash = { "urls" => { "url" => array }}.with_indifferent_access
+ assert_equal SubclassingArray, hash[:urls][:url].class
+ end
+
+ def test_should_preserve_array_class_when_hash_value_is_frozen_array
+ array = SubclassingArray.new
+ array << { "address" => "1" }
+ hash = { "urls" => { "url" => array.freeze }}.with_indifferent_access
+ assert_equal SubclassingArray, hash[:urls][:url].class
+ end
def test_stringify_and_symbolize_keys_on_indifferent_preserves_hash
h = HashWithIndifferentAccess.new