Permalink
Browse files

Merge pull request #8078 from nikitug/serialized_attributes_before_ty…

…pe_cast

Fix `attributes_before_type_cast` for serialized attributes.
  • Loading branch information...
2 parents 85039d4 + e7e59a7 commit 65697098811af50a0191a4fce3289b24335f96f9 @carlosantoniodasilva carlosantoniodasilva committed Oct 31, 2012
@@ -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.
@@ -119,6 +119,16 @@ def read_attribute_before_type_cast(attr_name)
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
@@ -56,11 +56,21 @@ def test_serialized_attribute_in_base_class
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

0 comments on commit 6569709

Please sign in to comment.