diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-11-09 03:01:34 -0800 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-11-09 03:01:34 -0800 |
commit | c7ef6b62c0fc6e5cb1cc63c9977ee1a9b45a17f2 (patch) | |
tree | 1b4fbf2b97fc7819ac047998872544941fa119ab /activerecord | |
parent | 90a5ec758dfb9698ecfd59bd08340ffecbda1d75 (diff) | |
parent | 8dbf5a4ddb1924b8460b166a29afb1265a1aaeff (diff) | |
download | rails-c7ef6b62c0fc6e5cb1cc63c9977ee1a9b45a17f2.tar.gz rails-c7ef6b62c0fc6e5cb1cc63c9977ee1a9b45a17f2.tar.bz2 rails-c7ef6b62c0fc6e5cb1cc63c9977ee1a9b45a17f2.zip |
Merge pull request #8082 from nikitug/backport_serialized_attributes_before_type_cast
Backport #8078: Fix `attributes_before_type_cast` for serialised attributes
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/serialization.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 10 |
3 files changed, 24 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 27c4839ac4..bcc7765d2e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 3.2.10 (unreleased) +* `AR::Base#attributes_before_type_cast` now returns unserialized values for serialized attributes. + + *Nikita Afanasenko* + * Fix issue that raises `NameError` when overriding the `accepts_nested_attributes` in child classes. Before: diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 00023b0b6c..cf4c35cf84 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -97,6 +97,16 @@ module ActiveRecord super end end + + def attributes_before_type_cast + super.dup.tap do |attributes| + self.class.serialized_attributes.each_key do |key| + if attributes.key?(key) + attributes[key] = attributes[key].unserialized_value + end + end + end + end end end end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 073e856e5e..d145486f64 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1293,6 +1293,16 @@ class BasicsTest < ActiveRecord::TestCase assert_equal({ :foo => :bar }, t.content_before_type_cast) end + def test_serialized_attributes_before_type_cast_returns_unserialized_value + Topic.serialize :content, Hash + + t = Topic.new(:content => { :foo => :bar }) + assert_equal({ :foo => :bar }, t.attributes_before_type_cast["content"]) + t.save! + t.reload + assert_equal({ :foo => :bar }, t.attributes_before_type_cast["content"]) + end + def test_serialized_attribute_calling_dup_method klass = Class.new(ActiveRecord::Base) klass.table_name = "topics" |