deep_cloneable 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.2.2
@@ -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.1"
8
+ s.version = "1.2.2"
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{2011-02-07}
12
+ s.date = %q{2011-02-21}
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/init.rb CHANGED
@@ -1 +1 @@
1
- require 'deep_cloneable'
1
+ require 'deep_cloneable'
@@ -77,14 +77,9 @@ class ActiveRecord::Base
77
77
  when :belongs_to, :has_one
78
78
  self.send(association) && self.send(association).clone(opts)
79
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
80
  self.send(association).collect do |obj|
86
81
  tmp = obj.clone(opts)
87
- tmp.send("#{fk}=", kopy)
82
+ tmp.send("#{association_reflection.primary_key_name}=", kopy)
88
83
  tmp
89
84
  end
90
85
  end
data/test/schema.rb CHANGED
@@ -29,4 +29,13 @@ ActiveRecord::Schema.define(:version => 1) do
29
29
  create_table :battle_ships, :force => true do |t|
30
30
  t.column :name, :string
31
31
  end
32
+
33
+ create_table :pigs, :force => true do |t|
34
+ t.column :name, :string
35
+ t.column :human_id, :integer
36
+ end
37
+
38
+ create_table :humen, :force => true do |t|
39
+ t.column :name, :string
40
+ end
32
41
  end
@@ -19,7 +19,7 @@ class TestDeepCloneable < Test::Unit::TestCase
19
19
  assert_nil clone.name
20
20
  assert_equal @jack.nick_name, clone.nick_name
21
21
  end
22
-
22
+
23
23
  def test_multiple_clone_exception
24
24
  clone = @jack.clone(:except => [:name, :nick_name])
25
25
  assert clone.save
@@ -27,40 +27,40 @@ class TestDeepCloneable < Test::Unit::TestCase
27
27
  assert_equal 'no nickname', clone.nick_name
28
28
  assert_equal @jack.age, clone.age
29
29
  end
30
-
30
+
31
31
  def test_single_include_association
32
32
  clone = @jack.clone(:include => :mateys)
33
33
  assert clone.save
34
34
  assert_equal 1, clone.mateys.size
35
35
  end
36
-
36
+
37
37
  def test_single_include_belongs_to_polymorphic_association
38
38
  clone = @jack.clone(:include => :ship)
39
39
  assert clone.save
40
40
  assert_not_nil clone.ship
41
41
  assert_not_equal @jack.ship, clone.ship
42
42
  end
43
-
44
- def test_single_include_has_many_polymorphic_association
43
+
44
+ def test_single_include_has_many_polymorphic_association
45
45
  clone = @ship.clone(:include => :pirates)
46
46
  assert clone.save
47
- assert clone.pirates.any?
48
- end
49
-
47
+ assert clone.pirates.any?
48
+ end
49
+
50
50
  def test_multiple_include_association
51
51
  clone = @jack.clone(:include => [:mateys, :treasures])
52
52
  assert clone.save
53
53
  assert_equal 1, clone.mateys.size
54
54
  assert_equal 1, clone.treasures.size
55
55
  end
56
-
56
+
57
57
  def test_deep_include_association
58
58
  clone = @jack.clone(:include => {:treasures => :gold_pieces})
59
59
  assert clone.save
60
60
  assert_equal 1, clone.treasures.size
61
61
  assert_equal 1, clone.gold_pieces.size
62
62
  end
63
-
63
+
64
64
  def test_multiple_and_deep_include_association
65
65
  clone = @jack.clone(:include => {:treasures => :gold_pieces, :mateys => {}})
66
66
  assert clone.save
@@ -68,7 +68,7 @@ class TestDeepCloneable < Test::Unit::TestCase
68
68
  assert_equal 1, clone.gold_pieces.size
69
69
  assert_equal 1, clone.mateys.size
70
70
  end
71
-
71
+
72
72
  def test_multiple_and_deep_include_association_with_array
73
73
  clone = @jack.clone(:include => [{:treasures => :gold_pieces}, :mateys])
74
74
  assert clone.save
@@ -76,21 +76,21 @@ class TestDeepCloneable < Test::Unit::TestCase
76
76
  assert_equal 1, clone.gold_pieces.size
77
77
  assert_equal 1, clone.mateys.size
78
78
  end
79
-
79
+
80
80
  def test_with_belongs_to_relation
