Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update deep_cloning for Rails 3.1

  • Loading branch information...
commit 9ea0c54d3a3f74b570d76143125055742a1d44a4 1 parent f0dcbeb
@DefV authored
View
12 README.rdoc
@@ -10,19 +10,19 @@ Install with:
== Example
=== Cloning a model without an attribute
- pirate.clone :except => :name
+ pirate.dup :except => :name
=== Cloning a model without multiple attributes
- pirate.clone :except => [:name, :nick_name]
+ pirate.dup :except => [:name, :nick_name]
=== Cloning one single association
- pirate.clone :include => :mateys
+ pirate.dup :include => :mateys
=== Cloning multiple associations
- pirate.clone :include => [:mateys, :treasures]
+ pirate.dup :include => [:mateys, :treasures]
=== Cloning really deep
- pirate.clone :include => {:treasures => :gold_pieces}
+ pirate.dup :include => {:treasures => :gold_pieces}
=== Cloning really deep with multiple associations
- pirate.clone :include => [:mateys, {:treasures => :gold_pieces}]
+ pirate.dup :include => [:mateys, {:treasures => :gold_pieces}]
Copyright (c) 2008 Jan De Poorter, released under the MIT license
View
12 lib/deep_cloning.rb
@@ -2,7 +2,7 @@
module DeepCloning
def self.included(base) #:nodoc:
- base.alias_method_chain :clone, :deep_cloning
+ base.alias_method_chain :dup, :deep_cloning
end
# clones an ActiveRecord model.
@@ -28,12 +28,12 @@ def self.included(base) #:nodoc:
# ==== Cloning really deep with multiple associations
# pirate.clone :include => [:mateys, {:treasures => :gold_pieces}]
#
- def clone_with_deep_cloning options = {}
- kopy = clone_without_deep_cloning
+ def dup_with_deep_cloning options = {}
+ kopy = dup_without_deep_cloning()
if options[:except]
Array(options[:except]).each do |attribute|
- kopy.write_attribute(attribute, attributes_from_column_definition[attribute.to_s])
+ kopy.send("#{attribute}=", self.class.column_defaults[attribute.to_s])
end
end
@@ -46,9 +46,9 @@ def clone_with_deep_cloning options = {}
opts = deep_associations.blank? ? {} : {:include => deep_associations}
cloned_object = case self.class.reflect_on_association(association).macro
when :belongs_to, :has_one
- self.send(association) && self.send(association).clone(opts)
+ self.send(association) && self.send(association).dup(opts)
when :has_many, :has_and_belongs_to_many
- self.send(association).collect { |obj| obj.clone(opts) }
+ self.send(association).collect { |obj| obj.dup(opts) }
end
kopy.send("#{association}=", cloned_object)
end
View
22 test/deep_cloning_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/test_helper'
-class DeepCloningTest < Test::Unit::TestCase
+class DeepCloningTest < ActiveSupport::TestCase
fixtures :pirates, :gold_pieces, :treasures, :mateys, :parrots
def setup
@@ -9,15 +9,15 @@ def setup
end
def test_single_clone_exception
- clone = @jack.clone(:except => :name)
+ clone = @jack.dup(:except => :name)
assert clone.save
- assert_equal pirates(:jack).name, @jack.clone.name # Old behavour
+ assert_equal pirates(:jack).name, @jack.dup.name # Old behavour
assert_nil clone.name
assert_equal pirates(:jack).nick_name, clone.nick_name
end
def test_multiple_clone_exception
- clone = @jack.clone(:except => [:name, :nick_name])
+ clone = @jack.dup(:except => [:name, :nick_name])
assert clone.save
assert_nil clone.name
assert_equal 'no nickname', clone.nick_name
@@ -25,27 +25,27 @@ def test_multiple_clone_exception
end
def test_single_include_association
- clone = @jack.clone(:include => :mateys)
+ clone = @jack.dup(:include => :mateys)
assert clone.save
assert_equal 1, clone.mateys.size
end
def test_multiple_include_association
- clone = @jack.clone(:include => [:mateys, :treasures])
+ clone = @jack.dup(:include => [:mateys, :treasures])
assert clone.save
assert_equal 1, clone.mateys.size
assert_equal 1, clone.treasures.size
end
def test_deep_include_association
- clone = @jack.clone(:include => {:treasures => :gold_pieces})
+ clone = @jack.dup(:include => {:treasures => :gold_pieces})
assert clone.save
assert_equal 1, clone.treasures.size
assert_equal 1, clone.gold_pieces.size
end
def test_multiple_and_deep_include_association
- clone = @jack.clone(:include => {:treasures => :gold_pieces, :mateys => {}})
+ clone = @jack.dup(:include => {:treasures => :gold_pieces, :mateys => {}})
assert clone.save
assert_equal 1, clone.treasures.size
assert_equal 1, clone.gold_pieces.size
@@ -53,7 +53,7 @@ def test_multiple_and_deep_include_association
end
def test_multiple_and_deep_include_association_with_array
- clone = @jack.clone(:include => [{:treasures => :gold_pieces}, :mateys])
+ clone = @jack.dup(:include => [{:treasures => :gold_pieces}, :mateys])
assert clone.save
assert_equal 1, clone.treasures.size
assert_equal 1, clone.gold_pieces.size
@@ -61,8 +61,8 @@ def test_multiple_and_deep_include_association_with_array
end
def test_with_belongs_to_relation
- clone = @jack.clone(:include => :parrot)
+ clone = @jack.dup(:include => :parrot)
assert clone.save
assert_not_equal clone.parrot, @jack.parrot
end
-end
+end
View
11 test/test_helper.rb
@@ -16,10 +16,13 @@
load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
-Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
-$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
-class Test::Unit::TestCase #:nodoc:
+class ActiveSupport::TestCase #:nodoc:
+ include ActiveRecord::TestFixtures
+
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
+ $LOAD_PATH.unshift(self.fixture_path)
+
def create_fixtures(*table_names)
if block_given?
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
@@ -35,4 +38,4 @@ def create_fixtures(*table_names)
self.use_instantiated_fixtures = false
# Add more helper methods to be used by all tests here...
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.