composite_primary_keys 10.0.5 → 11.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +5 -5
  2. data/History.rdoc +2 -15
  3. data/README.rdoc +2 -1
  4. data/lib/composite_primary_keys.rb +7 -7
  5. data/lib/composite_primary_keys/arel/to_sql.rb +12 -6
  6. data/lib/composite_primary_keys/associations/association.rb +12 -14
  7. data/lib/composite_primary_keys/associations/association_scope.rb +7 -2
  8. data/lib/composite_primary_keys/associations/collection_association.rb +14 -5
  9. data/lib/composite_primary_keys/associations/foreign_association.rb +15 -0
  10. data/lib/composite_primary_keys/associations/has_many_association.rb +3 -15
  11. data/lib/composite_primary_keys/associations/has_many_through_association.rb +56 -58
  12. data/lib/composite_primary_keys/associations/join_dependency.rb +69 -71
  13. data/lib/composite_primary_keys/associations/preloader/association.rb +92 -75
  14. data/lib/composite_primary_keys/attribute_methods.rb +4 -6
  15. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +20 -23
  16. data/lib/composite_primary_keys/attribute_methods/read.rb +16 -18
  17. data/lib/composite_primary_keys/attribute_methods/write.rb +31 -33
  18. data/lib/composite_primary_keys/composite_arrays.rb +1 -1
  19. data/lib/composite_primary_keys/composite_predicates.rb +3 -0
  20. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  21. data/lib/composite_primary_keys/core.rb +40 -45
  22. data/lib/composite_primary_keys/fixtures.rb +17 -19
  23. data/lib/composite_primary_keys/locking/optimistic.rb +46 -38
  24. data/lib/composite_primary_keys/nested_attributes.rb +58 -60
  25. data/lib/composite_primary_keys/persistence.rb +72 -44
  26. data/lib/composite_primary_keys/reflection.rb +20 -0
  27. data/lib/composite_primary_keys/relation.rb +33 -79
  28. data/lib/composite_primary_keys/relation/batches.rb +7 -28
  29. data/lib/composite_primary_keys/relation/calculations.rb +28 -28
  30. data/lib/composite_primary_keys/relation/finder_methods.rb +34 -56
  31. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +18 -0
  32. data/lib/composite_primary_keys/relation/query_methods.rb +23 -11
  33. data/lib/composite_primary_keys/relation/where_clause.rb +13 -22
  34. data/lib/composite_primary_keys/sanitization.rb +0 -2
  35. data/lib/composite_primary_keys/version.rb +3 -3
  36. data/scripts/console.rb +48 -48
  37. data/scripts/txt2html +76 -76
  38. data/scripts/txt2js +65 -65
  39. data/tasks/website.rake +18 -18
  40. data/test/README_tests.rdoc +56 -56
  41. data/test/connections/databases.yml +30 -40
  42. data/test/db_test.rb +52 -52
  43. data/test/fixtures/articles.yml +6 -6
  44. data/test/fixtures/capitol.rb +3 -3
  45. data/test/fixtures/capitols.yml +16 -16
  46. data/test/fixtures/comments.yml +15 -15
  47. data/test/fixtures/department.rb +5 -5
  48. data/test/fixtures/departments.yml +15 -15
  49. data/test/fixtures/dorms.yml +4 -4
  50. data/test/fixtures/group.rb +2 -2
  51. data/test/fixtures/groups.yml +6 -6
  52. data/test/fixtures/hack.rb +4 -4
  53. data/test/fixtures/hacks.yml +2 -2
  54. data/test/fixtures/membership_status.rb +2 -2
  55. data/test/fixtures/product.rb +9 -9
  56. data/test/fixtures/product_tariff.rb +5 -5
  57. data/test/fixtures/products.yml +11 -11
  58. data/test/fixtures/reading.rb +4 -4
  59. data/test/fixtures/readings.yml +10 -10
  60. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  61. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  62. data/test/fixtures/reference_codes.yml +28 -28
  63. data/test/fixtures/reference_types.yml +9 -9
  64. data/test/fixtures/restaurant.rb +9 -9
  65. data/test/fixtures/restaurants.yml +14 -14
  66. data/test/fixtures/restaurants_suburbs.yml +10 -10
  67. data/test/fixtures/room.rb +11 -11
  68. data/test/fixtures/room_assignment.rb +13 -13
  69. data/test/fixtures/room_assignments.yml +24 -24
  70. data/test/fixtures/room_attribute.rb +2 -2
  71. data/test/fixtures/room_attribute_assignment.rb +4 -4
  72. data/test/fixtures/room_attribute_assignments.yml +4 -4
  73. data/test/fixtures/room_attributes.yml +2 -2
  74. data/test/fixtures/rooms.yml +12 -12
  75. data/test/fixtures/seat.rb +5 -5
  76. data/test/fixtures/seats.yml +8 -8
  77. data/test/fixtures/street.rb +2 -2
  78. data/test/fixtures/streets.yml +16 -16
  79. data/test/fixtures/student.rb +3 -3
  80. data/test/fixtures/students.yml +15 -15
  81. data/test/fixtures/suburbs.yml +14 -14
  82. data/test/fixtures/tariffs.yml +1 -1
  83. data/test/plugins/pagination.rb +405 -405
  84. data/test/plugins/pagination_helper.rb +135 -135
  85. data/test/setup.rb +50 -50
  86. data/test/test_aliases.rb +18 -18
  87. data/test/test_associations.rb +6 -20
  88. data/test/test_composite_arrays.rb +24 -24
  89. data/test/test_counter_cache.rb +30 -30
  90. data/test/test_delete.rb +146 -146
  91. data/test/test_dup.rb +37 -37
  92. data/test/test_exists.rb +39 -39
  93. data/test/test_find.rb +1 -1
  94. data/test/test_miscellaneous.rb +32 -32
  95. data/test/test_pagination.rb +35 -35
  96. data/test/test_preload.rb +0 -7
  97. data/test/test_validations.rb +13 -13
  98. metadata +10 -17
  99. data/lib/composite_primary_keys/arel/in.rb +0 -6
  100. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +0 -24
  101. data/lib/composite_primary_keys/associations/preloader/belongs_to.rb +0 -19
  102. data/lib/composite_primary_keys/relation/predicate_builder/association_query_handler.rb +0 -33
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: f506d34f44d88354550c10d7b79f38c206dfa739c9c903e59c888252db3ae9ed
4
- data.tar.gz: 06e5788af10e07e0dbe785338aa35c8cce99df310a7e43d3605b1cefb0eaa0ac
2
+ SHA1:
3
+ metadata.gz: d06bf922f3eb20b48d379ecac345de0ba325c713
4
+ data.tar.gz: 6d76da0b87220520f5cd255637c587ca5d1ec16a
5
5
  SHA512:
