Skip to content
This repository
Browse code

Merge branch 'fix-hwia' into 4-0-0

  • Loading branch information...
commit ac0d144d5f8169da4c3b28cf510a6074a0863ae6 2 parents 77ac25c + 72f7f94
Rafael Mendonça França authored June 03, 2013
22  activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -91,7 +91,7 @@ def self.[](*args)
91 91
     #
92 92
     # This value can be later fetched using either +:key+ or +'key'+.
93 93
     def []=(key, value)
94  
-      regular_writer(convert_key(key), convert_value(value))
  94
+      regular_writer(convert_key(key), convert_value(value, for: :assignment))
95 95
     end
96 96
 
97 97
     alias_method :store, :[]=
@@ -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, for: :to_hash)
  235
+      end
  236
+      Hash.new(default).merge!(_new_hash)
233 237
     end
234 238
 
235 239
     protected
@@ -237,12 +241,18 @@ 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, options = {})
241 245
         if value.is_a? Hash
242  
-          value.nested_under_indifferent_access
  246
+          if options[:for] == :to_hash
  247
+            value.to_hash
  248
+          else
  249
+            value.nested_under_indifferent_access
  250
+          end
243 251
         elsif value.is_a?(Array)
244  
-          value = value.dup if value.frozen?
245  
-          value.map! { |e| convert_value(e) }
  252
+          unless options[:for] == :assignment
  253
+            value = value.dup
  254
+          end
  255
+          value.map! { |e| convert_value(e, options) }
246 256
         else
247 257
           value
248 258
         end
16  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
@@ -499,9 +503,21 @@ def test_lookup_returns_the_same_object_that_is_stored_in_hash_indifferent_acces
499 503
     assert_equal [1], hash[:a]
500 504
   end
501 505
 
  506
+  def test_with_indifferent_access_has_no_side_effects_on_existing_hash
  507
+    hash = {content: [{:foo => :bar, 'bar' => 'baz'}]}
  508
+    hash.with_indifferent_access
  509
+
  510
+    assert_equal [:foo, "bar"], hash[:content].first.keys
  511
+  end
  512
+
502 513
   def test_indifferent_hash_with_array_of_hashes
503 514
     hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access
504 515
     assert_equal "1", hash[:urls][:url].first[:address]
  516
+
  517
+    hash = hash.to_hash
  518
+    assert_not hash.instance_of?(HashWithIndifferentAccess)
  519
+    assert_not hash["urls"].instance_of?(HashWithIndifferentAccess)
  520
+    assert_not hash["urls"]["url"].first.instance_of?(HashWithIndifferentAccess)
505 521
   end
506 522
 
507 523
   def test_should_preserve_array_subclass_when_value_is_array

0 notes on commit ac0d144

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