diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index bd70d2cde48f4ddbd8d102a87e9e48799a1d11d1..6818f32f5556486484cb00b5bea22db4e0541aa4 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix issue with #class_inheritable_accessor saving updates to the parent class when initialized with an Array or Hash [mojombo] + * Hash#to_xml supports Bignum and BigDecimal. #6313 [edibiase] * Don't undefine #class in OptionMerger [Rick] diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index 2e70a644362122e895c0167eab2225b77c3c3112..bc8bf09ab206b9fa1947f8190fd48fef1eaad6f1 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -107,7 +107,12 @@ def reset_inheritable_attributes private def inherited_with_inheritable_attributes(child) inherited_without_inheritable_attributes(child) if respond_to?(:inherited_without_inheritable_attributes) - child.instance_variable_set('@inheritable_attributes', inheritable_attributes.dup) + + new_inheritable_attributes = inheritable_attributes.inject({}) do |memo, (key, value)| + memo.update(key => (value.dup rescue value)) + end + + child.instance_variable_set('@inheritable_attributes', new_inheritable_attributes) end alias inherited_without_inheritable_attributes inherited diff --git a/activesupport/test/class_inheritable_attributes_test.rb b/activesupport/test/class_inheritable_attributes_test.rb index 54e533e964073dc518b35943a43f31d1862fce16..048b86135cc991f2db81018a2c8d4bdd01bafe7c 100644 --- a/activesupport/test/class_inheritable_attributes_test.rb +++ b/activesupport/test/class_inheritable_attributes_test.rb @@ -137,4 +137,34 @@ def test_inheritance assert_equal 'b', @klass.b assert_equal 'B', @sub.b end + + def test_array_inheritance + @klass.class_inheritable_accessor :a + @klass.a = [] + + @sub = eval("class SubbyArray < @klass; end; SubbyArray") + + assert_equal [], @klass.a + assert_equal [], @sub.a + + @sub.a << :first + + assert_equal [:first], @sub.a + assert_equal [], @klass.a + end + + def test_array_inheritance + @klass.class_inheritable_accessor :a + @klass.a = {} + + @sub = eval("class SubbyHash < @klass; end; SubbyHash") + + assert_equal Hash.new, @klass.a + assert_equal Hash.new, @sub.a + + @sub.a[:first] = :first + + assert_equal 1, @sub.a.keys.size + assert_equal 0, @klass.a.keys.size + end end