6
- metadata.gz: 83ec3b8f8f42e7a5c92f11c60cb86fdb8fd4bdc9e6a58f1f3305ffb5c9b6af9b2abaabcaa43eeaa2e40e19cddab4cad5d7e5501905643e7b4fad0749525ac0b7
7
- data.tar.gz: d1a8bad2cdea8ff1eab3ba7d5c456d68f008583b26b6000a930f58d319842aa6b2a24e66d7d509bd531cce4852f062797b96e53c0f929ed8aa0c6a19d40621c2
6
+ metadata.gz: fafab03151230da90b5891b4f821685869bcd3e745b803ce812e4ee85091153a11c076a3f97ebf56b1f6d0fcc57bf7c45c30a7976be90241a3f3dd8b9e09d688
7
+ data.tar.gz: dade89e143c24027b39a99166798e467fbf6ca5fe83689b31838756d57ecfd27604277b89b016c3effa9538576e4d3267b72ae1d4c5882454455051e55310856
@@ -1,15 +1,6 @@
1
- == 10.0.5 (2018-06-24)
2
- * Fix in_batches (Urmo Rae)
1
+ == 11.0.0.beta1 (2017-12-03)
3
2
 
4
- == 10.0.4 (2018-06-17)
5
-
6
- * Lock mysql2 gem to 0.4.x (Le Trung Kien)
7
- * Set inversion when using preload (Le Trung Kien)
8
- * Fixes for AR 5.1.6 (Tim Morgan)
9
-
10
- == 10.0.3 (2018-02-25)
11
-
12
- * Require a minimum of activerecord version 5.1.5 (Tom Hughes)
3
+ * ActiveRecord 5.2.beta2 support (Charlie Savage)
13
4
 
