aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVipul A M <vipulnsward@gmail.com>2013-04-19 13:20:11 +0530
committerVipul A M <vipulnsward@gmail.com>2013-04-19 21:20:32 +0530
commitdf24b8790f22384a068fece7042f04ffd2fcb33e (patch)
tree7fc49372e9c43a1ad4ab623ce0a469b03ed9fabb
parent87fddba98fb9de7c45340ab747ccc0aa29820a88 (diff)
downloadrails-df24b8790f22384a068fece7042f04ffd2fcb33e.tar.gz
rails-df24b8790f22384a068fece7042f04ffd2fcb33e.tar.bz2
rails-df24b8790f22384a068fece7042f04ffd2fcb33e.zip
fix HashWithIndifferentAccess#to_hash behaviour
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb10
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb4
2 files changed, 11 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 837db05dcc..b6c18fa54b 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -229,7 +229,11 @@ module ActiveSupport
# Convert to a regular hash with string keys.
def to_hash
- Hash.new(default).merge!(self)
+ _new_hash= {}
+ each do |key, value|
+ _new_hash[convert_key(key)] = convert_value(value,true)
+ end
+ Hash.new(default).merge!(_new_hash)
end
protected
@@ -237,9 +241,9 @@ module ActiveSupport
key.kind_of?(Symbol) ? key.to_s : key
end
- def convert_value(value)
+ def convert_value(value, _convert_for_to_hash = false)
if value.is_a? Hash
- value.nested_under_indifferent_access
+ _convert_for_to_hash ? value.to_hash : value.nested_under_indifferent_access
elsif value.is_a?(Array)
value = value.dup if value.frozen?
value.map! { |e| convert_value(e) }
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 30d95b75bc..b385e806bc 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -490,6 +490,10 @@ class HashExtTest < ActiveSupport::TestCase
roundtrip = mixed_with_default.with_indifferent_access.to_hash
assert_equal @strings, roundtrip
assert_equal '1234', roundtrip.default
+ new_to_hash = @nested_mixed.with_indifferent_access.to_hash
+ assert_not new_to_hash.instance_of?(HashWithIndifferentAccess)
+ assert_not new_to_hash["a"].instance_of?(HashWithIndifferentAccess)
+ assert_not new_to_hash["a"]["b"].instance_of?(HashWithIndifferentAccess)
end
def test_lookup_returns_the_same_object_that_is_stored_in_hash_indifferent_access