提交 3122d321 编写于 作者: M Michael Koziarski

Make sure AR::Base#clone handles attr changes made in after_initialize hooks. Closes #7191 [weyus]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7802 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 4eaa8ba5
......@@ -1751,9 +1751,9 @@ def destroy
def clone
attrs = self.attributes_before_type_cast
attrs.delete(self.class.primary_key)
self.class.new do |record|
record.send :instance_variable_set, '@attributes', attrs
end
record = self.class.new
record.send :instance_variable_set, '@attributes', attrs
record
end
# Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records.
......
......@@ -1008,6 +1008,10 @@ def test_clone
cloned_topic.title["a"] = "c"
assert_equal "b", topic.title["a"]
#test if attributes set as part of after_initialize are cloned correctly
assert_equal topic.author_email_address, cloned_topic.author_email_address
# test if saved clone object differs from original
cloned_topic.save
assert !cloned_topic.new_record?
assert cloned_topic.id != topic.id
......
......@@ -28,4 +28,10 @@ def default_written_on
def destroy_children
self.class.delete_all "parent_id = #{id}"
end
end
def after_initialize
if self.new_record?
self.author_email_address = 'test@test.com'
end
end
end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册