composite_primary_keys 3.1.8 → 3.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/History.txt +16 -12
  2. data/Rakefile +2 -4
  3. data/lib/composite_primary_keys/version.rb +1 -1
  4. data/test/connections/native_mysql/connection.rb +1 -1
  5. data/test/connections/native_oracle/connection.rb +1 -1
  6. data/test/connections/native_oracle_enhanced/connection.rb +1 -1
  7. data/test/connections/native_postgresql/connection.rb +1 -1
  8. data/test/connections/native_sqlite/connection.rb +1 -1
  9. data/test/fixtures/article.rb +1 -1
  10. data/test/fixtures/comment.rb +0 -1
  11. data/test/fixtures/db_definitions/db2-create-tables.sql +4 -13
  12. data/test/fixtures/db_definitions/db2-drop-tables.sql +1 -1
  13. data/test/fixtures/db_definitions/mysql.sql +5 -13
  14. data/test/fixtures/db_definitions/oracle.drop.sql +1 -2
  15. data/test/fixtures/db_definitions/oracle.sql +4 -13
  16. data/test/fixtures/db_definitions/postgresql.sql +5 -13
  17. data/test/fixtures/db_definitions/sqlite.sql +4 -13
  18. data/test/fixtures/department.rb +1 -0
  19. data/test/fixtures/departments.yml +5 -1
  20. data/test/fixtures/employees.yml +12 -2
  21. data/test/fixtures/membership.rb +0 -2
  22. data/test/fixtures/product.rb +4 -2
  23. data/test/fixtures/restaurant.rb +4 -1
  24. data/test/fixtures/restaurants.yml +8 -1
  25. data/test/fixtures/tariff.rb +0 -1
  26. data/test/fixtures/tariffs.yml +2 -0
  27. data/test/test_associations.rb +6 -23
  28. data/test/test_attribute_methods.rb +5 -6
  29. data/test/test_delete.rb +35 -2
  30. data/test/test_habtm.rb +42 -0
  31. data/test/test_ids.rb +0 -8
  32. data/test/test_suite.rb +1 -0
  33. metadata +6 -23
  34. data/lib/composite_primary_keys/associations/association.rb +0 -23
  35. data/lib/composite_primary_keys/associations/association_scope.rb +0 -67
  36. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +0 -22
  37. data/lib/composite_primary_keys/associations/join_dependency/join_part.rb +0 -39
  38. data/lib/composite_primary_keys/associations/preloader/association.rb +0 -61
  39. data/lib/composite_primary_keys/associations/preloader/belongs_to.rb +0 -13
  40. data/lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb +0 -46
  41. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +0 -19
  42. data/lib/composite_primary_keys/attribute_methods/read.rb +0 -88
  43. data/lib/composite_primary_keys/attribute_methods/write.rb +0 -40
  44. data/lib/composite_primary_keys/composite_predicates.rb +0 -44
  45. data/lib/composite_primary_keys/named_scope.rb +0 -29
  46. data/lib/composite_primary_keys/relation/finder_methods.rb +0 -119
  47. data/lib/composite_primary_keys/relation/query_methods.rb +0 -24
  48. data/test/fixtures/article_group.rb +0 -4
  49. data/test/fixtures/article_groups.yml +0 -7
  50. data/test/fixtures/kitchen_sink.rb +0 -3
  51. data/test/fixtures/kitchen_sinks.yml +0 -5
  52. data/test/fixtures/way_node.rb +0 -3
@@ -1,7 +1,7 @@
1
1
  require 'abstract_unit'
2
2
 
3
3
  class TestAttributeMethods < ActiveSupport::TestCase
4
- fixtures :kitchen_sinks, :reference_types
4
+ fixtures :reference_types
5
5
 
6
6
  def test_read_attribute_with_single_key
7
7
  rt = ReferenceType.find(1)
@@ -11,10 +11,9 @@ class TestAttributeMethods < ActiveSupport::TestCase
11
11
  end
12
12
 
13
13
  def test_read_attribute_with_composite_keys
