提交 e08494a9 编写于 作者: S Sean Griffin

New records should remain new after yaml serialization

上级 87cc918d
* New records remain new after YAML serialization.
*Sean Griffin*
* PostgreSQL support default values for enum types. Fixes #7814.
*Yves Senn*
......
......@@ -284,7 +284,7 @@ def init_with(coder)
init_internals
@new_record = false
@new_record = coder['new_record']
self.class.define_attribute_methods
......@@ -354,6 +354,7 @@ def initialize_dup(other) # :nodoc:
# 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+
......
......@@ -49,7 +49,11 @@ def create(attributes = nil, &block)
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
......
......@@ -52,4 +52,21 @@ def test_cast_types_are_not_changed_on_round_trip
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册