Skip to content

Commit

Permalink
Call initialize_copy when cloning [#3164 state:resolved]
Browse files Browse the repository at this point in the history
Cloned AR objects are now instantiated through initialize_copy rather than
new/initialize. This allows AR classes to override initialize_copy in order to
implement deep cloning.

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
paulgillard authored and josh committed Oct 6, 2009
1 parent 126f623 commit 6361d42
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
65 changes: 38 additions & 27 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -2427,6 +2427,29 @@ def initialize(attributes = nil)
result
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)
# Think the assertion which fails if the after_initialize callback goes at the end of the method is wrong. The
# deleted clone method called new which therefore called the after_initialize callback. It then went on to copy
# over the attributes. But if it's copying the attributes afterwards then it hasn't finished initializing right?
# For example in the test suite the topic model's after_initialize method sets the author_email_address to
# test@test.com. I would have thought this would mean that all cloned models would have an author email address
# of test@test.com. However the test_clone test method seems to test that this is not the case. As a result the
# after_initialize callback has to be run *before* the copying of the atrributes rather than afterwards in order
# for all tests to pass. This makes no sense to me.
callback(:after_initialize) if respond_to_without_attributes?(:after_initialize)
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
cloned_attributes.delete(self.class.primary_key)
@attributes = cloned_attributes
clear_aggregation_cache
@attributes_cache = {}
@new_record = true
ensure_proper_type
self.class.send(:scope, :create).each { |att, value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create)
end

# Returns a String, which Action Pack uses for constructing an URL to this
# object. The default implementation returns this record's id as a String,
# or nil if this record's unsaved.
Expand Down Expand Up @@ -2555,19 +2578,6 @@ def destroy
freeze
end

# Returns a clone of the record that hasn't been assigned an id yet and
# is treated as a new record. Note that this is a "shallow" clone:
# 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 clone
attrs = clone_attributes(:read_attribute_before_type_cast)
attrs.delete(self.class.primary_key)
record = self.class.new
record.send :instance_variable_set, '@attributes', attrs
record
end

# Returns an instance of the specified +klass+ with the attributes of the current record. This is mostly useful in relation to
# single-table inheritance structures where you want a subclass to appear as the superclass. This can be used along with record
# identification in Action Pack to allow, say, <tt>Client < Company</tt> to do something like render <tt>:partial => @client.becomes(Company)</tt>
Expand Down Expand Up @@ -2831,6 +2841,21 @@ def inspect
"#<#{self.class} #{attributes_as_nice_string}>"
end

protected
def clone_attributes(reader_method = :read_attribute, attributes = {})
self.attribute_names.inject(attributes) do |attrs, name|
attrs[name] = clone_attribute_value(reader_method, name)
attrs
end
end

def clone_attribute_value(reader_method, attribute_name)
value = send(reader_method, attribute_name)
value.duplicable? ? value.clone : value
rescue TypeError, NoMethodError
value
end

private
def create_or_update
raise ReadOnlyRecord if readonly?
Expand Down Expand Up @@ -3093,20 +3118,6 @@ def object_from_yaml(string)
return string unless string.is_a?(String) && string =~ /^---/
YAML::load(string) rescue string
end

def clone_attributes(reader_method = :read_attribute, attributes = {})
self.attribute_names.inject(attributes) do |attrs, name|
attrs[name] = clone_attribute_value(reader_method, name)
attrs
end
end

def clone_attribute_value(reader_method, attribute_name)
value = send(reader_method, attribute_name)
value.duplicable? ? value.clone : value
rescue TypeError, NoMethodError
value
end
end

Base.class_eval do
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/base_test.rb
Expand Up @@ -1352,7 +1352,7 @@ def test_clone
cloned_topic.title["a"] = "c"
assert_equal "b", topic.title["a"]

#test if attributes set as part of after_initialize are cloned correctly
# 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
Expand Down

0 comments on commit 6361d42

Please sign in to comment.