14
5
  == 10.0.2 (2017-12-02)
15
6
 
@@ -28,10 +19,6 @@
28
19
  * Fix typo in Readme (Mike Gunderloy)
29
20
  * Improved sql server support (Artyom Nikolaev)
30
21
 
31
- == 9.0.10 (2018-06-24)
32
-
33
- * Fix AR 5.0.7 (Jordan Owens)
34
-
35
22
  == 9.0.8 (2017-10-11)
36
23
 
37
24
  * Fix Paper Trail compatibility (Sean Linsley)
@@ -20,7 +20,8 @@ Every major version of ActiveRecord has included numerous internal changes. As
20
20
  CPK has to be rewritten for each version of ActiveRecord. To help keep
21
21
  things straight, here is the mapping:
22
22
 
23
- Version 10.x is designed to work with ActiveRecord 5.1.x (note this is in progress)
23
+ Version 11.x is designed to work with ActiveRecord 5.2.x (note this is in progress)
24
+ Version 10.x is designed to work with ActiveRecord 5.1.x
24
25
  Version 9.x is designed to work with ActiveRecord 5.0.x
25
26
  Version 8.x is designed to work with ActiveRecord 4.2.x
26
27
  Version 7.x is designed to work with ActiveRecord 4.1.x
@@ -26,7 +26,7 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', ['~>5.1.0', '>= 5.1.5']
29
+ gem 'activerecord', '~> 5.2.0.beta2'
30
30
  require 'active_record'
31
31
  end
32
32
 
@@ -35,6 +35,7 @@ require 'active_record/counter_cache'
35
35
  require 'active_record/fixtures'
36
36
  require 'active_record/model_schema'
37
37
  require 'active_record/persistence'
38
+ require 'active_record/reflection'
38
39
  require 'active_record/relation'
39
40
  require 'active_record/sanitization'
40
41
  require 'active_record/attribute_methods'
@@ -42,12 +43,11 @@ require 'active_record/autosave_association'
42
43
 
43
44
  require 'active_record/associations/association'
44
45
  require 'active_record/associations/association_scope'
46
+ require 'active_record/associations/foreign_association'
45
47
  require 'active_record/associations/has_many_association'
46
48
  require 'active_record/associations/has_many_through_association'
47
49
  require 'active_record/associations/join_dependency'
48
- require 'active_record/associations/join_dependency/join_association'
49
50
  require 'active_record/associations/preloader/association'
50
- require 'active_record/associations/preloader/belongs_to'
51
51
  require 'active_record/associations/singular_association'
52
52
  require 'active_record/associations/collection_association'
53
53
 
@@ -66,6 +66,7 @@ require 'active_record/relation/where_clause'
66
66
  require 'active_record/relation/calculations'
67
67
  require 'active_record/relation/finder_methods'
68
68
  require 'active_record/relation/query_methods'
69
+ require 'active_record/relation/predicate_builder/association_query_value'
69
70
 
70
71
  # CPK files
71
72
  require 'composite_primary_keys/persistence'
@@ -74,6 +75,7 @@ require 'composite_primary_keys/core'
74
75
  require 'composite_primary_keys/composite_arrays'
