diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-05-30 11:26:59 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-30 15:50:24 -0300 |
commit | c470001891990b067f9e76b5a5c0ae49be1a507f (patch) | |
tree | acabfb5ccaff4b31901d0759abd6d57d937f327b /activerecord | |
parent | f09ae8538f3625468ee175d5a4d9782e0d5ba415 (diff) | |
download | rails-c470001891990b067f9e76b5a5c0ae49be1a507f.tar.gz rails-c470001891990b067f9e76b5a5c0ae49be1a507f.tar.bz2 rails-c470001891990b067f9e76b5a5c0ae49be1a507f.zip |
Merge pull request #5810 from kennyj/fix_5797
Fix #5797. Error calling dup method on AR model with serialized field
Conflicts:
activerecord/lib/active_record/core.rb
Diffstat (limited to 'activerecord')
4 files changed, 15 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 1798afc3b7..00023b0b6c 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -58,12 +58,13 @@ module ActiveRecord self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder) end - def initialize_attributes(attributes) #:nodoc: - super + def initialize_attributes(attributes, options = {}) #:nodoc: + serialized = (options.delete(:serialized) { true }) ? :serialized : :unserialized + super(attributes, options) serialized_attributes.each do |key, coder| if attributes.key?(key) - attributes[key] = Attribute.new(coder, attributes[key], :serialized) + attributes[key] = Attribute.new(coder, attributes[key], serialized) end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8c8717b759..2ac259d52d 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -533,7 +533,7 @@ 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) + self.class.initialize_attributes(cloned_attributes, :serialized => false) cloned_attributes.delete(self.class.primary_key) diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 0e33cdb617..7deac2588a 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -170,7 +170,7 @@ module ActiveRecord # start the lock version at zero. Note we can't use # <tt>locking_enabled?</tt> at this point as # <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 attributes[locking_column] ||= 0 end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index f6497d9bad..b065919310 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1280,6 +1280,15 @@ class BasicsTest < ActiveRecord::TestCase assert_equal({ :foo => :bar }, t.content_before_type_cast) 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 hash = { 'important1' => 'value1', 'important2' => 'value2' } important_topic = ImportantTopic.create("important" => hash) |