aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource/base.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-03-28 21:17:38 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-03-28 21:17:38 +0000
commitad8df03f9c831d88b8a7eb80c1b7dbcf02fc1b19 (patch)
treee38fac0f66c06e576e0127a9553fed5f26c297ed /activeresource/lib/active_resource/base.rb
parent5c0656c9ee9ecc7ffeebea69dc0cd371d978b254 (diff)
downloadrails-ad8df03f9c831d88b8a7eb80c1b7dbcf02fc1b19.tar.gz
rails-ad8df03f9c831d88b8a7eb80c1b7dbcf02fc1b19.tar.bz2
rails-ad8df03f9c831d88b8a7eb80c1b7dbcf02fc1b19.zip
Add #clone. Closes #7352 [Ryan Daigle, thechrisoshow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9121 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib/active_resource/base.rb')
-rw-r--r--activeresource/lib/active_resource/base.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 37e77d4f93..d854b9baed 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -606,6 +606,43 @@ module ActiveResource
load(attributes)
end
+ # Returns a clone of the resource that hasn't been assigned an id yet and
+ # is treated as a new resource.
+ #
+ # ryan = Person.find(1)
+ # not_ryan = ryan.clone
+ # not_ryan.new? # => true
+ #
+ # Any active resource member attributes will NOT be cloned, though all other
+ # attributes are. This is to prevent the conflict between any prefix_options
+ # that refer to the original parent resource and the newly cloned parent
+ # resource that does not exist.
+ #
+ # ryan = Person.find(1)
+ # ryan.address = StreetAddress.find(1, :person_id => ryan.id)
+ # ryan.hash = {:not => "an ARes instance"}
+ #
+ # not_ryan = ryan.clone
+ # not_ryan.new? # => true
+ # not_ryan.address # => NoMethodError
+ # not_ryan.hash # => {:not => "an ARes instance"}
+ #
+ def clone
+ # Clone all attributes except the pk and any nested ARes
+ attrs = self.attributes.reject {|k,v| k == self.class.primary_key || v.is_a?(ActiveResource::Base)}.inject({}) do |attrs, (k, v)|
+ attrs[k] = v.clone
+ attrs
+ end
+ # Form the new resource - bypass initialize of resource with 'new' as that will call 'load' which
+ # attempts to convert hashes into member objects and arrays into collections of objects. We want
+ # the raw objects to be cloned so we bypass load by directly setting the attributes hash.
+ resource = self.class.new({})
+ resource.prefix_options = self.prefix_options
+ resource.send :instance_variable_set, '@attributes', attrs
+ resource
+ end
+
+
# A method to determine if the resource a new object (i.e., it has not been POSTed to the remote service yet).
#
# ==== Examples