75
76
  require 'composite_primary_keys/composite_predicates'
76
77
  require 'composite_primary_keys/fixtures'
78
+ require 'composite_primary_keys/reflection'
77
79
  require 'composite_primary_keys/relation'
78
80
  require 'composite_primary_keys/sanitization'
79
81
  require 'composite_primary_keys/attribute_methods'
@@ -82,12 +84,11 @@ require 'composite_primary_keys/version'
82
84
 
83
85
  require 'composite_primary_keys/associations/association'
84
86
  require 'composite_primary_keys/associations/association_scope'
87
+ require 'composite_primary_keys/associations/foreign_association'
85
88
  require 'composite_primary_keys/associations/has_many_association'
86
89
  require 'composite_primary_keys/associations/has_many_through_association'
87
90
  require 'composite_primary_keys/associations/join_dependency'
88
- require 'composite_primary_keys/associations/join_dependency/join_association'
89
91
  require 'composite_primary_keys/associations/preloader/association'
90
- require 'composite_primary_keys/associations/preloader/belongs_to'
91
92
  require 'composite_primary_keys/associations/collection_association'
92
93
 
93
94
  require 'composite_primary_keys/attribute_methods/primary_key'
@@ -104,11 +105,10 @@ require 'composite_primary_keys/relation/batches'
104
105
  require 'composite_primary_keys/relation/where_clause'
105
106
  require 'composite_primary_keys/relation/calculations'
106
107
  require 'composite_primary_keys/relation/finder_methods'
107
- require 'composite_primary_keys/relation/predicate_builder/association_query_handler'
108
+ require 'composite_primary_keys/relation/predicate_builder/association_query_value'
108
109
  require 'composite_primary_keys/relation/query_methods'
109
110
 
110
111
  require 'composite_primary_keys/composite_relation'
111
112
 
112
- require 'composite_primary_keys/arel/in'
113
113
  require 'composite_primary_keys/arel/to_sql'
114
114
  require 'composite_primary_keys/arel/sqlserver'
@@ -1,12 +1,18 @@
1
1
  module Arel
2
2
  module Visitors
3
3
  class ToSql
4
- def visit_CompositePrimaryKeys_Nodes_In o, collector
5
- collector << "("
6
- visit(o.left, collector)
7
- collector << ")"
8
- collector << " IN ("
9
- visit(o.right, collector) << ")"
4
+ def visit_Arel_Nodes_In o, collector
5
+ if Array === o.right && o.right.empty?
6
+ collector << '1=0'
7
+ else
8
+ # CPK
9
+ collector << "("
10
+ collector = visit o.left, collector
11
+ # CPK
12
+ collector << ")"
13
+ collector << " IN ("
14
+ visit(o.right, collector) << ")"
15
+ end
10
16
  end
11
17
 
12
18
  def visit_CompositePrimaryKeys_CompositeKeys o, collector
@@ -1,24 +1,22 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class Association
4
- silence_warnings do
5
- def creation_attributes
6
- attributes = {}
4
+ def creation_attributes
5
+ attributes = {}
7
6
 
8
- if (reflection.has_one? || reflection.collection?) && !options[:through]
9
- # CPK
10
- # attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
11
- Array(reflection.foreign_key).zip(Array(reflection.active_record_primary_key)).each do |key1, key2|
12
- attributes[key1] = owner[key2]
13
- end
14
-
15
- if reflection.options[:as]
16
- attributes[reflection.type] = owner.class.base_class.name
17
- end
7
+ if (reflection.has_one? || reflection.collection?) && !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]
18
12
  end
19
13
 
20
- attributes
14
+ if reflection.options[:as]
15
+ attributes[reflection.type] = owner.class.base_class.name
16
+ end
21
17
  end
18
+
19
+ attributes
22
20
  end
23
21
  end
24
22
  end
@@ -22,11 +22,13 @@ module ActiveRecord
22
22
  binds
