New records should remain new after yaml serialization

This commit is contained in:
Sean Griffin 2014-05-31 06:01:59 -07:00
parent 87cc918daa
commit e08494a912
4 changed files with 28 additions and 2 deletions

View File

@ -1,3 +1,7 @@
* New records remain new after YAML serialization.
*Sean Griffin*
* PostgreSQL support default values for enum types. Fixes #7814.
*Yves Senn*

View File

@ -284,7 +284,7 @@ module ActiveRecord
init_internals
@new_record = false
@new_record = coder['new_record']
self.class.define_attribute_methods
@ -354,6 +354,7 @@ module ActiveRecord
# coder # => {"attributes" => {"id" => nil, ... }}
def encode_with(coder)
coder['attributes'] = @raw_attributes
coder['new_record'] = new_record?
end
# Returns true if +comparison_object+ is the same exact object, or +comparison_object+

View File

@ -49,7 +49,11 @@ module ActiveRecord
def instantiate(attributes, column_types = {})
klass = discriminate_class_for_record(attributes)
column_types = klass.decorate_columns(column_types.dup)
klass.allocate.init_with('attributes' => attributes, 'column_types' => column_types)
klass.allocate.init_with(
'attributes' => attributes,
'column_types' => column_types,
'new_record' => false,
)
end
private

View File

@ -52,4 +52,21 @@ class YamlSerializationTest < ActiveRecord::TestCase
assert_equal 123, topic.parent_id
assert_equal 123, YAML.load(YAML.dump(topic)).parent_id
end
def test_new_records_remain_new_after_round_trip
topic = Topic.new
assert topic.new_record?, "Sanity check that new records are new"
assert YAML.load(YAML.dump(topic)).new_record?, "Record should be new after deserialization"
topic.save!
assert_not topic.new_record?, "Saved records are not new"
assert_not YAML.load(YAML.dump(topic)).new_record?, "Saved record should not be new after deserialization"
topic = Topic.select('title').last
assert_not topic.new_record?, "Loaded records without ID are not new"
assert_not YAML.load(YAML.dump(topic)).new_record?, "Record should not be new after deserialization"
end
end