deep_cloneable 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -1
- data/VERSION +1 -1
- data/deep_cloneable.gemspec +2 -2
- data/lib/deep_cloneable.rb +14 -10
- data/test/schema.rb +5 -0
- data/test/test_deep_cloneable.rb +14 -0
- data/test/test_helper.rb +6 -3
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -49,7 +49,8 @@ If this is not an option for you, it is also possible to populate the dictionary
|
|
49
49
|
|
50
50
|
dict = { :mateys => {} }
|
51
51
|
pirate.mateys.each{|m| dict[:mateys][m] = m.clone }
|
52
|
-
|
52
|
+
pirate.clone :include => [:mateys, {:treasures => [:matey, :gold_pieces], :dictionary => dict }]
|
53
|
+
|
53
54
|
=== Cloning a model without an attribute
|
54
55
|
pirate.clone :except => :name
|
55
56
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.1
|
data/deep_cloneable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{deep_cloneable}
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Reinier de Lange"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-02-07}
|
13
13
|
s.description = %q{Extends the functionality of ActiveRecord::Base#clone to perform a deep clone that includes user specified associations. }
|
14
14
|
s.email = %q{r.j.delange@nedforce.nl}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/deep_cloneable.rb
CHANGED
@@ -74,16 +74,20 @@ class ActiveRecord::Base
|
|
74
74
|
|
75
75
|
association_reflection = self.class.reflect_on_association(association)
|
76
76
|
cloned_object = case association_reflection.macro
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
77
|
+
when :belongs_to, :has_one
|
78
|
+
self.send(association) && self.send(association).clone(opts)
|
79
|
+
when :has_many, :has_and_belongs_to_many
|
80
|
+
if association_reflection.options[:as]
|
81
|
+
fk = association_reflection.options[:as].to_s + "_id"
|
82
|
+
else
|
83
|
+
fk = association_reflection.options[:foreign_key] || self.class.to_s.underscore + "_id"
|
84
|
+
end
|
85
|
+
self.send(association).collect do |obj|
|
86
|
+
tmp = obj.clone(opts)
|
87
|
+
tmp.send("#{fk}=", kopy)
|
88
|
+
tmp
|
89
|
+
end
|
90
|
+
end
|
87
91
|
kopy.send("#{association}=", cloned_object)
|
88
92
|
end
|
89
93
|
end
|
data/test/schema.rb
CHANGED
@@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
3
3
|
t.column :name, :string
|
4
4
|
t.column :nick_name, :string, :default => 'no nickname'
|
5
5
|
t.column :age, :string
|
6
|
+
t.column :ship_id, :integer
|
6
7
|
end
|
7
8
|
|
8
9
|
create_table :parrots, :force => true do |t|
|
@@ -24,4 +25,8 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
24
25
|
create_table :gold_pieces, :force => true do |t|
|
25
26
|
t.column :treasure_id, :integer
|
26
27
|
end
|
28
|
+
|
29
|
+
create_table :battle_ships, :force => true do |t|
|
30
|
+
t.column :name, :string
|
31
|
+
end
|
27
32
|
end
|
data/test/test_deep_cloneable.rb
CHANGED
@@ -9,6 +9,7 @@ class TestDeepCloneable < Test::Unit::TestCase
|
|
9
9
|
@john = Matey.create(:name => 'John', :pirate => @jack)
|
10
10
|
@treasure = Treasure.create(:found_at => 'Isla del Muerte', :pirate => @jack, :matey => @john)
|
11
11
|
@gold_piece = GoldPiece.create(:treasure => @treasure)
|
12
|
+
@ship = BattleShip.create(:name => 'Black Pearl', :pirates => [@jack])
|
12
13
|
end
|
13
14
|
|
14
15
|
def test_single_clone_exception
|
@@ -33,6 +34,19 @@ class TestDeepCloneable < Test::Unit::TestCase
|
|
33
34
|
assert_equal 1, clone.mateys.size
|
34
35
|
end
|
35
36
|
|
37
|
+
def test_single_include_belongs_to_polymorphic_association
|
38
|
+
clone = @jack.clone(:include => :ship)
|
39
|
+
assert clone.save
|
40
|
+
assert_not_nil clone.ship
|
41
|
+
assert_not_equal @jack.ship, clone.ship
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_single_include_has_many_polymorphic_association
|
45
|
+
clone = @ship.clone(:include => :pirates)
|
46
|
+
assert clone.save
|
47
|
+
assert clone.pirates.any?
|
48
|
+
end
|
49
|
+
|
36
50
|
def test_multiple_include_association
|
37
51
|
clone = @jack.clone(:include => [:mateys, :treasures])
|
38
52
|
assert clone.save
|
data/test/test_helper.rb
CHANGED
@@ -10,11 +10,14 @@ require 'active_record'
|
|
10
10
|
require 'active_record/fixtures'
|
11
11
|
require File.dirname(__FILE__) + '/../init.rb'
|
12
12
|
|
13
|
-
class GoldPiece < ActiveRecord::Base;
|
14
|
-
class Matey < ActiveRecord::Base;
|
15
|
-
class Parrot < ActiveRecord::Base;
|
13
|
+
class GoldPiece < ActiveRecord::Base; belongs_to :treasure end
|
14
|
+
class Matey < ActiveRecord::Base; belongs_to :pirate end
|
15
|
+
class Parrot < ActiveRecord::Base; belongs_to :pirate end
|
16
|
+
class BattleShip < ActiveRecord::Base; has_many :pirates, :as => :ship end
|
16
17
|
|
17
18
|
class Pirate < ActiveRecord::Base
|
19
|
+
belongs_to :ship, :polymorphic => true
|
20
|
+
|
18
21
|
has_many :mateys
|
19
22
|
has_many :treasures
|
20
23
|
has_many :gold_pieces, :through => :treasures
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_cloneable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 1
|
10
|
+
version: 1.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Reinier de Lange
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-07 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|