23
23
  end
24
24
 
25
- def last_chain_scope(scope, table, reflection, owner)
25
+ def last_chain_scope(scope, reflection, owner)
26
26
  join_keys = reflection.join_keys
27
27
  key = join_keys.key
28
28
  foreign_key = join_keys.foreign_key
29
29
 
30
+ table = reflection.aliased_table
31
+
30
32
  # CPK
31
33
  # value = transform_value(owner[foreign_key])
32
34
  # scope = apply_scope(scope, table, key, value)
@@ -43,11 +45,14 @@ module ActiveRecord
43
45
  scope
44
46
  end
45
47
 
46
- def next_chain_scope(scope, table, reflection, foreign_table, next_reflection)
48
+ def next_chain_scope(scope, reflection, next_reflection)
47
49
  join_keys = reflection.join_keys
48
50
  key = join_keys.key
49
51
  foreign_key = join_keys.foreign_key
50
52
 
53
+ table = reflection.aliased_table
54
+ foreign_table = next_reflection.aliased_table
55
+
51
56
  # CPK
52
57
  # constraint = table[key].eq(foreign_table[foreign_key])
53
58
  constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
@@ -24,9 +24,10 @@ module CompositePrimaryKeys
24
24
  end
25
25
 
26
26
  def ids_writer(ids)
27
- pk_type = reflection.primary_key_type
27
+ pk_type = reflection.association_primary_key_type
28
28
  ids = Array(ids).reject(&:blank?)
29
29
  ids.map! { |i| pk_type.cast(i) }
30
+
30
31
  # CPK
31
32
  if reflection.association_primary_key.is_a?(Array)
32
33
  predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
@@ -34,11 +35,19 @@ module CompositePrimaryKeys
34
35
  reflection.association_primary_key.map{ |k| r.send(k) }
35
36
  end.values_at(*ids)
36
37
  else
37
- records = klass.where(reflection.association_primary_key => ids).index_by do |r|
38
- r.send(reflection.association_primary_key)
39
- end.values_at(*ids)
38
+ primary_key = reflection.association_primary_key
39
+ records = klass.where(primary_key => ids).index_by do |r|
40
+ r.public_send(primary_key)
41
+ end.values_at(*ids).compact
42
+ end
43
+
44
+ if records.size != ids.size
45
+ found_ids = records.map { |record| record.public_send(primary_key) }
46
+ not_found_ids = ids - found_ids
47
+ klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
48
+ else
49
+ replace(records)
40
50
  end
41
- replace(records)
42
51
  end
43
52
  end
44
53
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord::Associations
4
+ module ForeignAssociation # :nodoc:
5
+ def foreign_key_present?
6
+ if reflection.klass.primary_key
7
+ # CPK
8
+ # owner.attribute_present?(reflection.active_record_primary_key)
9
+ Array(reflection.active_record_primary_key).all? {|key| owner.attribute_present?(key)}
10
+ else
11
+ false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,21 +1,19 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class HasManyAssociation
4
- include CompositePrimaryKeys::Predicates
5
-
6
4
  def delete_records(records, method)
7
5
  if method == :destroy
8
6
  records.each(&:destroy!)
9
7
  update_counter(-records.length) unless reflection.inverse_updates_counter_cache?
10
- return
11
- # Zerista
8
+ # CPK
12
9
  elsif self.reflection.klass.composite?
13
10
  predicate = cpk_in_predicate(self.scope.table, self.reflection.klass.primary_keys, records.map(&:id))
14
11
  scope = self.scope.where(predicate)
12
+ update_counter(-delete_count(method, scope))
15
13
  else
16
14
  scope = self.scope.where(reflection.klass.primary_key => records)
15
+ update_counter(-delete_count(method, scope))
17
16
  end
18
- update_counter(-delete_count(method, scope))
19
17
  end
20
18
 
21
19
  def delete_count(method, scope)
