diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-29 20:44:58 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-29 20:44:58 -0200 |
commit | 6bc179075d95030cee4c143b472345fa7f12b1b9 (patch) | |
tree | 0cd9fca8ca77d7326254212238deb394dee8d888 /activerecord/lib | |
parent | bd0173574ca908acca3f29b9da7db03bd6a89436 (diff) | |
parent | c9346322b15c15f51234c33a3db1b3895ffe84ab (diff) | |
download | rails-6bc179075d95030cee4c143b472345fa7f12b1b9.tar.gz rails-6bc179075d95030cee4c143b472345fa7f12b1b9.tar.bz2 rails-6bc179075d95030cee4c143b472345fa7f12b1b9.zip |
Merge pull request #13867 from mauricio/bug-13861
Fixing issue with activerecord serialization not being able to dump a record after loading it from YAML - fixes #13861
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/serialization.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 2 |
3 files changed, 17 insertions, 1 deletions
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 <tt>#inspect</tt>-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+ |