From c9346322b15c15f51234c33a3db1b3895ffe84ab Mon Sep 17 00:00:00 2001 From: Mauricio Linhares Date: Wed, 29 Jan 2014 00:54:13 -0300 Subject: Fixing issue with activerecord serialization not being able to dump a record after loading it from YAML - fixes #13861 --- activerecord/lib/active_record/attribute_methods.rb | 5 +++++ .../lib/active_record/attribute_methods/serialization.rb | 11 +++++++++++ activerecord/lib/active_record/core.rb | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index ccbff8d1ff..9326c9c117 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -278,6 +278,11 @@ module ActiveRecord } end + # Placeholder so it can be overriden when needed by serialization + def attributes_for_coder # :nodoc: + attributes + end + # Returns an #inspect-like string for the value of the # attribute +attr_name+. String attributes are truncated upto 50 # characters, Date and Time attributes are returned in the diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 3227464032..93e3ca8000 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -76,6 +76,7 @@ module ActiveRecord end class Attribute < Struct.new(:coder, :value, :state) # :nodoc: + def unserialized_value(v = value) state == :serialized ? unserialize(v) : value end @@ -164,6 +165,16 @@ module ActiveRecord super end end + + def attributes_for_coder + attribute_names.each_with_object({}) do |name,attrs| + attrs[name] = if self.class.serialized_attributes.include?(name) + @attributes[name].serialized_value + else + read_attribute(name) + end + end + end end end end diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 6f02c763fe..6303fe5ee4 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -275,7 +275,7 @@ module ActiveRecord # Post.new.encode_with(coder) # coder # => {"attributes" => {"id" => nil, ... }} def encode_with(coder) - coder['attributes'] = attributes + coder['attributes'] = attributes_for_coder end # Returns true if +comparison_object+ is the same exact object, or +comparison_object+ -- cgit v1.2.3