@@ -31,16 +29,6 @@ module ActiveRecord
31
29
  scope.update_all(conds)
32
30
  end
33
31
  end
34
-
35
- def foreign_key_present?
36
- if reflection.klass.primary_key
37
- # CPK
38
- # owner.attribute_present?(reflection.association_primary_key)
39
- Array(reflection.klass.primary_key).all? {|key| owner.attribute_present?(key)}
40
- else
41
- false
42
- end
43
- end
44
32
  end
45
33
  end
46
34
  end
@@ -13,75 +13,73 @@ module ActiveRecord
13
13
  cpk_in_predicate(through_association.scope.klass.arel_table, source_reflection.foreign_key, ids)
14
14
  end
15
15
 
16
- silence_warnings do
17
- def delete_records(records, method)
18
- ensure_not_nested
16
+ def delete_records(records, method)
17
+ ensure_not_nested
19
18
 
20
- scope = through_association.scope
21
- # CPK
22
- # scope.where! construct_join_attributes(*records)
23
- if source_reflection.klass.composite?
24
- scope.where! cpk_join_through_predicate(*records)
25
- else
26
- scope.where! construct_join_attributes(*records)
27
- end
19
+ scope = through_association.scope
20
+ # CPK
21
+ # scope.where! construct_join_attributes(*records)
22
+ if source_reflection.klass.composite?
23
+ scope.where! cpk_join_through_predicate(*records)
24
+ else
25
+ scope.where! construct_join_attributes(*records)
26
+ end
28
27
 
29
- case method
30
- when :destroy
31
- if scope.klass.primary_key
32
- count = scope.destroy_all.length
33
- else
34
- scope.to_a.each do |record|
35
- record.run_callbacks :destroy
36
- end
28
+ case method
29
+ when :destroy
30
+ if scope.klass.primary_key
31
+ count = scope.destroy_all.length
32
+ else
33
+ scope.to_a.each do |record|
34
+ record.run_callbacks :destroy
35
+ end
37
36
 
38
- arel = scope.arel
37
+ arel = scope.arel
39
38
 
40
- stmt = Arel::DeleteManager.new arel.engine
41
- stmt.from scope.klass.arel_table
42
- stmt.wheres = arel.constraints
39
+ stmt = Arel::DeleteManager.new arel.engine
40
+ stmt.from scope.klass.arel_table
41
+ stmt.wheres = arel.constraints
43
42
 
44
- count = scope.klass.connection.delete(stmt, 'SQL', scope.bind_values)
45
- end
46
- when :nullify
47
- count = scope.update_all(source_reflection.foreign_key => nil)
48
- else
49
- count = scope.delete_all
43
+ count = scope.klass.connection.delete(stmt, 'SQL', scope.bind_values)
50
44
  end
45
+ when :nullify
46
+ count = scope.update_all(source_reflection.foreign_key => nil)
47
+ else
48
+ count = scope.delete_all
49
+ end
51
50
 
52
- delete_through_records(records)
53
-
54
- if source_reflection.options[:counter_cache] && method != :destroy
55
- counter = source_reflection.counter_cache_column
56
- klass.decrement_counter counter, records.map(&:id)
57
- end
51
+ delete_through_records(records)
58
52
 
59
- if through_reflection.collection? && update_through_counter?(method)
60
- update_counter(-count, through_reflection)
61
- end
53
+ if source_reflection.options[:counter_cache] && method != :destroy
54
+ counter = source_reflection.counter_cache_column
55
+ klass.decrement_counter counter, records.map(&:id)
56
+ end
62
57
 
63
- update_counter(-count)
58
+ if through_reflection.collection? && update_through_counter?(method)
59
+ update_counter(-count, through_reflection)
64
60
  end
65
61
 