81
81
  clone = @jack.clone(:include => :parrot)
82
82
  assert clone.save
83
83
  assert_not_equal clone.parrot, @jack.parrot
84
84
  end
85
-
85
+
86
86
  def test_should_pass_nested_exceptions
87
87
  clone = @jack.clone(:include => :parrot, :except => [:name, { :parrot => [:name] }])
88
88
  assert clone.save
89
89
  assert_not_equal clone.parrot, @jack.parrot
90
90
  assert_not_nil @jack.parrot.name
91
- assert_nil clone.parrot.name
91
+ assert_nil clone.parrot.name
92
92
  end
93
-
93
+
94
94
  def test_should_not_double_clone_when_using_dictionary
95
95
  current_matey_count = Matey.count
96
96
  clone = @jack.clone(:include => [:mateys, { :treasures => :matey }], :use_dictionary => true)
@@ -98,16 +98,34 @@ class TestDeepCloneable < Test::Unit::TestCase
98
98
 
99
99
  assert_equal current_matey_count + 1, Matey.count
100
100
  end
101
-
101
+
102
102
  def test_should_not_double_clone_when_using_manual_dictionary
103
103
  current_matey_count = Matey.count
104
-
104
+
105
105
  dict = { :mateys => {} }
106
106
  @jack.mateys.each{|m| dict[:mateys][m] = m.clone }
107
-
107
+
108
108
  clone = @jack.clone(:include => [:mateys, { :treasures => :matey }], :dictionary => dict)
109
109
  clone.save!
110
110
 
111
111
  assert_equal current_matey_count + 1, Matey.count
112
- end
112
+ end
113
+
114
+ def test_should_support_ar_class_under_module
115
+ @human = Animal::Human.create :name => "Michael"
116
+ @pig = Animal::Pig.create :human => @human, :name => 'big pig'
117
+
118
+ clone_human = @human.clone :include => [:pigs]
119
+ assert clone_human.save
120
+ assert_equal 1, clone_human.pigs.count
121
+
122
+ @human2 = Animal::Human.create :name => "John"
123
+ @pig2 = @human2.pigs.create :name => 'small pig'
124
+
125
+ clone_human_2 = @human.clone :include => [:pigs]
126
+ assert clone_human_2.save
127
+ assert_equal 1, clone_human_2.pigs.count
128
+ end
129
+
130
+
113
131
  end
data/test/test_helper.rb CHANGED
@@ -7,9 +7,17 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
 
8
8
  Gem.activate 'activerecord'
9
9
  require 'active_record'
10
- require 'active_record/fixtures'
11
10
  require File.dirname(__FILE__) + '/../init.rb'
12
11
 
12
+ module Animal
13
+ class Human < ActiveRecord::Base
14
+ has_many :pigs
15
+ end
16
+ class Pig < ActiveRecord::Base
17
+ belongs_to :human
18
+ end
19
+ end
20
+
13
21
  class GoldPiece < ActiveRecord::Base; belongs_to :treasure end
14
22
  class Matey < ActiveRecord::Base; belongs_to :pirate end
15
23
  class Parrot < ActiveRecord::Base; belongs_to :pirate end
@@ -17,7 +25,7 @@ class BattleShip < ActiveRecord::Base; has_many :pirates, :as => :ship end
17
25
 
18
26
  class Pirate < ActiveRecord::Base
19
27
  belongs_to :ship, :polymorphic => true
20
-
28
+
21
29
  has_many :mateys
22
30
  has_many :treasures
23
31
  has_many :gold_pieces, :through => :treasures
@@ -25,7 +33,7 @@ class Pirate < ActiveRecord::Base
25
33
  end
26
34
 
27
35
  class Treasure < ActiveRecord::Base
28
- belongs_to :pirate
36
+ belongs_to :pirate
29
37
  belongs_to :matey
30
38
  has_many :gold_pieces
31
39
  end
@@ -52,4 +60,4 @@ def load_schema
52
60
  end
53
61
  ActiveRecord::Base.establish_connection(config[db_adapter])
54
62
  load(File.dirname(__FILE__) + "/schema.rb")
55
- end
63
+ end
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 1
10
- version: 1.2.1
9
+ - 2
10
+ version: 1.2.2
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: 2011-02-07 00:00:00 +01:00
18
+ date: 2011-02-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21