提交 1b075220 编写于 作者: A Aaron Patterson

Merge pull request #5810 from kennyj/fix_5797

Fix #5797. Error calling dup method on AR model with serialized field
...@@ -72,12 +72,13 @@ def serialize(attr_name, class_name = Object) ...@@ -72,12 +72,13 @@ def serialize(attr_name, class_name = Object)
self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder) self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder)
end end
def initialize_attributes(attributes) #:nodoc: def initialize_attributes(attributes, options = {}) #:nodoc:
super serialized = (options.delete(:serialized) { true }) ? :serialized : :unserialized
super(attributes, options)
serialized_attributes.each do |key, coder| serialized_attributes.each do |key, coder|
if attributes.key?(key) if attributes.key?(key)
attributes[key] = Attribute.new(coder, attributes[key], :serialized) attributes[key] = Attribute.new(coder, attributes[key], serialized)
end end
end end
......
...@@ -241,7 +241,7 @@ def init_with(coder) ...@@ -241,7 +241,7 @@ def init_with(coder)
## ##
def initialize_dup(other) # :nodoc: def initialize_dup(other) # :nodoc:
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast) cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
self.class.initialize_attributes(cloned_attributes) self.class.initialize_attributes(cloned_attributes, :serialized => false)
cloned_attributes.delete(self.class.primary_key) cloned_attributes.delete(self.class.primary_key)
......
...@@ -168,7 +168,7 @@ def update_counters(id, counters) ...@@ -168,7 +168,7 @@ def update_counters(id, counters)
# start the lock version at zero. Note we can't use # start the lock version at zero. Note we can't use
# <tt>locking_enabled?</tt> at this point as # <tt>locking_enabled?</tt> at this point as
# <tt>@attributes</tt> may not have been initialized yet. # <tt>@attributes</tt> may not have been initialized yet.
def initialize_attributes(attributes) #:nodoc: def initialize_attributes(attributes, options = {}) #:nodoc:
if attributes.key?(locking_column) && lock_optimistically if attributes.key?(locking_column) && lock_optimistically
attributes[locking_column] ||= 0 attributes[locking_column] ||= 0
end end
......
...@@ -1309,6 +1309,15 @@ def test_serialized_attribute_before_type_cast_returns_unserialized_value ...@@ -1309,6 +1309,15 @@ def test_serialized_attribute_before_type_cast_returns_unserialized_value
assert_equal({ :foo => :bar }, t.content_before_type_cast) assert_equal({ :foo => :bar }, t.content_before_type_cast)
end end
def test_serialized_attribute_calling_dup_method
klass = Class.new(ActiveRecord::Base)
klass.table_name = "topics"
klass.serialize :content, JSON
t = klass.new(:content => { :foo => :bar }).dup
assert_equal({ :foo => :bar }, t.content_before_type_cast)
end
def test_serialized_attribute_declared_in_subclass def test_serialized_attribute_declared_in_subclass
hash = { 'important1' => 'value1', 'important2' => 'value2' } hash = { 'important1' => 'value1', 'important2' => 'value2' }
important_topic = ImportantTopic.create("important" => hash) important_topic = ImportantTopic.create("important" => hash)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册