composite_primary_keys 3.1.9 → 3.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ == 3.1.10 2011-07-08
2
+ * Bugfix for belongs_to with includes (John Ash)
3
+ * Improved tests for calling clear on a habtm association, which involved (David Rueck)
4
+ * Fixed bug that resulted in unrelated records being deleted when calling (David Rueck)
5
+ * Output deprecation warnings about extra columns in join table CPK-aware (David Rueck)
6
+
1
7
  == 3.1.9 2011-06-04
2
8
  * Improve HABTM association tests (David Rueck)
3
9
  * Remove deprecated Rake tasks (Charlie Savage)
@@ -128,7 +128,8 @@ module ActiveRecord
128
128
  parent_table, reflection.options[:primary_key] || parent.primary_key)
129
129
  end
130
130
  when :belongs_to
131
- [aliased_table[options[:primary_key] || reflection.klass.primary_key].eq(parent_table[options[:foreign_key] || reflection.primary_key_name])]
131
+ #[aliased_table[options[:primary_key] || reflection.klass.primary_key].eq(parent_table[options[:foreign_key] || reflection.primary_key_name])]
132
+ composite_join_predicates(aliased_table, options[:primary_key] || reflection.klass.primary_key, parent_table, options[:foreign_key] || reflection.primary_key_name)
132
133
  end
133
134
 
134
135
  unless klass.descends_from_active_record?
@@ -108,7 +108,7 @@ module ActiveRecord
108
108
  else
109
109
  relation = Arel::Table.new(@reflection.options[:join_table])
110
110
 
111
- if @reflection.cpk_primary_key
111
+ if @reflection.cpk_primary_key.size > 1
112
112
  owner_conditions = []
113
113
  @reflection.cpk_primary_key.each_with_index do |column,i|
114
114
  owner_conditions << relation[column.to_sym].eq(@owner.id[i])
@@ -50,7 +50,7 @@ module CompositePrimaryKeys
50
50
  case id
51
51
  # CPK
52
52
  when CompositePrimaryKeys::CompositeKeys
53
- relation = select(primary_keys).limit(1)
53
+ relation = select("1").limit(1)
54
54
  relation = relation.where_cpk_id(id) if id
55
55
  relation.first ? true : false
56
56
  when Array
@@ -66,7 +66,7 @@ module CompositePrimaryKeys
66
66
  # CPK
67
67
  #relation = select(primary_key).limit(1)
68
68
  #relation = relation.where(primary_key.eq(id)) if id
69
- relation = select(primary_keys).limit(1)
69
+ relation = select("1").limit(1)
70
70
  relation = relation.where_cpk_id(id) if id
71
71
  relation.first ? true : false
72
72
  end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 1
5
- TINY = 9
5
+ TINY = 10
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,7 +1,9 @@
1
1
  first_product:
2
2
  id: 1
3
3
  name: Product One
4
-
5
4
  second_product:
6
5
  id: 2
7
- name: Product Two
6
+ name: Product Two
7
+ third_product:
8
+ id: 3
9
+ name: Product Three
@@ -6,4 +6,7 @@ second:
6
6
  city_id: 2
7
7
  suburb_id: 1
8
8
  name: Second Suburb
9
-
9
+ no_mcdonalds:
10
+ city_id: 1
11
+ suburb_id: 2
12
+ name: Third Suburb
@@ -6,7 +6,7 @@ class TestAssociations < ActiveSupport::TestCase
6
6
  :departments, :memberships
7
7
 
8
8
  def test_count
9
- assert_equal(2, Product.count(:include => :product_tariffs))
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,
@@ -40,14 +40,14 @@ class TestAssociations < ActiveSupport::TestCase
40
40
  def test_find_includes_products
41
41
  # Old style
42
42
  assert @products = Product.find(:all, :include => :product_tariffs)
43
- assert_equal 2, @products.length
43
+ assert_equal 3, @products.length
44
44
  assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
45
45
  assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
46
46
  "Incorrect number of product_tariffs returned"
47
47
 
48
48
  # New style
49
49
  assert @products = Product.includes(:product_tariffs)
50
- assert_equal 2, @products.length
50
+ assert_equal 3, @products.length
51
51
  assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
52
52
  assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
53
53
  "Incorrect number of product_tariffs returned"
@@ -93,6 +93,11 @@ class TestAssociations < ActiveSupport::TestCase
93
93
  assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
94
94
  end
95
95
 
96
+ def test_new_style_includes_with_conditions
97
+ product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').first
98
+ assert_equal(0, product_tariff.tariff.amount)
99
+ end
100
+
96
101
  def test_find_includes_extended
97
102
  assert @products = Product.find(:all, :include => {:product_tariffs => :tariff})
