Take the 2008 Git User's Survey and help out! [ hide ]

public
Description: DeepCloning plugin
Homepage: http://blog.defv.be/2008/3/27/activerecord-deepclone-plugin
Clone URL: git://github.com/DefV/deep_cloning.git
Search Repo:
Cloning really deep with multiple associations

Made it easy to do deep cloning with multiple associations. So you could 
do:
  pirate.clone :include => [:mateys, {:treasures => :gold_pieces}]

thanks to Mark Donnelly for this patch
DefV (author)
Thu Apr 17 03:03:06 -0700 2008
commit  247fdd274687583e174ac5bc8c8232d3c2ad9bf6
tree    ed647911ee4b0fbcc236398dbb37b8ac6816bf9e
parent  a1efe599d8334948339e38c5a260a3c637a2c04a
...
23
24
25
 
 
26
...
23
24
25
26
27
28
0
@@ -23,4 +23,6 @@ Install with:
0
 === Cloning really deep
0
    pirate.clone :include => {:treasures => :gold_pieces}
0
 
0
+=== Cloning really deep with multiple associations
0
+ pirate.clone :include => [:mateys, {:treasures => :gold_pieces}]
0
 Copyright (c) 2008 Jan De Poorter, released under the MIT license
...
24
25
26
 
 
 
27
28
29
...
36
37
38
 
 
 
 
39
40
41
...
24
25
26
27
28
29
30
31
32
...
39
40
41
42
43
44
45
46
47
48
0
@@ -24,6 +24,9 @@ module DeepCloning
0
   #
0
   # ==== Cloning really deep
0
   # pirate.clone :include => {:treasures => :gold_pieces}
0
+ #
0
+ # ==== Cloning really deep with multiple associations
0
+ # pirate.clone :include => [:mateys, {:treasures => :gold_pieces}]
0
   #
0
   def clone_with_deep_cloning options = {}
0
     kopy = clone_without_deep_cloning
0
@@ -36,6 +39,10 @@ module DeepCloning
0
     
0
     if options[:include]
0
       Array(options[:include]).each do |association, deep_associations|
0
+ if (association.kind_of? Hash)
0
+ deep_associations = association[association.keys.first]
0
+ association = association.keys.first
0
+ end
0
         opts = deep_associations.blank? ? {} : {:include => deep_associations}
0
         kopy.send("#{association}=", self.send(association).collect {|i| i.clone(opts) })
0
       end
...
50
51
52
 
 
 
 
 
 
 
 
53
54
...
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -50,4 +50,12 @@ class DeepCloningTest < Test::Unit::TestCase
0
     assert_equal 1, clone.gold_pieces.size
0
     assert_equal 1, clone.mateys.size
0
   end
0
+
0
+ def test_multiple_and_deep_include_association_with_array
0
+ clone = @jack.clone(:include => [{:treasures => :gold_pieces}, :mateys])
0
+ assert clone.save
0
+ assert_equal 1, clone.treasures.size
0
+ assert_equal 1, clone.gold_pieces.size
0
+ assert_equal 1, clone.mateys.size
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.