1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

dup keeps changes

This commit is contained in:
Aaron Patterson 2010-11-23 10:58:19 -08:00
parent ca7b0a0d1a
commit 5badf60d12
2 changed files with 38 additions and 30 deletions

View file

@ -1382,30 +1382,6 @@ MSG
result result
end end
# Cloned 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" clone is
# application specific and is therefore left to the application to implement according to its need.
def initialize_copy(other)
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
cloned_attributes.delete(self.class.primary_key)
@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
@attributes_cache = {}
@persisted = false
ensure_proper_type
populate_with_current_scope_attributes
end
# Initialize an empty model object from +coder+. +coder+ must contain # Initialize an empty model object from +coder+. +coder+ must contain
# the attributes necessary for initializing an empty model object. For # the attributes necessary for initializing an empty model object. For
# example: # example:
@ -1420,7 +1396,7 @@ MSG
@attributes = coder['attributes'] @attributes = coder['attributes']
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {} @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
@readonly = @destroyed = @marked_for_destruction = false @readonly = @destroyed = @marked_for_destruction = false
@persisted = false @persisted = true
_run_find_callbacks _run_find_callbacks
_run_initialize_callbacks _run_initialize_callbacks
end end
@ -1615,15 +1591,32 @@ MSG
@attributes.frozen? @attributes.frozen?
end 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
# specific and is therefore left to the application to implement according
# to its need.
def initialize_dup(other) def initialize_dup(other)
super super
init_with 'attributes' => other.attributes 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_cache = {}
@persisted = false
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
clear_aggregation_cache
clear_association_cache
ensure_proper_type
populate_with_current_scope_attributes
self self
end end
# Returns duplicated record with unfreezed attributes. def initialize_clone(other)
def dup
super super
@persisted = other.persisted?
end end
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back # Returns +true+ if the record is read only. Records loaded through joins with piggy-back

View file

@ -6,7 +6,7 @@ module ActiveRecord
fixtures :topics fixtures :topics
def test_dup def test_dup
assert !Minimalistic.new.freeze.dup.frozen? assert !Topic.new.freeze.dup.frozen?
end end
def test_dup_not_persisted def test_dup_not_persisted
@ -26,6 +26,7 @@ module ActiveRecord
def test_clone_persisted def test_clone_persisted
topic = Topic.first topic = Topic.first
cloned = topic.clone cloned = topic.clone
assert topic.persisted?, 'topic persisted'
assert cloned.persisted?, 'topic persisted' assert cloned.persisted?, 'topic persisted'
assert !cloned.new_record?, 'topic is not new' assert !cloned.new_record?, 'topic is not new'
end end
@ -37,7 +38,21 @@ module ActiveRecord
cloned = topic.clone cloned = topic.clone
assert cloned.persisted?, 'topic persisted' assert cloned.persisted?, 'topic persisted'
assert !cloned.new_record?, 'topic is not new' assert !cloned.new_record?, 'topic is not new'
assert cloned.frozen?, 'topic is frozen' assert cloned.frozen?, 'topic should be frozen'
end
def test_dup_with_modified_attributes
topic = Topic.first
topic.author_name = 'Aaron'
duped = topic.dup
assert_equal 'Aaron', duped.author_name
end
def test_dup_with_changes
topic = Topic.first
topic.author_name = 'Aaron'
duped = topic.dup
assert_equal topic.changes, duped.changes
end end
end end
end end