提交 3727d1e0 编写于 作者: R Ryuta Kamizono

`becomes` should clear the mutation tracker which is created in `after_initialize`

`becomes` creates new object and copies attributes from the receiver. If
new object has mutation tracker which is created in `after_initialize`,
it should be cleared since it is for discarded attributes.

But if the receiver doesn't have mutation tracker yet, it will not be
cleared properly.

It should be cleared regardless of whether the receiver has mutation
tracker or not.

Fixes #32867.
上级 8416a592
* `becomes` should clear the mutation tracker which is created in `after_initialize`.
Fixes #32867.
*Ryuta Kamizono*
* Allow a belonging to parent object to be created from a new record.
*Jolyon Pawlyn*
......
......@@ -119,7 +119,7 @@ def changed_attribute_names_to_save
# Alias for +changed_attributes+
def attributes_in_database
changes_to_save.transform_values(&:first)
mutations_from_database.changed_values
end
private
......
......@@ -373,7 +373,7 @@ def becomes(klass)
became = klass.allocate
became.send(:initialize)
became.instance_variable_set("@attributes", @attributes)
became.instance_variable_set("@mutations_from_database", @mutations_from_database) if defined?(@mutations_from_database)
became.instance_variable_set("@mutations_from_database", @mutations_from_database ||= nil)
became.instance_variable_set("@changed_attributes", attributes_changed_by_setter)
became.instance_variable_set("@new_record", new_record?)
became.instance_variable_set("@destroyed", destroyed?)
......
......@@ -290,6 +290,17 @@ def test_duped_becomes_persists_changes_from_the_original
assert_equal "The First Topic", Topic.find(copy.id).title
end
def test_becomes_wont_break_mutation_tracking
topic = topics(:first)
reply = topic.becomes(Reply)
assert_equal 1, topic.id_in_database
assert_empty topic.attributes_in_database
assert_equal 1, reply.id_in_database
assert_empty reply.attributes_in_database
end
def test_becomes_includes_changed_attributes
company = Company.new(name: "37signals")
client = company.becomes(Client)
......
......@@ -97,7 +97,7 @@ def destroy_children
end
def set_email_address
unless persisted?
unless persisted? || will_save_change_to_author_email_address?
self.author_email_address = "test@test.com"
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册