fixing dup regressions
This commit is contained in:
parent
93d78b8318
commit
064c28d6c2
|
@ -1592,8 +1592,8 @@ MSG
|
|||
end
|
||||
|
||||
# Duped objects have no id assigned and are treated as new records. Note
|
||||
# that this is a "shallow" clone as it copies the object's attributes
|
||||
# only, not its associations. The extent of a "deep" dup is application
|
||||
# that this is a "shallow" copy as it copies the object's attributes
|
||||
# only, not its associations. The extent of a "deep" copy is application
|
||||
# specific and is therefore left to the application to implement according
|
||||
# to its need.
|
||||
def initialize_dup(other)
|
||||
|
@ -1603,7 +1603,11 @@ MSG
|
|||
cloned_attributes.delete(self.class.primary_key)
|
||||
|
||||
@attributes = cloned_attributes
|
||||
@changed_attributes = other.changed_attributes.dup
|
||||
|
||||
@changed_attributes = {}
|
||||
attributes_from_column_definition.each do |attr, orig_value|
|
||||
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
|
||||
end
|
||||
|
||||
clear_aggregation_cache
|
||||
clear_association_cache
|
||||
|
|
|
@ -701,24 +701,24 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert_equal({'a' => 'c'}.to_s, duped_topic.title)
|
||||
end
|
||||
|
||||
def test_clone_with_aggregate_of_same_name_as_attribute
|
||||
def test_dup_with_aggregate_of_same_name_as_attribute
|
||||
dev = DeveloperWithAggregate.find(1)
|
||||
assert_kind_of DeveloperSalary, dev.salary
|
||||
|
||||
clone = nil
|
||||
assert_nothing_raised { clone = dev.clone }
|
||||
assert_kind_of DeveloperSalary, clone.salary
|
||||
assert_equal dev.salary.amount, clone.salary.amount
|
||||
assert !clone.persisted?
|
||||
dup = nil
|
||||
assert_nothing_raised { dup = dev.dup }
|
||||
assert_kind_of DeveloperSalary, dup.salary
|
||||
assert_equal dev.salary.amount, dup.salary.amount
|
||||
assert !dup.persisted?
|
||||
|
||||
# test if the attributes have been cloned
|
||||
original_amount = clone.salary.amount
|
||||
# test if the attributes have been dupd
|
||||
original_amount = dup.salary.amount
|
||||
dev.salary.amount = 1
|
||||
assert_equal original_amount, clone.salary.amount
|
||||
assert_equal original_amount, dup.salary.amount
|
||||
|
||||
assert clone.save
|
||||
assert clone.persisted?
|
||||
assert_not_equal clone.id, dev.id
|
||||
assert dup.save
|
||||
assert dup.persisted?
|
||||
assert_not_equal dup.id, dev.id
|
||||
end
|
||||
|
||||
def test_dup_does_not_copy_associations
|
||||
|
@ -766,22 +766,22 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert !cloned_developer.salary_changed? # ... and cloned instance should behave same
|
||||
end
|
||||
|
||||
def test_clone_of_saved_object_marks_attributes_as_dirty
|
||||
def test_dup_of_saved_object_marks_attributes_as_dirty
|
||||
developer = Developer.create! :name => 'Bjorn', :salary => 100000
|
||||
assert !developer.name_changed?
|
||||
assert !developer.salary_changed?
|
||||
|
||||
cloned_developer = developer.clone
|
||||
cloned_developer = developer.dup
|
||||
assert cloned_developer.name_changed? # both attributes differ from defaults
|
||||
assert cloned_developer.salary_changed?
|
||||
end
|
||||
|
||||
def test_clone_of_saved_object_marks_as_dirty_only_changed_attributes
|
||||
def test_dup_of_saved_object_marks_as_dirty_only_changed_attributes
|
||||
developer = Developer.create! :name => 'Bjorn'
|
||||
assert !developer.name_changed? # both attributes of saved object should be threated as not changed
|
||||
assert !developer.salary_changed?
|
||||
|
||||
cloned_developer = developer.clone
|
||||
cloned_developer = developer.dup
|
||||
assert cloned_developer.name_changed? # ... but on cloned object should be
|
||||
assert !cloned_developer.salary_changed? # ... BUT salary has non-nil default which should be threated as not changed on cloned instance
|
||||
end
|
||||
|
|
|
@ -338,13 +338,13 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
assert !pirate.changed?
|
||||
end
|
||||
|
||||
def test_cloned_objects_should_not_copy_dirty_flag_from_creator
|
||||
def test_dup_objects_should_not_copy_dirty_flag_from_creator
|
||||
pirate = Pirate.create!(:catchphrase => "shiver me timbers")
|
||||
pirate_clone = pirate.clone
|
||||
pirate_clone.reset_catchphrase!
|
||||
pirate_dup = pirate.dup
|
||||
pirate_dup.reset_catchphrase!
|
||||
pirate.catchphrase = "I love Rum"
|
||||
assert pirate.catchphrase_changed?
|
||||
assert !pirate_clone.catchphrase_changed?
|
||||
assert !pirate_dup.catchphrase_changed?
|
||||
end
|
||||
|
||||
def test_reverted_changes_are_not_dirty
|
||||
|
|
|
@ -31,9 +31,12 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_dup_with_changes
|
||||
topic = Topic.first
|
||||
topic.author_name = 'Aaron'
|
||||
duped = topic.dup
|
||||
dbtopic = Topic.first
|
||||
topic = Topic.new
|
||||
|
||||
topic.attributes = dbtopic.attributes
|
||||
|
||||
duped = dbtopic.dup
|
||||
assert_equal topic.changes, duped.changes
|
||||
end
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ class InheritanceTest < ActiveRecord::TestCase
|
|||
|
||||
def test_class_with_blank_sti_name
|
||||
company = Company.find(:first)
|
||||
company = company.clone
|
||||
company = company.dup
|
||||
company.extend(Module.new {
|
||||
def read_attribute(name)
|
||||
return ' ' if name == 'type'
|
||||
|
|
Loading…
Reference in New Issue