66
- def through_records_for(record)
67
- # CPK
68
- # attributes = construct_join_attributes(record)
69
- # candidates = Array.wrap(through_association.target)
70
- # candidates.find_all do |c|
71
- # attributes.all? do |key, value|
72
- # c.public_send(key) == value
73
- # end
74
- # end
75
- if record.composite?
76
- candidates = Array.wrap(through_association.target)
77
- candidates.find_all { |c| c.attributes.slice(*source_reflection.association_primary_key) == record.ids_hash }
78
- else
79
- attributes = construct_join_attributes(record)
80
- candidates = Array.wrap(through_association.target)
81
- candidates.find_all do |c|
82
- attributes.all? do |key, value|
83
- c.public_send(key) == value
84
- end
62
+ update_counter(-count)
63
+ end
64
+
65
+ def through_records_for(record)
66
+ # CPK
67
+ # attributes = construct_join_attributes(record)
68
+ # candidates = Array.wrap(through_association.target)
69
+ # candidates.find_all do |c|
70
+ # attributes.all? do |key, value|
71
+ # c.public_send(key) == value
72
+ # end
73
+ # end
74
+ if record.composite?
75
+ candidates = Array.wrap(through_association.target)
76
+ candidates.find_all { |c| c.attributes.slice(*source_reflection.association_primary_key) == record.ids_hash }
77
+ else
78
+ attributes = construct_join_attributes(record)
79
+ candidates = Array.wrap(through_association.target)
80
+ candidates.find_all do |c|
81
+ attributes.all? do |key, value|
82
+ c.public_send(key) == value
85
83
  end
86
84
  end
87
85
  end
@@ -2,100 +2,98 @@ module ActiveRecord
2
2
  module Associations
3
3
  class JoinDependency
4
4
  class Aliases # :nodoc:
5
- silence_warnings do
6
- def column_alias(node, column)
7
- # CPK
8
- #@alias_cache[node][column]
9
- if column.kind_of?(Array)
10
- column.map do |a_column|
11
- @alias_cache[node][a_column]
12
- end
13
- else
14
- @alias_cache[node][column]
5
+ def column_alias(node, column)
6
+ # CPK
7
+ #@alias_cache[node][column]
8
+ if column.kind_of?(Array)
9
+ column.map do |a_column|
10
+ @alias_cache[node][a_column]
15
11
  end
12
+ else
13
+ @alias_cache[node][column]
16
14
  end
17
15
  end
18
16
  end
19
17
 
20
- silence_warnings do
21
- def instantiate(result_set, &block)
22
- primary_key = aliases.column_alias(join_root, join_root.primary_key)
18
+ def instantiate(result_set, &block)
19
+ primary_key = aliases.column_alias(join_root, join_root.primary_key)
23
20
 