14
- sink = KitchenSink.find(1,2)
15
- assert_equal(1, sink.id_1)
16
- assert_equal(2, sink.id_2)
17
- assert_equal(Date.today, sink.a_date.to_date)
18
- assert_equal('string', sink.a_string)
14
+ ref_code = ReferenceCode.find(1,1)
15
+ assert_equal(1, ref_code.id.first)
16
+ assert_equal(1, ref_code.id.last)
17
+ assert_equal('Mr', ref_code.abbreviation)
19
18
  end
20
19
  end
@@ -1,7 +1,8 @@
1
1
  require 'abstract_unit'
2
2
 
3
3
  class TestDelete < ActiveSupport::TestCase
4
- fixtures :reference_types, :reference_codes, :departments, :employees
4
+ fixtures :departments, :employees, :products, :product_tariffs,
5
+ :reference_types, :reference_codes
5
6
 
6
7
  CLASSES = {
7
8
  :single => {
@@ -68,12 +69,44 @@ class TestDelete < ActiveSupport::TestCase
68
69
  assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
69
70
  end
70
71
 
71
- def test_delete_cpk_all_association
72
+ def test_destroy_has_one
73
+ # In this case the association is a has_one with
74
+ # dependent set to :destroy
75
+ department = departments(:engineering)
76
+ assert_not_nil(department.head)
77
+
78
+ # Get head employee id
79
+ head_id = department.head.id
80
+
81
+ # Delete department - should delete the head
82
+ department.destroy
83
+
84
+ # Verify the head is also
85
+ assert_raise(ActiveRecord::RecordNotFound) do
86
+ Employee.find(head_id)
87
+ end
88
+ end
89
+
90
+ def test_destroy_has_many_delete_all
72
91
  # In this case the association is a has_many composite key with
73
92
  # dependent set to :delete_all
74
93
  product = Product.find(1)
75
94
  assert_equal(2, product.product_tariffs.length)
76
95
 
96
+ # Get product_tariff length
97
+ product_tariff_size = ProductTariff.count
98
+
99
+ # Delete product - should delete 2 product tariffs
100
+ product.destroy
101
+
102
+ # Verify product_tariff are deleted
103
+ assert_equal(product_tariff_size - 2, ProductTariff.count)
104
+ end
105
+
106
+ def test_delete_cpk_association
107
+ product = Product.find(1)
108
+ assert_equal(2, product.product_tariffs.length)
109
+
77
110
  product_tariff = product.product_tariffs.first
78
111
  product.product_tariffs.delete(product_tariff)
79
112
 
@@ -0,0 +1,42 @@
1
+ require 'abstract_unit'
2
+
3
+ class TestHabtm < ActiveSupport::TestCase
4
+
5
+ fixtures :suburbs, :restaurants, :restaurants_suburbs, :products
6
+
7
+ def test_has_and_belongs_to_many
8
+ @restaurant = Restaurant.find([1,1])
9
+ assert_equal 2, @restaurant.suburbs.size
10
+
11
+ @restaurant = Restaurant.find([1,1], :include => :suburbs)
12
+ assert_equal 2, @restaurant.suburbs.size
13
+ end
14
+
15
+ def test_habtm_clear_cpk_both_sides
16
+ @restaurant = restaurants(:mcdonalds)
17
+ assert_equal 2, @restaurant.suburbs.size
18
+ @restaurant.suburbs.clear
19
+ assert_equal 0, @restaurant.suburbs.size
20
+ end
21
+
22
+ def test_habtm_clear_cpk_owner_side_only
23
+ subway = restaurants(:subway_one)
24
+ first_product = products(:first_product)
25
+ second_product = products(:second_product)
26
+ subway.products << first_product << second_product
27
+ assert_equal 2, subway.products.size
28
+ subway.products.clear
29
+ assert_equal 0, subway.products.size
30
+ end
31
+
32
+ def test_habtm_clear_cpk_association_side_only
33
+ product = products(:first_product)
34
+ subway_one = restaurants(:subway_one)
35
+ subway_two = restaurants(:subway_two)
36
+ product.restaurants << subway_one << subway_two
37
+ assert_equal 2, product.restaurants.size
38
+ product.restaurants.clear
39
+ assert_equal 0, product.restaurants.size
40
+ end
41
+
42
+ end
@@ -87,12 +87,4 @@ class TestIds < ActiveSupport::TestCase
87
87
  ref_code.id = [2,1]
88
88
  assert_equal([2,1], ref_code.id)
89
89
  end
90
-
91
- def test_id_as_component
92
- way_node = WayNode.new
93
- assert_equal([nil, nil], way_node.id)
94
-
95
- way_node.id = [2,1]
96
- assert_equal([2,1], way_node.id)
97
- end
98
90
  end
@@ -10,6 +10,7 @@ require 'test_delete'
10
10
  require 'test_equal'
11
11
  require 'test_exists'
12
12
  require 'test_find'
13
+ require 'test_habtm'
13
14
  require 'test_ids'
14
15
  require 'test_miscellaneous'
15
16
  require 'test_pagination'
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 8
10
- version: 3.1.8
9
+ - 9
10
+ version: 3.1.9
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-05-26 00:00:00 Z
19
+ date: 2011-06-04 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: activerecord
@@ -73,28 +73,17 @@ files:
73
73
  - init.rb
74
74
  - install.rb
75
75
  - loader.rb
76
- - lib/composite_primary_keys/associations/association.rb
77
76
  - lib/composite_primary_keys/associations/association_proxy.rb
78
- - lib/composite_primary_keys/associations/association_scope.rb
79
77
  - lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
80
78
  - lib/composite_primary_keys/associations/has_many_association.rb
81
79
  - lib/composite_primary_keys/associations/has_one_association.rb
82
- - lib/composite_primary_keys/associations/join_dependency/join_association.rb
83
- - lib/composite_primary_keys/associations/join_dependency/join_part.rb
84
- - lib/composite_primary_keys/associations/preloader/association.rb
85
- - lib/composite_primary_keys/associations/preloader/belongs_to.rb
86
- - lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb
87
80
  - lib/composite_primary_keys/associations/through_association_scope.rb
88
81
  - lib/composite_primary_keys/associations.rb
89
82
  - lib/composite_primary_keys/association_preload.rb
90
- - lib/composite_primary_keys/attribute_methods/primary_key.rb
91
- - lib/composite_primary_keys/attribute_methods/read.rb
92
- - lib/composite_primary_keys/attribute_methods/write.rb
93
83
  - lib/composite_primary_keys/attribute_methods.rb
94
84
  - lib/composite_primary_keys/base.rb
95
85
  - lib/composite_primary_keys/calculations.rb
96
86
  - lib/composite_primary_keys/composite_arrays.rb
97
- - lib/composite_primary_keys/composite_predicates.rb
98
87
  - lib/composite_primary_keys/connection_adapters/abstract_adapter.rb
99
88
  - lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb
100
89
  - lib/composite_primary_keys/connection_adapters/oracle_adapter.rb
@@ -103,14 +92,11 @@ files:
103
92
  - lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb
104
93
  - lib/composite_primary_keys/finder_methods.rb
105
94
  - lib/composite_primary_keys/fixtures.rb
106
- - lib/composite_primary_keys/named_scope.rb
107
95
  - lib/composite_primary_keys/persistence.rb
108
96
  - lib/composite_primary_keys/primary_key.rb
109
97
  - lib/composite_primary_keys/query_methods.rb
110
98
  - lib/composite_primary_keys/read.rb
111
99
  - lib/composite_primary_keys/reflection.rb
112
- - lib/composite_primary_keys/relation/finder_methods.rb
113
- - lib/composite_primary_keys/relation/query_methods.rb
114
100
  - lib/composite_primary_keys/relation.rb
115
101
  - lib/composite_primary_keys/validations/uniqueness.rb
116
102
  - lib/composite_primary_keys/version.rb
@@ -138,8 +124,6 @@ files:
138
124
  - test/debug.log
139
125
  - test/fixtures/article.rb
140
126
  - test/fixtures/articles.yml
141
- - test/fixtures/article_group.rb
142
- - test/fixtures/article_groups.yml
143
127
  - test/fixtures/capitol.rb
144
128
  - test/fixtures/capitols.yml
145
129
  - test/fixtures/comment.rb
@@ -161,8 +145,6 @@ files:
161
145
  - test/fixtures/groups.yml
162
146
  - test/fixtures/hack.rb
163
147
  - test/fixtures/hacks.yml
164
- - test/fixtures/kitchen_sink.rb
165
- - test/fixtures/kitchen_sinks.yml
166
148
  - test/fixtures/membership.rb
167
149
  - test/fixtures/memberships.yml
168
150
  - test/fixtures/membership_status.rb
@@ -200,7 +182,6 @@ files:
200
182
  - test/fixtures/tariffs.yml
201
183
  - test/fixtures/user.rb
202
184
  - test/fixtures/users.yml
203
- - test/fixtures/way_node.rb
204
185
  - test/hash_tricks.rb
205
186
  - test/plugins/pagination.rb
206
187
  - test/plugins/pagination_helper.rb
@@ -216,6 +197,7 @@ files:
216
197
  - test/test_equal.rb
217
198
  - test/test_exists.rb
218
199
  - test/test_find.rb
200
+ - test/test_habtm.rb
219
201
  - test/test_ids.rb
220
202
  - test/test_miscellaneous.rb
221
203
  - test/test_pagination.rb
@@ -277,6 +259,7 @@ test_files:
277
259
  - test/test_equal.rb
278
260
  - test/test_exists.rb
279
261
  - test/test_find.rb
262
+ - test/test_habtm.rb
280
263
  - test/test_ids.rb
281
264
  - test/test_miscellaneous.rb
282
265
  - test/test_pagination.rb
@@ -1,23 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class Association
4
- def creation_attributes
5
- attributes = {}
6
-
7
- if reflection.macro.in?([:has_one, :has_many]) && !options[:through]
8
- # CPK
9
- # attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
10
- Array(reflection.foreign_key).zip(Array(reflection.active_record_primary_key)).each do |key1, key2|
11
- attributes[key1] = owner[key2]
12
- end
13
-
14
- if reflection.options[:as]
15
- attributes[reflection.type] = owner.class.base_class.name
16
- end
17
- end
18
-
19
- attributes
20
- end
21
- end
22
- end
23
- end
@@ -1,67 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class AssociationScope
4
- def add_constraints(scope)
5
- tables = construct_tables
6
-
7
- chain.each_with_index do |reflection, i|
8
- table, foreign_table = tables.shift, tables.first
9
-
10
- if reflection.source_macro == :has_and_belongs_to_many
11
- join_table = tables.shift
12
-
13
- # CPK
14
- # scope = scope.joins(join(
15
- # join_table,
16
- # table[reflection.active_record_primary_key].
17
- # eq(join_table[reflection.association_foreign_key])
18
- #))
19
- predicate = cpk_join_predicate(table, reflection.association_primary_key,
20
- join_table, reflection.association_foreign_key)
21
- scope = scope.joins(join(join_table, predicate))
22
-
23
- table, foreign_table = join_table, tables.first
24
- end
25
-
26
- if reflection.source_macro == :belongs_to
27
- key = reflection.association_primary_key
28
- foreign_key = reflection.foreign_key
29
- else
30
- key = reflection.foreign_key
31
- foreign_key = reflection.active_record_primary_key
32
- end
33
-
34
- if reflection == chain.last
35
- # CPK
36
- # scope = scope.where(table[key].eq(owner[foreign_key]))
37
- predicate = cpk_join_predicate(table, key, owner, foreign_key)
38
- scope = scope.where(predicate)
39
-
40
- conditions[i].each do |condition|
41
- if options[:through] && condition.is_a?(Hash)
42
- condition = { table.name => condition }
43
- end
44
-
45
- scope = scope.where(interpolate(condition))
46
- end
47
- else
48
- # CPK
49
- # constraint = table[key].eq(foreign_table[foreign_key])
50
- constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
51
- scope = scope.where(predicate)
52
-
53
- join = join(foreign_table, constraint)
54
-
55
- scope = scope.joins(join)
56
-
57
- unless conditions[i].empty?
58
- scope = scope.where(sanitize(conditions[i], table))
59
- end
60
- end
61
- end
62
-
63
- scope
64
- end
65
- end
66
- end
67
- end
@@ -1,22 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class JoinDependency
4
- class JoinAssociation
5
- def build_constraint(reflection, table, key, foreign_table, foreign_key)
6
- # CPK
7
- # constraint = table[key].eq(foreign_table[foreign_key])
8
- constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
9
-
10
- if reflection.klass.finder_needs_type_condition?
11
- constraint = table.create_and([
12
- constraint,
13
- reflection.klass.send(:type_condition, table)
14
- ])
15
- end
16
-
17
- constraint
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,39 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class JoinDependency
4
- class JoinPart
5
- def aliased_primary_key
6
- # CPK
7
- # "#{aliased_prefix}_r0"
8
-
9
- active_record.composite? ?
10
- primary_key.inject([]) {|aliased_keys, key| aliased_keys << "#{ aliased_prefix }_r#{aliased_keys.length}"} :
11
- "#{ aliased_prefix }_r0"
12
- end
13
-
14
- def record_id(row)
15
- # CPK
16
- # row[aliased_primary_key]
17
- active_record.composite? ?
18
- aliased_primary_key.map {|key| row[key]}.to_composite_keys :
19
- row[aliased_primary_key]
20
- end
21
-
22
- def column_names_with_alias
23
- unless @column_names_with_alias
24
- @column_names_with_alias = []
25
-
26
- # CPK
27
- #([primary_key] + (column_names - [primary_key])).each_with_index do |column_name, i|
28
- keys = active_record.composite? ? primary_key.map(&:to_s) : [primary_key]
29
-
30
- (keys + (column_names - keys)).each_with_index do |column_name, i|
31
- @column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"]
32
- end
33
- end
34
- @column_names_with_alias
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,61 +0,0 @@
1
- module ActiveRecord
2
- module Associations
3
- class Preloader
4
- class Association
5
- def records_for(ids)
6
- # CPK
7
- # scoped.where(association_key.in(ids))
8
- predicate = cpk_in_predicate(table, reflection.foreign_key, ids)
9
- scoped.where(predicate)
10
- end
11
-
12
- def associated_records_by_owner
13
- # CPK
14
- # owner_keys = owners.map { |owner| owner[owner_key_name] }.compact.uniq
15
- owner_keys = owners.map do |owner|
16
- Array(owner_key_name).map do |owner_key|
17
- owner[owner_key]
18
- end
19
- end.compact.uniq
20
-
21
- if klass.nil? || owner_keys.empty?
22
- records = []
23
- else
24
- # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
25
- # Make several smaller queries if necessary or make one query if the adapter supports it
26
- sliced = owner_keys.each_slice(model.connection.in_clause_length || owner_keys.size)
27
- records = sliced.map { |slice| records_for(slice) }.flatten
28
- end
29
-
30
- # Each record may have multiple owners, and vice-versa
31
- records_by_owner = Hash[owners.map { |owner| [owner, []] }]
32
- records.each do |record|
33
- # CPK
34
- # owner_key = record[association_key_name].to_s
35
- owner_key = Array(association_key_name).map do |key_name|
36
- record[key_name]
37
- end.join(CompositePrimaryKeys::ID_SEP)
38
-
39
- owners_by_key[owner_key].each do |owner|
40
- records_by_owner[owner] << record
41
- end
42
- end
43
- records_by_owner
44
- end
45
-
46
- def owners_by_key
47
- @owners_by_key ||= owners.group_by do |owner|
48
- # CPK
49
- # key = owner[owner_key_name]
50
- key = Array(owner_key_name).map do |key_name|
51
- owner[key_name]
52
- end
53
- # CPK
54
- # key && key.to_s
55
- key && key.join(CompositePrimaryKeys::ID_SEP)
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end