提交 064c28d6 编写于 作者: A Aaron Patterson

fixing dup regressions

上级 93d78b83
......@@ -1592,8 +1592,8 @@ def frozen?
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)
......@@ -1602,8 +1602,12 @@ def initialize_dup(other)
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
cloned_attributes.delete(self.class.primary_key)
@attributes = cloned_attributes
@changed_attributes = other.changed_attributes.dup
@attributes = cloned_attributes
@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 @@ def test_dup
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 @@ def test_clone_of_new_object_marks_as_dirty_only_changed_attributes
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 @@ def test_reload_should_clear_changed_attributes
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 @@ def test_dup_with_modified_attributes
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 @@ def test_class_with_store_full_sti_class_returns_full_name
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'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册