24
- seen = Hash.new { |i, object_id|
25
- i[object_id] = Hash.new { |j, child_class|
26
- j[child_class] = {}
27
- }
21
+ seen = Hash.new { |i, object_id|
22
+ i[object_id] = Hash.new { |j, child_class|
23
+ j[child_class] = {}
28
24
  }
25
+ }
29
26
 
30
- model_cache = Hash.new { |h,klass| h[klass] = {} }
31
- parents = model_cache[join_root]
32
- column_aliases = aliases.column_aliases join_root
27
+ model_cache = Hash.new { |h, klass| h[klass] = {} }
28
+ parents = model_cache[join_root]
29
+ column_aliases = aliases.column_aliases join_root
33
30
 
34
- message_bus = ActiveSupport::Notifications.instrumenter
31
+ message_bus = ActiveSupport::Notifications.instrumenter
35
32
 
36
- payload = {
33
+ payload = {
37
34
  record_count: result_set.length,
38
35
  class_name: join_root.base_klass.name
39
- }
36
+ }
40
37
 
41
- message_bus.instrument('instantiation.active_record', payload) do
42
- result_set.each { |row_hash|
43
- # CPK
44
- parent_key = if primary_key.kind_of?(Array)
45
- primary_key.map {|key| row_hash[key]}
46
- else
47
- primary_key ? row_hash[primary_key] : row_hash
48
- end
49
-
50
- parent = parents[parent_key] ||= join_root.instantiate(row_hash, column_aliases, &block)
51
- construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
52
- }
53
- end
38
+ message_bus.instrument("instantiation.active_record", payload) do
39
+ result_set.each { |row_hash|
40
+ # CPK
41
+ # parent_key = primary_key ? row_hash[primary_key] : row_hash
42
+ # CPK
43
+ parent_key = if primary_key.kind_of?(Array)
44
+ primary_key.map {|key| row_hash[key]}
45
+ else
46
+ primary_key ? row_hash[primary_key] : row_hash
47
+ end
54
48
 
55
- parents.values
49
+ parent = parents[parent_key] ||= join_root.instantiate(row_hash, column_aliases, &block)
50
+ construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
51
+ }
56
52
  end
57
53
 
58
- def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
59
- return if ar_parent.nil?
54
+ parents.values
55
+ end
56
+
57
+ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
58
+ return if ar_parent.nil?
60
59
 
61
- parent.children.each do |node|
62
- if node.reflection.collection?
63
- other = ar_parent.association(node.reflection.name)
64
- other.loaded!
65
- elsif ar_parent.association_cached?(node.reflection.name)
66
- model = ar_parent.association(node.reflection.name).target
67
- construct(model, node, row, rs, seen, model_cache, aliases)
68
- next
69
- end
60
+ parent.children.each do |node|
61
+ if node.reflection.collection?
62
+ other = ar_parent.association(node.reflection.name)
63
+ other.loaded!
64
+ elsif ar_parent.association_cached?(node.reflection.name)
65
+ model = ar_parent.association(node.reflection.name).target
66
+ construct(model, node, row, rs, seen, model_cache, aliases)
67
+ next
68
+ end
70
69
 
71
- key = aliases.column_alias(node, node.primary_key)
70
+ key = aliases.column_alias(node, node.primary_key)
72
71
 
73
- # CPK
74
- if key.is_a?(Array)
75
- id = Array(key).map do |column_alias|
76
- row[column_alias]
77
- end
78
- # At least the first value in the key has to be set. Should we require all values to be set?
79
- id = nil if id.first.nil?
80
- else # original
81
- id = row[key]
72
+ # CPK
73
+ if key.is_a?(Array)
74
+ id = Array(key).map do |column_alias|
75
+ row[column_alias]
82
76
  end
77
+ # At least the first value in the key has to be set. Should we require all values to be set?
78
+ id = nil if id.first.nil?
79
+ else # original
80
+ id = row[key]
81
+ end
83
82
 
84
- if id.nil? # duplicating this so it is clear what remained unchanged from the original
85
- nil_association = ar_parent.association(node.reflection.name)
86
- nil_association.loaded!
87
- next
88
- end
83
+ if id.nil? # duplicating this so it is clear what remained unchanged from the original
84
+ nil_association = ar_parent.association(node.reflection.name)
85
+ nil_association.loaded!
86
+ next
87
+ end
89
88
 
90
- model = seen[ar_parent.object_id][node.base_klass][id]
89
+ model = seen[ar_parent.object_id][node.base_klass][id]
91
90
 
92
- if model
93
- construct(model, node, row, rs, seen, model_cache, aliases)
94
- else
95
- model = construct_model(ar_parent, node, row, model_cache, id, aliases)
96
- seen[ar_parent.object_id][node.base_klass][id] = model
97
- construct(model, node, row, rs, seen, model_cache, aliases)
98
- end
91
+ if model
92
+ construct(model, node, row, rs, seen, model_cache, aliases)
93
+ else
94
+ model = construct_model(ar_parent, node, row, model_cache, id, aliases)
95
+ seen[ar_parent.object_id][node.base_klass][id] = model
96
+ construct(model, node, row, rs, seen, model_cache, aliases)
99
97
  end
100
98
  end
101
99
  end