mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Fix attribute_before_type_cast for serialized attributes. Fixes #4837.
Conflicts: activerecord/lib/active_record/core.rb
This commit is contained in:
@@ -88,6 +88,14 @@ module ActiveRecord
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def read_attribute_before_type_cast(attr_name)
|
||||
if serialized_attributes.include?(attr_name)
|
||||
super.unserialized_value
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -531,6 +531,8 @@ module ActiveRecord #:nodoc:
|
||||
# The dup method does not preserve the timestamps (created|updated)_(at|on).
|
||||
def initialize_dup(other)
|
||||
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
|
||||
self.class.initialize_attributes(cloned_attributes)
|
||||
|
||||
cloned_attributes.delete(self.class.primary_key)
|
||||
|
||||
@attributes = cloned_attributes
|
||||
|
||||
@@ -1251,6 +1251,21 @@ class BasicsTest < ActiveRecord::TestCase
|
||||
assert_equal(hash, important_topic.content)
|
||||
end
|
||||
|
||||
# This test was added to fix GH #4004. Obviously the value returned
|
||||
# is not really the value 'before type cast' so we should maybe think
|
||||
# about changing that in the future.
|
||||
def test_serialized_attribute_before_type_cast_returns_unserialized_value
|
||||
klass = Class.new(ActiveRecord::Base)
|
||||
klass.table_name = "topics"
|
||||
klass.serialize :content, Hash
|
||||
|
||||
t = klass.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)
|
||||
end
|
||||
|
||||
def test_serialized_attribute_declared_in_subclass
|
||||
hash = { 'important1' => 'value1', 'important2' => 'value2' }
|
||||
important_topic = ImportantTopic.create("important" => hash)
|
||||
|
||||
Reference in New Issue
Block a user