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.
- checksums.yaml +5 -5
- data/History.rdoc +2 -15
- data/README.rdoc +2 -1
- data/lib/composite_primary_keys.rb +7 -7
- data/lib/composite_primary_keys/arel/to_sql.rb +12 -6
- data/lib/composite_primary_keys/associations/association.rb +12 -14
- data/lib/composite_primary_keys/associations/association_scope.rb +7 -2
- data/lib/composite_primary_keys/associations/collection_association.rb +14 -5
- data/lib/composite_primary_keys/associations/foreign_association.rb +15 -0
- data/lib/composite_primary_keys/associations/has_many_association.rb +3 -15
- data/lib/composite_primary_keys/associations/has_many_through_association.rb +56 -58
- data/lib/composite_primary_keys/associations/join_dependency.rb +69 -71
- data/lib/composite_primary_keys/associations/preloader/association.rb +92 -75
- data/lib/composite_primary_keys/attribute_methods.rb +4 -6
- data/lib/composite_primary_keys/attribute_methods/primary_key.rb +20 -23
- data/lib/composite_primary_keys/attribute_methods/read.rb +16 -18
- data/lib/composite_primary_keys/attribute_methods/write.rb +31 -33
- data/lib/composite_primary_keys/composite_arrays.rb +1 -1
- data/lib/composite_primary_keys/composite_predicates.rb +3 -0
- data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
- data/lib/composite_primary_keys/core.rb +40 -45
- data/lib/composite_primary_keys/fixtures.rb +17 -19
- data/lib/composite_primary_keys/locking/optimistic.rb +46 -38
- data/lib/composite_primary_keys/nested_attributes.rb +58 -60
- data/lib/composite_primary_keys/persistence.rb +72 -44
- data/lib/composite_primary_keys/reflection.rb +20 -0
- data/lib/composite_primary_keys/relation.rb +33 -79
- data/lib/composite_primary_keys/relation/batches.rb +7 -28
- data/lib/composite_primary_keys/relation/calculations.rb +28 -28
- data/lib/composite_primary_keys/relation/finder_methods.rb +34 -56
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +18 -0
- data/lib/composite_primary_keys/relation/query_methods.rb +23 -11
- data/lib/composite_primary_keys/relation/where_clause.rb +13 -22
- data/lib/composite_primary_keys/sanitization.rb +0 -2
- data/lib/composite_primary_keys/version.rb +3 -3
- data/scripts/console.rb +48 -48
- data/scripts/txt2html +76 -76
- data/scripts/txt2js +65 -65
- data/tasks/website.rake +18 -18
- data/test/README_tests.rdoc +56 -56
- data/test/connections/databases.yml +30 -40
- data/test/db_test.rb +52 -52
- data/test/fixtures/articles.yml +6 -6
- data/test/fixtures/capitol.rb +3 -3
- data/test/fixtures/capitols.yml +16 -16
- data/test/fixtures/comments.yml +15 -15
- data/test/fixtures/department.rb +5 -5
- data/test/fixtures/departments.yml +15 -15
- data/test/fixtures/dorms.yml +4 -4
- data/test/fixtures/group.rb +2 -2
- data/test/fixtures/groups.yml +6 -6
- data/test/fixtures/hack.rb +4 -4
- data/test/fixtures/hacks.yml +2 -2
- data/test/fixtures/membership_status.rb +2 -2
- data/test/fixtures/product.rb +9 -9
- data/test/fixtures/product_tariff.rb +5 -5
- data/test/fixtures/products.yml +11 -11
- data/test/fixtures/reading.rb +4 -4
- data/test/fixtures/readings.yml +10 -10
- data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
- data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
- data/test/fixtures/reference_codes.yml +28 -28
- data/test/fixtures/reference_types.yml +9 -9
- data/test/fixtures/restaurant.rb +9 -9
- data/test/fixtures/restaurants.yml +14 -14
- data/test/fixtures/restaurants_suburbs.yml +10 -10
- data/test/fixtures/room.rb +11 -11
- data/test/fixtures/room_assignment.rb +13 -13
- data/test/fixtures/room_assignments.yml +24 -24
- data/test/fixtures/room_attribute.rb +2 -2
- data/test/fixtures/room_attribute_assignment.rb +4 -4
- data/test/fixtures/room_attribute_assignments.yml +4 -4
- data/test/fixtures/room_attributes.yml +2 -2
- data/test/fixtures/rooms.yml +12 -12
- data/test/fixtures/seat.rb +5 -5
- data/test/fixtures/seats.yml +8 -8
- data/test/fixtures/street.rb +2 -2
- data/test/fixtures/streets.yml +16 -16
- data/test/fixtures/student.rb +3 -3
- data/test/fixtures/students.yml +15 -15
- data/test/fixtures/suburbs.yml +14 -14
- data/test/fixtures/tariffs.yml +1 -1
- data/test/plugins/pagination.rb +405 -405
- data/test/plugins/pagination_helper.rb +135 -135
- data/test/setup.rb +50 -50
- data/test/test_aliases.rb +18 -18
- data/test/test_associations.rb +6 -20
- data/test/test_composite_arrays.rb +24 -24
- data/test/test_counter_cache.rb +30 -30
- data/test/test_delete.rb +146 -146
- data/test/test_dup.rb +37 -37
- data/test/test_exists.rb +39 -39
- data/test/test_find.rb +1 -1
- data/test/test_miscellaneous.rb +32 -32
- data/test/test_pagination.rb +35 -35
- data/test/test_preload.rb +0 -7
- data/test/test_validations.rb +13 -13
- metadata +10 -17
- data/lib/composite_primary_keys/arel/in.rb +0 -6
- data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +0 -24
- data/lib/composite_primary_keys/associations/preloader/belongs_to.rb +0 -19
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_handler.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d06bf922f3eb20b48d379ecac345de0ba325c713
|
4
|
+
data.tar.gz: 6d76da0b87220520f5cd255637c587ca5d1ec16a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fafab03151230da90b5891b4f821685869bcd3e745b803ce812e4ee85091153a11c076a3f97ebf56b1f6d0fcc57bf7c45c30a7976be90241a3f3dd8b9e09d688
|
7
|
+
data.tar.gz: dade89e143c24027b39a99166798e467fbf6ca5fe83689b31838756d57ecfd27604277b89b016c3effa9538576e4d3267b72ae1d4c5882454455051e55310856
|
data/History.rdoc
CHANGED
@@ -1,15 +1,6 @@
|
|
1
|
-
==
|
2
|
-
* Fix in_batches (Urmo Rae)
|
1
|
+
== 11.0.0.beta1 (2017-12-03)
|
3
2
|
|
4
|
-
|
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)
|
data/README.rdoc
CHANGED
@@ -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
|
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',
|
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/
|
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
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
5
|
-
|
6
|
-
attributes = {}
|
4
|
+
def creation_attributes
|
5
|
+
attributes = {}
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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,
|
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,
|
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.
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
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
|
-
|
17
|
-
|
18
|
-
ensure_not_nested
|
16
|
+
def delete_records(records, method)
|
17
|
+
ensure_not_nested
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
37
|
+
arel = scope.arel
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
stmt = Arel::DeleteManager.new arel.engine
|
40
|
+
stmt.from scope.klass.arel_table
|
41
|
+
stmt.wheres = arel.constraints
|
43
42
|
|
44
|
-
|
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
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
58
|
+
if through_reflection.collection? && update_through_counter?(method)
|
59
|
+
update_counter(-count, through_reflection)
|
64
60
|
end
|
65
61
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
21
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
31
|
+
message_bus = ActiveSupport::Notifications.instrumenter
|
35
32
|
|
36
|
-
|
33
|
+
payload = {
|
37
34
|
record_count: result_set.length,
|
38
35
|
class_name: join_root.base_klass.name
|
39
|
-
|
36
|
+
}
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
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
|
-
|
59
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
70
|
+
key = aliases.column_alias(node, node.primary_key)
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
89
|
+
model = seen[ar_parent.object_id][node.base_klass][id]
|
91
90
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|