Permalink
Browse files

Merge pull request #15444 from sgrif/sg-yaml-new-record

New records should remain new after yaml serialization
  • Loading branch information...
2 parents 87cc918 + e08494a commit fab8d0795a4213e9210c7aa88d3b5653232bb221 @guilleiguaran guilleiguaran committed Jun 1, 2014
@@ -1,3 +1,7 @@
+* 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

0 comments on commit fab8d07

Please sign in to comment.