Skip to content
Browse files

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
  • Loading branch information...
2 parents bd01735 + c934632 commit 6bc179075d95030cee4c143b472345fa7f12b1b9 @rafaelfranca rafaelfranca committed Jan 29, 2014
View
12 activerecord/CHANGELOG.md
@@ -1,3 +1,15 @@
+* ActiveRecord objects can now be correctly dumped, loaded and dumped again without issues.
+
+ Previously, if you did `YAML.dump`, `YAML.load` and then `YAML.dump` again
+ in an ActiveRecord model that used serialization it would fail at the last
+ dump due to the fields not being correctly serialized before being dumped
+ to YAML. Now it is possible to dump and load the same object as many times
+ as needed without any issues.
+
+ Fixes #13861.
+
+ *Maurício Linhares*
+
* `find_in_batches` now returns an `Enumerator` when called without a block, so that it
can be chained with other `Enumerable` methods.
View
5 activerecord/lib/active_record/attribute_methods.rb
@@ -278,6 +278,11 @@ def attributes
}
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
View
11 activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -76,6 +76,7 @@ def accessor
end
class Attribute < Struct.new(:coder, :value, :state) # :nodoc:
+
def unserialized_value(v = value)
state == :serialized ? unserialize(v) : value
end
@@ -164,6 +165,16 @@ def typecasted_attribute_value(name)
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
View
2 activerecord/lib/active_record/core.rb
@@ -275,7 +275,7 @@ def initialize_dup(other) # :nodoc:
# 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+
View
23 activerecord/test/cases/store_test.rb
@@ -166,4 +166,27 @@ class StoreTest < ActiveRecord::TestCase
test "YAML coder initializes the store when a Nil value is given" do
assert_equal({}, @john.params)
end
+
+ test "attributes_for_coder should return stored fields already serialized" do
+ attributes = {
+ "id" => @john.id,
+ "name"=> @john.name,
+ "settings" => "--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\ncolor: black\n",
+ "preferences" => "--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nremember_login: true\n",
+ "json_data" => "{\"height\":\"tall\"}", "json_data_empty"=>"{\"is_a_good_guy\":true}",
+ "params" => "--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}\n",
+ "account_id"=> @john.account_id }
+ assert_equal attributes, @john.attributes_for_coder
+ end
+
+ test "dump, load and dump again a model" do
+ dumped = YAML.dump( @john )
+ loaded = YAML.load( dumped )
+ assert_equal @john, loaded
+
+ second_dump = YAML.dump( loaded )
+ assert_equal dumped, second_dump
+ assert_equal @john, YAML.load( second_dump )
+ end
+
end

0 comments on commit 6bc1790

Please sign in to comment.
Something went wrong with that request. Please try again.