deep_cloneable 1.2.2 → 1.2.3

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.2
1
+ 1.2.3
@@ -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.2"
8
+ s.version = "1.2.3"
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-21}
12
+ s.date = %q{2011-02-27}
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 = [
@@ -72,14 +72,18 @@ class ActiveRecord::Base
72
72
  opts.merge!(:except => deep_exceptions[association]) if deep_exceptions[association]
73
73
  opts.merge!(:dictionary => dict) if dict
74
74
 
75
- association_reflection = self.class.reflect_on_association(association)
75
+ association_reflection = self.class.reflect_on_association(association)
76
76
  cloned_object = case association_reflection.macro
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
+ reverse_association_name = association_reflection.klass.reflect_on_all_associations.detect do |a|
81
+ a.primary_key_name.to_s == association_reflection.primary_key_name.to_s
82
+ end.try(:name)
83
+
80
84
  self.send(association).collect do |obj|
81
85
  tmp = obj.clone(opts)
82
- tmp.send("#{association_reflection.primary_key_name}=", kopy)
86
+ tmp.send("#{reverse_association_name.to_s}=", kopy) if reverse_association_name
83
87
  tmp
84
88
  end
85
89
  end
data/test/schema.rb CHANGED
@@ -18,7 +18,7 @@ ActiveRecord::Schema.define(:version => 1) do
18
18
 
19
19
  create_table :treasures, :force => true do |t|
20
20
  t.column :found_at, :string
21
- t.column :pirate_id, :integer
21
+ t.column :owner, :integer
22
22
  t.column :matey_id, :integer
23
23
  end
24
24
 
@@ -61,6 +61,14 @@ class TestDeepCloneable < Test::Unit::TestCase
61
61
  assert_equal 1, clone.gold_pieces.size
62
62
  end
63
63
 
64
+ def test_include_association_assignments
65
+ clone = @jack.clone(:include => :treasures)
66
+
67
+ clone.treasures.each do |treasure|
68
+ assert_equal clone, treasure.pirate
69
+ end
70
+ end
71
+
64
72
  def test_multiple_and_deep_include_association
65
73
  clone = @jack.clone(:include => {:treasures => :gold_pieces, :mateys => {}})
66
74
  assert clone.save
@@ -128,4 +136,4 @@ class TestDeepCloneable < Test::Unit::TestCase
128
136
  end
129
137
 
130
138
 
131
- end
139
+ end
data/test/test_helper.rb CHANGED
@@ -27,13 +27,13 @@ class Pirate < ActiveRecord::Base
27
27
  belongs_to :ship, :polymorphic => true
28
28
 
29
29
  has_many :mateys
30
- has_many :treasures
30
+ has_many :treasures, :foreign_key => 'owner'
31
31
  has_many :gold_pieces, :through => :treasures
32
32
  has_one :parrot
33
33
  end
34
34
 
35
35
  class Treasure < ActiveRecord::Base
36
- belongs_to :pirate
36
+ belongs_to :pirate, :foreign_key => :owner
37
37
  belongs_to :matey
38
38
  has_many :gold_pieces
39
39
  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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 2
10
- version: 1.2.2
9
+ - 3
10
+ version: 1.2.3
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-21 00:00:00 +01:00
18
+ date: 2011-02-27 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21