mirror of
https://github.com/github/rails.git
synced 2026-02-19 02:14:20 -05:00
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
This commit is contained in:
@@ -1751,9 +1751,9 @@ module ActiveRecord #:nodoc:
|
||||
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 @@ class BasicsTest < Test::Unit::TestCase
|
||||
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
|
||||
|
||||
8
activerecord/test/fixtures/topic.rb
vendored
8
activerecord/test/fixtures/topic.rb
vendored
@@ -28,4 +28,10 @@ class Topic < ActiveRecord::Base
|
||||
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
|
||||
Reference in New Issue
Block a user