mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
clone and dup are working on 1.8
This commit is contained in:
parent
fe4388eb15
commit
d717cb2913
2 changed files with 27 additions and 3 deletions
|
@ -1591,19 +1591,28 @@ MSG
|
|||
@attributes.frozen?
|
||||
end
|
||||
|
||||
# Backport dup from 1.9 so that initialize_dup() gets called
|
||||
unless Object.respond_to?(:initialize_dup)
|
||||
def dup # :nodoc:
|
||||
copy = super
|
||||
copy.initialize_dup(self)
|
||||
copy
|
||||
end
|
||||
end
|
||||
|
||||
# Duped objects have no id assigned and are treated as new records. Note
|
||||
# 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)
|
||||
super
|
||||
_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
|
||||
|
||||
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
|
||||
|
||||
@changed_attributes = {}
|
||||
attributes_from_column_definition.each do |attr, orig_value|
|
||||
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
|
||||
|
@ -1612,7 +1621,7 @@ MSG
|
|||
clear_aggregation_cache
|
||||
clear_association_cache
|
||||
@attributes_cache = {}
|
||||
@persisted = false
|
||||
@persisted = false
|
||||
|
||||
ensure_proper_type
|
||||
populate_with_current_scope_attributes
|
||||
|
|
|
@ -9,6 +9,21 @@ module ActiveRecord
|
|||
assert !Topic.new.freeze.dup.frozen?
|
||||
end
|
||||
|
||||
def test_not_readonly
|
||||
topic = Topic.first
|
||||
|
||||
duped = topic.dup
|
||||
assert !topic.readonly?, 'should not be readonly'
|
||||
end
|
||||
|
||||
def test_is_readonly
|
||||
topic = Topic.first
|
||||
topic.readonly!
|
||||
|
||||
duped = topic.dup
|
||||
assert topic.readonly?, 'should be readonly'
|
||||
end
|
||||
|
||||
def test_dup_not_persisted
|
||||
topic = Topic.first
|
||||
duped = topic.dup
|
||||
|
|
Loading…
Reference in a new issue