composite_primary_keys 4.0.0.beta1 → 4.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +11 -1
- data/lib/composite_primary_keys/associations/join_dependency.rb +26 -0
- data/lib/composite_primary_keys/version.rb +1 -1
- data/lib/composite_primary_keys.rb +3 -1
- data/test/fixtures/products.yml +4 -1
- data/test/test_associations.rb +10 -5
- data/test/test_habtm.rb +32 -0
- data/test/test_miscellaneous.rb +2 -2
- metadata +8 -7
data/History.txt
CHANGED
@@ -1,7 +1,17 @@
|
|
1
|
+
== 4.0.0.beta2 Not released
|
2
|
+
* ActiveRecord 3.1 RC4 compatibility.
|
3
|
+
* Fix instantiation of CPK models with included associations
|
4
|
+
|
1
5
|
== 4.0.0.beta1 2011-06-09
|
2
|
-
* ActiveRecord 3.1 compatibility. This required a significant rewrite due to
|
6
|
+
* ActiveRecord 3.1 RC1 compatibility. This required a significant rewrite due to
|
3
7
|
all the changes in AR 3.1 versus 3.0.
|
4
8
|
|
9
|
+
== 3.1.10 Not released
|
10
|
+
* Bugfix for belongs_to with includes (John Ash)
|
11
|
+
* Improved tests for calling clear on a habtm association, which involved (David Rueck)
|
12
|
+
* Fixed bug that resulted in unrelated records being deleted when calling (David Rueck)
|
13
|
+
|
14
|
+
|
5
15
|
== 3.1.9 2011-06-04
|
6
16
|
* Improve HABTM association tests (David Rueck)
|
7
17
|
* Remove deprecated Rake tasks (Charlie Savage)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Associations
|
3
|
+
class JoinDependency
|
4
|
+
def instantiate(rows)
|
5
|
+
primary_key = join_base.aliased_primary_key
|
6
|
+
parents = {}
|
7
|
+
|
8
|
+
records = rows.map { |model|
|
9
|
+
# CPK
|
10
|
+
#primary_id = model[primary_key]
|
11
|
+
primary_id = if primary_key.kind_of?(Array)
|
12
|
+
primary_key.map {|key| model[key]}
|
13
|
+
else
|
14
|
+
model[primary_key]
|
15
|
+
end
|
16
|
+
parent = parents[primary_id] ||= join_base.instantiate(model)
|
17
|
+
construct(parent, @associations, join_associations, model)
|
18
|
+
parent
|
19
|
+
}.uniq
|
20
|
+
|
21
|
+
remove_duplicate_results!(active_record, records, @associations)
|
22
|
+
records
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -26,7 +26,7 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
26
26
|
|
27
27
|
unless defined?(ActiveRecord)
|
28
28
|
require 'rubygems'
|
29
|
-
gem 'activerecord', '=3.1.0.
|
29
|
+
gem 'activerecord', '=3.1.0.rc4'
|
30
30
|
require 'active_record'
|
31
31
|
end
|
32
32
|
|
@@ -39,6 +39,7 @@ require 'active_record/associations/association'
|
|
39
39
|
require 'active_record/associations/association_scope'
|
40
40
|
require 'active_record/associations/has_and_belongs_to_many_association'
|
41
41
|
require 'active_record/associations/has_many_association'
|
42
|
+
require 'active_record/associations/join_dependency'
|
42
43
|
require 'active_record/associations/join_dependency/join_part'
|
43
44
|
require 'active_record/associations/join_dependency/join_association'
|
44
45
|
require 'active_record/associations/preloader/association'
|
@@ -72,6 +73,7 @@ require 'composite_primary_keys/associations/association'
|
|
72
73
|
require 'composite_primary_keys/associations/association_scope'
|
73
74
|
require 'composite_primary_keys/associations/has_and_belongs_to_many_association'
|
74
75
|
require 'composite_primary_keys/associations/has_many_association'
|
76
|
+
require 'composite_primary_keys/associations/join_dependency'
|
75
77
|
require 'composite_primary_keys/associations/join_dependency/join_part'
|
76
78
|
require 'composite_primary_keys/associations/join_dependency/join_association'
|
77
79
|
require 'composite_primary_keys/associations/preloader/association'
|
data/test/fixtures/products.yml
CHANGED
data/test/test_associations.rb
CHANGED
@@ -6,7 +6,7 @@ class TestAssociations < ActiveSupport::TestCase
|
|
6
6
|
:departments, :memberships
|
7
7
|
|
8
8
|
def test_count
|
9
|
-
assert_equal(
|
9
|
+
assert_equal(3, Product.count(:include => :product_tariffs))
|
10
10
|
assert_equal(3, Tariff.count(:include => :product_tariffs))
|
11
11
|
|
12
12
|
expected = {Date.today => 2,
|
@@ -45,12 +45,12 @@ class TestAssociations < ActiveSupport::TestCase
|
|
45
45
|
def test_find_includes_products
|
46
46
|
# Old style
|
47
47
|
products = Product.find(:all, :include => :product_tariffs)
|
48
|
-
assert_equal(
|
48
|
+
assert_equal(3, products.length)
|
49
49
|
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
50
50
|
|
51
51
|
# New style
|
52
52
|
products = Product.includes(:product_tariffs)
|
53
|
-
assert_equal(
|
53
|
+
assert_equal(3, products.length)
|
54
54
|
assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
|
55
55
|
end
|
56
56
|
|
@@ -90,15 +90,20 @@ class TestAssociations < ActiveSupport::TestCase
|
|
90
90
|
|
91
91
|
def test_has_many_through
|
92
92
|
products = Product.find(:all, :include => :tariffs)
|
93
|
-
assert_equal(
|
93
|
+
assert_equal(3, products.length)
|
94
94
|
|
95
95
|
tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
|
96
96
|
assert_equal(3, tarrifs_length)
|
97
97
|
end
|
98
98
|
|
99
|
+
def test_new_style_includes_with_conditions
|
100
|
+
product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').first
|
101
|
+
assert_equal(0, product_tariff.tariff.amount)
|
102
|
+
end
|
103
|
+
|
99
104
|
def test_find_product_includes
|
100
105
|
products = Product.find(:all, :include => {:product_tariffs => :tariff})
|
101
|
-
assert_equal(
|
106
|
+
assert_equal(3, products.length)
|
102
107
|
|
103
108
|
product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
|
104
109
|
assert_equal(3, product_tariffs_length)
|
data/test/test_habtm.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'abstract_unit'
|
2
|
+
require 'ruby-debug'
|
2
3
|
|
3
4
|
class TestHabtm < ActiveSupport::TestCase
|
4
5
|
|
@@ -26,6 +27,8 @@ class TestHabtm < ActiveSupport::TestCase
|
|
26
27
|
subway.products << first_product << second_product
|
27
28
|
assert_equal 2, subway.products.size
|
28
29
|
subway.products.clear
|
30
|
+
# reload to force reload of associations
|
31
|
+
subway = restaurants(:subway_one)
|
29
32
|
assert_equal 0, subway.products.size
|
30
33
|
end
|
31
34
|
|
@@ -36,7 +39,36 @@ class TestHabtm < ActiveSupport::TestCase
|
|
36
39
|
product.restaurants << subway_one << subway_two
|
37
40
|
assert_equal 2, product.restaurants.size
|
38
41
|
product.restaurants.clear
|
42
|
+
# reload to force reload of associations
|
43
|
+
product = products(:first_product)
|
39
44
|
assert_equal 0, product.restaurants.size
|
40
45
|
end
|
41
46
|
|
47
|
+
# tests case reported in issue #39 where a bug resulted in
|
48
|
+
# deletion of incorrect join table records because the owner's id
|
49
|
+
# was assumed to be an array and is not in this case
|
50
|
+
# and evaluates to a useable but incorrect value
|
51
|
+
def test_habtm_clear_cpk_association_side_only_deletes_only_correct_records
|
52
|
+
product_one = Product.find(1)
|
53
|
+
product_three = Product.find(3)
|
54
|
+
subway_one = restaurants(:subway_one)
|
55
|
+
subway_two = restaurants(:subway_two)
|
56
|
+
product_one.restaurants << subway_one << subway_two
|
57
|
+
product_three.restaurants << subway_one << subway_two
|
58
|
+
assert_equal 2, product_one.restaurants.size
|
59
|
+
assert_equal 2, product_three.restaurants.size
|
60
|
+
|
61
|
+
# if product_three's id is incorrectly assumed to be
|
62
|
+
# an array it will be evaluated as 3[0], which is 1, which would
|
63
|
+
# delete product_one's associations rather than product_three's
|
64
|
+
product_three.restaurants.clear
|
65
|
+
|
66
|
+
# reload to force reload of associations
|
67
|
+
product_one = Product.find(1)
|
68
|
+
product_three = Product.find(3)
|
69
|
+
|
70
|
+
assert_equal 0, product_three.restaurants.size
|
71
|
+
assert_equal 2, product_one.restaurants.size
|
72
|
+
end
|
73
|
+
|
42
74
|
end
|
data/test/test_miscellaneous.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 62196263
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 4
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- beta
|
11
|
-
-
|
12
|
-
version: 4.0.0.
|
11
|
+
- 2
|
12
|
+
version: 4.0.0.beta2
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Dr Nic Williams
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2011-06-
|
21
|
+
date: 2011-06-21 00:00:00 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
24
|
name: activerecord
|
@@ -28,14 +28,14 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - "="
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
hash:
|
31
|
+
hash: 15424109
|
32
32
|
segments:
|
33
33
|
- 3
|
34
34
|
- 1
|
35
35
|
- 0
|
36
36
|
- rc
|
37
|
-
-
|
38
|
-
version: 3.1.0.
|
37
|
+
- 4
|
38
|
+
version: 3.1.0.rc4
|
39
39
|
type: :runtime
|
40
40
|
version_requirements: *id001
|
41
41
|
description: Composite key support for ActiveRecord 3
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- lib/composite_primary_keys/associations/has_many_association.rb
|
62
62
|
- lib/composite_primary_keys/associations/join_dependency/join_association.rb
|
63
63
|
- lib/composite_primary_keys/associations/join_dependency/join_part.rb
|
64
|
+
- lib/composite_primary_keys/associations/join_dependency.rb
|
64
65
|
- lib/composite_primary_keys/associations/preloader/association.rb
|
65
66
|
- lib/composite_primary_keys/associations/preloader/belongs_to.rb
|
66
67
|
- lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb
|