98
103
  assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@product_tariffs').length},
@@ -1,4 +1,5 @@
1
1
  require 'abstract_unit'
2
+ require 'ruby-debug'
2
3
 
3
4
  class TestHabtm < ActiveSupport::TestCase
4
5
 
@@ -12,6 +13,48 @@ class TestHabtm < ActiveSupport::TestCase
12
13
  assert_equal 2, @restaurant.suburbs.size
13
14
  end
14
15
 
16
+ def test_include_cpk_both_sides
17
+ # assuming the association was set up in the fixtures
18
+ # file restaurants_suburbs.yml
19
+ mcdonalds = restaurants(:mcdonalds)
20
+ # check positive
21
+ suburb = mcdonalds.suburbs[0]
22
+ assert mcdonalds.suburbs.include?(suburb)
23
+ # check negative
24
+ suburb_with_no_mcdonalds = suburbs(:no_mcdonalds)
25
+ assert !mcdonalds.suburbs.include?(suburb_with_no_mcdonalds)
26
+ end
27
+
28
+ def test_include_cpk_owner_side_only
29
+ subway = restaurants(:subway_one)
30
+ product = products(:first_product)
31
+ subway.products << product
32
+
33
+ # reload
34
+ # test positive
35
+ subway = restaurants(:subway_one)
36
+ assert subway.products.include?(product)
37
+
38
+ # test negative
39
+ product_two = products(:second_product)
40
+ assert !subway.products.include?(product_two)
41
+ end
42
+
43
+ def test_include_cpk_association_side_only
44
+ product = products(:first_product)
45
+ subway = restaurants(:subway_one)
46
+ product.restaurants << subway
47
+
48
+ # reload
49
+ # test positive
50
+ product = products(:first_product)
51
+ assert product.restaurants.include?(subway)
52
+
53
+ # test negative
54
+ mcdonalds = restaurants(:mcdonalds)
55
+ assert !product.restaurants.include?(mcdonalds)
56
+ end
57
+
15
58
  def test_habtm_clear_cpk_both_sides
16
59
  @restaurant = restaurants(:mcdonalds)
17
60
  assert_equal 2, @restaurant.suburbs.size
@@ -26,6 +69,8 @@ class TestHabtm < ActiveSupport::TestCase
26
69
  subway.products << first_product << second_product
27
70
  assert_equal 2, subway.products.size
28
71
  subway.products.clear
72
+ # reload to force reload of associations
73
+ subway = restaurants(:subway_one)
29
74
  assert_equal 0, subway.products.size
30
75
  end
31
76
 
@@ -36,7 +81,36 @@ class TestHabtm < ActiveSupport::TestCase
36
81
  product.restaurants << subway_one << subway_two
37
82
  assert_equal 2, product.restaurants.size
38
83
  product.restaurants.clear
84
+ # reload to force reload of associations
85
+ product = products(:first_product)
39
86
  assert_equal 0, product.restaurants.size
40
87
  end
41
88
 
89
+ # tests case reported in issue #39 where a bug resulted in
90
+ # deletion of incorrect join table records because the owner's id
91
+ # was assumed to be an array and is not in this case
92
+ # and evaluates to a useable but incorrect value
93
+ def test_habtm_clear_cpk_association_side_only_deletes_only_correct_records
94
+ product_one = Product.find(1)
95
+ product_three = Product.find(3)
96
+ subway_one = restaurants(:subway_one)
97
+ subway_two = restaurants(:subway_two)
98
+ product_one.restaurants << subway_one << subway_two
99
+ product_three.restaurants << subway_one << subway_two
100
+ assert_equal 2, product_one.restaurants.size
101
+ assert_equal 2, product_three.restaurants.size
102
+
103
+ # if product_three's id is incorrectly assumed to be
104
+ # an array it will be evaluated as 3[0], which is 1, which would
105
+ # delete product_one's associations rather than product_three's
106
+ product_three.restaurants.clear
107
+
108
+ # reload to force reload of associations
109
+ product_one = Product.find(1)
110
+ product_three = Product.find(3)
111
+
112
+ assert_equal 0, product_three.restaurants.size
113
+ assert_equal 2, product_one.restaurants.size
114
+ end
115
+
42
116
  end
@@ -31,7 +31,7 @@ class TestMiscellaneous < ActiveSupport::TestCase
31
31
  end
32
32
 
33
33
  def test_count
34
- assert_equal 2, Product.count
34
+ assert_equal 3, Product.count
35
35
  end
36
36
 
37
- end
37
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 9
10
- version: 3.1.9
9
+ - 10
10
+ version: 3.1.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dr Nic Williams
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-06-04 00:00:00 Z
19
+ date: 2011-07-08 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: activerecord