Skip to content
This repository
Browse code

fix HashWithIndifferentAccess#to_hash behaviour

  • Loading branch information...
commit df24b8790f22384a068fece7042f04ffd2fcb33e 1 parent 87fddba
Vipul A M authored April 19, 2013
10  activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -229,7 +229,11 @@ def to_options!; self end
229 229
 
230 230
     # Convert to a regular hash with string keys.
231 231
     def to_hash
232  
-      Hash.new(default).merge!(self)
  232
+      _new_hash= {}
  233
+      each do |key, value|
  234
+        _new_hash[convert_key(key)] = convert_value(value,true)
  235
+      end
  236
+      Hash.new(default).merge!(_new_hash)
233 237
     end
234 238
 
235 239
     protected
@@ -237,9 +241,9 @@ def convert_key(key)
237 241
         key.kind_of?(Symbol) ? key.to_s : key
238 242
       end
239 243
 
240  
-      def convert_value(value)
  244
+      def convert_value(value, _convert_for_to_hash = false)
241 245
         if value.is_a? Hash
242  
-          value.nested_under_indifferent_access
  246
+          _convert_for_to_hash ? value.to_hash : value.nested_under_indifferent_access
243 247
         elsif value.is_a?(Array)
244 248
           value = value.dup if value.frozen?
245 249
           value.map! { |e| convert_value(e) }
4  activesupport/test/core_ext/hash_ext_test.rb
@@ -490,6 +490,10 @@ def test_indifferent_to_hash
490 490
     roundtrip = mixed_with_default.with_indifferent_access.to_hash
491 491
     assert_equal @strings, roundtrip
492 492
     assert_equal '1234', roundtrip.default
  493
+    new_to_hash = @nested_mixed.with_indifferent_access.to_hash
  494
+    assert_not new_to_hash.instance_of?(HashWithIndifferentAccess)
  495
+    assert_not new_to_hash["a"].instance_of?(HashWithIndifferentAccess)
  496
+    assert_not new_to_hash["a"]["b"].instance_of?(HashWithIndifferentAccess)
493 497
   end
494 498
 
495 499
   def test_lookup_returns_the_same_object_that_is_stored_in_hash_indifferent_access

0 notes on commit df24b87

Please sign in to comment.
Something went wrong with that request. Please try again.