diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-10-31 04:09:58 -0700 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-10-31 04:09:58 -0700 |
commit | 65697098811af50a0191a4fce3289b24335f96f9 (patch) | |
tree | 2881faff3383dc4207798cded01752ff397920f2 | |
parent | 85039d4b75ceb41a020e2cc8e3a0f39709b1fbc4 (diff) | |
parent | e7e59a75aad6c52b44cd97753b3dfce216833f1f (diff) | |
download | rails-65697098811af50a0191a4fce3289b24335f96f9.tar.gz rails-65697098811af50a0191a4fce3289b24335f96f9.tar.bz2 rails-65697098811af50a0191a4fce3289b24335f96f9.zip |
Merge pull request #8078 from nikitug/serialized_attributes_before_type_cast
Fix `attributes_before_type_cast` for serialized attributes.
-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/serialized_attribute_test.rb | 16 |
3 files changed, 27 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 7d318808de..2355bb8c9f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* `AR::Base#attributes_before_type_cast` now returns unserialized values for serialized attributes. + + *Nikita Afanasenko* + * Use query cache/uncache when using DATABASE_URL. Fix #6951. diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index ada79c3567..5b9ed81424 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -119,6 +119,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/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index f24ee54cd2..068f3cf3cd 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -56,11 +56,21 @@ class SerializedAttributeTest < ActiveRecord::TestCase def test_serialized_attribute_before_type_cast_returns_unserialized_value Topic.serialize :content, Hash - t = Topic.new(:content => { :foo => :bar }) - assert_equal({ :foo => :bar }, t.content_before_type_cast) + t = Topic.new(content: { foo: :bar }) + assert_equal({ foo: :bar }, t.content_before_type_cast) t.save! t.reload - assert_equal({ :foo => :bar }, t.content_before_type_cast) + 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 |