composite_primary_keys 14.0.6 → 14.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +13 -0
  3. data/README.rdoc +182 -182
  4. data/Rakefile +1 -1
  5. data/lib/composite_primary_keys/associations/association.rb +2 -2
  6. data/lib/composite_primary_keys/associations/association_scope.rb +1 -1
  7. data/lib/composite_primary_keys/associations/collection_association.rb +38 -38
  8. data/lib/composite_primary_keys/associations/has_many_through_association.rb +19 -0
  9. data/lib/composite_primary_keys/associations/preloader/association.rb +52 -52
  10. data/lib/composite_primary_keys/autosave_association.rb +60 -60
  11. data/lib/composite_primary_keys/composite_arrays.rb +88 -88
  12. data/lib/composite_primary_keys/composite_predicates.rb +121 -121
  13. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +1 -2
  14. data/lib/composite_primary_keys/persistence.rb +96 -96
  15. data/lib/composite_primary_keys/relation/calculations.rb +110 -110
  16. data/lib/composite_primary_keys/relation/query_methods.rb +14 -16
  17. data/lib/composite_primary_keys/relation.rb +199 -197
  18. data/lib/composite_primary_keys/validations/uniqueness.rb +40 -40
  19. data/lib/composite_primary_keys/version.rb +1 -1
  20. data/lib/composite_primary_keys.rb +117 -119
  21. data/scripts/console.rb +2 -2
  22. data/tasks/databases/trilogy.rake +23 -0
  23. data/test/abstract_unit.rb +124 -118
  24. data/test/connections/databases.ci.yml +10 -0
  25. data/test/fixtures/admin.rb +4 -0
  26. data/test/fixtures/comments.yml +6 -0
  27. data/test/fixtures/db_definitions/db2-create-tables.sql +34 -0
  28. data/test/fixtures/db_definitions/db2-drop-tables.sql +7 -1
  29. data/test/fixtures/db_definitions/mysql.sql +23 -0
  30. data/test/fixtures/db_definitions/oracle.drop.sql +4 -0
  31. data/test/fixtures/db_definitions/oracle.sql +21 -0
  32. data/test/fixtures/db_definitions/postgresql.sql +23 -0
  33. data/test/fixtures/db_definitions/sqlite.sql +21 -0
  34. data/test/fixtures/db_definitions/sqlserver.sql +23 -0
  35. data/test/fixtures/department.rb +20 -20
  36. data/test/fixtures/moderator.rb +4 -0
  37. data/test/fixtures/room.rb +4 -1
  38. data/test/fixtures/room_assignment.rb +18 -18
  39. data/test/fixtures/staff_room.rb +6 -0
  40. data/test/fixtures/staff_room_key.rb +6 -0
  41. data/test/fixtures/user.rb +3 -0
  42. data/test/fixtures/user_with_polymorphic_name.rb +9 -0
  43. data/test/test_associations.rb +403 -403
  44. data/test/test_composite_arrays.rb +44 -44
  45. data/test/test_has_one_through.rb +30 -0
  46. data/test/test_polymorphic.rb +6 -0
  47. metadata +11 -4
  48. data/lib/composite_primary_keys/associations/through_association.rb +0 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76a010358e1d2cacd9b05f6229c1fe4225f20f888fefb57b0bd6a9f62589a720
4
- data.tar.gz: 1b7870c774bbed21d4556cf53518054860903cae4a0987f5978e02fc3883597f
3
+ metadata.gz: 717baef11c50b8c166a79ddb4c024c5c9bb1665062381125a338ad11f1d87fc0
4
+ data.tar.gz: dfb9f5b2cba6bb0f95a2f27126eba9b08a24eb3189ca7abcdfed98e30464e39d
5
5
  SHA512:
6
- metadata.gz: 117d7be0b54619f7adcb05ddb200c3c97571cc74bf2fcf33c99034efaec08924cf6d56e998db1135c92bf71497697b30ca531a882d755329d5b6f9192d606013
7
- data.tar.gz: a9f330398e5b1a9153a67f229a28e83d65c5bc02334e6e9f3456229c4c1e011a3bf03909fa45e37739d4ab47a37663a3ee6ef09c7d7194721e91849e520c0a01
6
+ metadata.gz: 4561141fc8ecbd420dbeb2e0e0c8b28c77cd8f2156e0bcf073776b17dd9cb3c9b5703ae159791c151973668aa6cfd46547501201a818a78ffcb553abf498a58f
7
+ data.tar.gz: c9bec41adfcd267e6b728831b7668b0634a69edaf5a84785a0ec2c1d77f1892b09c7e5044416aac66cdc9039a46e09cc1e855e28c70e182fbc2bf287cf71623c
data/History.rdoc CHANGED
@@ -1,3 +1,13 @@
1
+ == 14.0.8 (2023-11-24)
2
+ * Support polymorphic_name (Vladimir Kochnev)
3
+ * Fix HasOneThrough association (kyori19)
4
+
5
+ == 14.0.7 (2023-11-04)
6
+ * Add support for Trilogy Adapter (Zack Mariscal)
7
+ * Add Dockerfile and Docker compose support (Zack Mariscal)
8
+ * Support polymorphic_name (Vladimir Kochnev)
9
+ * Fix HasOneThrough association (kyori19)
10
+
1
11
  == 14.0.6 (2023-02-04)
2
12
  * Port fix for #573 (Charlie Savage)
3
13
  * Port fix for fix #577 (Charlie Savage)
@@ -31,6 +41,9 @@ more frequently. (Andrew Kiellor)
31
41
  == 14.0.0 (2022-01-9)
32
42
  * Update to ActiveRecord 7.0 (Sammy Larbi)
33
43
 
44
+ ==13.0.8 (2023-11-24)
45
+ Fix ‘polymorphic_name` support (Vladimir Kochnev)
46
+
34
47
  == 13.0.7 (2023-02-04)
35
48
  * Fix #573 (Charlie Savage)
36
49
 
data/README.rdoc CHANGED
@@ -1,182 +1,182 @@
1
- = Composite Primary Keys for ActiveRecords
2
-
3
- == Summary
4
-
5
- ActiveRecord infamously doesn't support composite primary keys.
6
- This gem, composite_primary_keys, or CPK for short, extends ActiveRecord
7
- to support composite keys.
8
-
9
- == Installation
10
-
11
- gem install composite_primary_keys
12
-
13
- If you are using Rails add the following to your Gemfile:
14
-
15
- gem 'composite_primary_keys', '=x.x.x' (see next section about what version to use)
16
-
17
- == Versions
18
-
19
- Every major version of ActiveRecord has included numerous internal changes. As a result,
20
- CPK has to be rewritten for each version of ActiveRecord. To help keep
21
- things straight, here is the mapping:
22
-
23
- Version 14.x is designed to work with ActiveRecord 7.0.x
24
- Version 13.x is designed to work with ActiveRecord 6.1.x
25
- Version 12.x is designed to work with ActiveRecord 6.0.x
26
- Version 11.x is designed to work with ActiveRecord 5.2.x
27
- Version 10.x is designed to work with ActiveRecord 5.1.x
28
- Version 9.x is designed to work with ActiveRecord 5.0.x
29
- Version 8.x is designed to work with ActiveRecord 4.2.x
30
- Version 7.x is designed to work with ActiveRecord 4.1.x
31
- Version 6.x is designed to work with ActiveRecord 4.0.x
32
- Version 5.x is designed to work with ActiveRecord 3.2.x
33
- Version 4.x is designed to work with ActiveRecord 3.1.x
34
-
35
- Run the following command to list available versions:
36
-
37
- gem list composite_primary_keys -ra
38
-
39
- == The basics
40
-
41
- A model with composite primary keys is defined like this:
42
-
43
- class Membership < ActiveRecord::Base
44
- self.primary_keys = :user_id, :group_id
45
- belongs_to :user
46
- belongs_to :group
47
- has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
48
- end
49
-
50
- Note the addition of the line:
51
-
52
- self.primary_keys = :user_id, :group_id
53
-
54
-
55
- A model associated with a composite key model is defined like this:
56
-
57
- class MembershipStatus < ActiveRecord::Base
58
- belongs_to :membership, :foreign_key => [:user_id, :group_id]
59
- end
60
-
61
- That is, associations can include composite keys too. All Rails association types are supported. Nice.
62
-
63
- == Usage
64
-
65
- Once you’ve created your models to specify composite primary keys (such as the Membership class)
66
- and associations (such as MembershipStatus#membership), you can use them like any normal model
67
- with associations.
68
-
69
- But first, lets check out our primary keys.
70
-
71
- MembershipStatus.primary_key # => "id" # normal single key
72
- Membership.primary_key # => [:user_id, :group_id] # composite keys
73
- Membership.primary_key.to_s # => "user_id,group_id"
74
-
75
- Now we want to be able to find instances using the same syntax we always use for ActiveRecords.
76
-
77
- MembershipStatus.find(1) # single id returns single instance
78
- => <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
79
-
80
- Membership.find([1,1]) # composite ids returns single instance
81
- => <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}>
82
-
83
- Notice the use of an array to specify the composite key values.
84
-
85
- NOTE - API CHANGE. CPK Version 6.x and earlier used to allow composite keys to be listed out
86
- like this:
87
-
88
- Membership.find(1,1)
89
-
90
- This usage is no longer supported.
91
-
92
- == Databases
93
-
94
- CPK supports the following databases:
95
-
96
- * PostgreSQL
97
- * MySQL
98
- * MariaDB
99
- * Oracle
100
- * DB2
101
- * SQLite
102
- * SQLServer
103
-
104
- == Tests
105
-
106
- To run tests you first need to install the appropriate gems for the database you want to test. Database gems are
107
- divided into the following bundler groups:
108
-
109
- * mysql
110
- * oracle
111
- * postgresql
112
- * sqlite
113
- * sqlserver
114
-
115
- Since it is likely you do not have all the above databases installed on your computer, you want to install just the
116
- gems for your database. For example, to test postgresql you would install the appropriate gems like this:
117
-
118
- bundler config set --local without "mysql oracle sqlite sqlserver"
119
- bundler install
120
-
121
- Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
122
- command for each database. Using our example:
123
-
124
- rake postgresql:build_database
125
-
126
- You can also rebuild the database if it already exists using this command:
127
-
128
- rake postgresql:rebuild_database
129
-
130
- To get a list of commands for your database use:
131
-
132
- Rake -T
133
-
134
- Finally, to run tests:
135
-
136
- rake postgresql:test
137
-
138
- Travis build status: {<img src="https://travis-ci.com/composite-primary-keys/composite_primary_keys.svg" alt="Build Status" />}[https://travis-ci.com/composite-primary-keys/composite_primary_keys]
139
-
140
- === DB2
141
-
142
- DB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build. Thus tests
143
- have not been run against db2.
144
-
145
- === MariaDb (mysql)
146
-
147
- MariaDb is fully supported with all tests passing.
148
-
149
- === Oracle
150
-
151
- Oracle is fully supported with all tests passing.
152
-
153
- === Postgresql
154
-
155
- Postgresql is fully supported with all tests passing.
156
-
157
- === Sqlite 3
158
-
159
- The sqlite database is created at the path composite_primary_keys/db. Note you must *first* create the database using the
160
- built-in rake task before running tests:
161
-
162
- rake sqlite:build_database
163
-
164
- For sqlite3 to work correctly, you must manually require 'composite_primary_keys/connection_adapters/sqlite3_adapter' after
165
- loading the CPK gem.
166
-
167
- === SqlServer
168
-
169
- SqlServer is partially supported. There are a number of failing tests - patches welcomed.
170
-
171
- == Questions, Discussion and Contributions
172
-
173
- For help please visit https://github.com/composite-primary-keys/composite_primary_keys.
174
-
175
- == Author
176
-
177
- First version was written by Dr Nic Williams.
178
-
179
- Maintained by Charlie Savage
180
-
181
- Contributions by many!
182
-
1
+ = Composite Primary Keys for ActiveRecords
2
+
3
+ == Summary
4
+
5
+ ActiveRecord infamously doesn't support composite primary keys.
6
+ This gem, composite_primary_keys, or CPK for short, extends ActiveRecord
7
+ to support composite keys.
8
+
9
+ == Installation
10
+
11
+ gem install composite_primary_keys
12
+
13
+ If you are using Rails add the following to your Gemfile:
14
+
15
+ gem 'composite_primary_keys', '=x.x.x' (see next section about what version to use)
16
+
17
+ == Versions
18
+
19
+ Every major version of ActiveRecord has included numerous internal changes. As a result,
20
+ CPK has to be rewritten for each version of ActiveRecord. To help keep
21
+ things straight, here is the mapping:
22
+
23
+ Version 14.x is designed to work with ActiveRecord 7.0.x
24
+ Version 13.x is designed to work with ActiveRecord 6.1.x
25
+ Version 12.x is designed to work with ActiveRecord 6.0.x
26
+ Version 11.x is designed to work with ActiveRecord 5.2.x
27
+ Version 10.x is designed to work with ActiveRecord 5.1.x
28
+ Version 9.x is designed to work with ActiveRecord 5.0.x
29
+ Version 8.x is designed to work with ActiveRecord 4.2.x
30
+ Version 7.x is designed to work with ActiveRecord 4.1.x
31
+ Version 6.x is designed to work with ActiveRecord 4.0.x
32
+ Version 5.x is designed to work with ActiveRecord 3.2.x
33
+ Version 4.x is designed to work with ActiveRecord 3.1.x
34
+
35
+ Run the following command to list available versions:
36
+
37
+ gem list composite_primary_keys -ra
38
+
39
+ == The basics
40
+
41
+ A model with composite primary keys is defined like this:
42
+
43
+ class Membership < ActiveRecord::Base
44
+ self.primary_keys = :user_id, :group_id
45
+ belongs_to :user
46
+ belongs_to :group
47
+ has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
48
+ end
49
+
50
+ Note the addition of the line:
51
+
52
+ self.primary_keys = :user_id, :group_id
53
+
54
+
55
+ A model associated with a composite key model is defined like this:
56
+
57
+ class MembershipStatus < ActiveRecord::Base
58
+ belongs_to :membership, :foreign_key => [:user_id, :group_id]
59
+ end
60
+
61
+ That is, associations can include composite keys too. All Rails association types are supported. Nice.
62
+
63
+ == Usage
64
+
65
+ Once you’ve created your models to specify composite primary keys (such as the Membership class)
66
+ and associations (such as MembershipStatus#membership), you can use them like any normal model
67
+ with associations.
68
+
69
+ But first, lets check out our primary keys.
70
+
71
+ MembershipStatus.primary_key # => "id" # normal single key
72
+ Membership.primary_key # => [:user_id, :group_id] # composite keys
73
+ Membership.primary_key.to_s # => "user_id,group_id"
74
+
75
+ Now we want to be able to find instances using the same syntax we always use for ActiveRecords.
76
+
77
+ MembershipStatus.find(1) # single id returns single instance
78
+ => <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
79
+
80
+ Membership.find([1,1]) # composite ids returns single instance
81
+ => <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}>
82
+
83
+ Notice the use of an array to specify the composite key values.
84
+
85
+ NOTE - API CHANGE. CPK Version 6.x and earlier used to allow composite keys to be listed out
86
+ like this:
87
+
88
+ Membership.find(1,1)
89
+
90
+ This usage is no longer supported.
91
+
92
+ == Databases
93
+
94
+ CPK supports the following databases:
95
+
96
+ * PostgreSQL
97
+ * MySQL
98
+ * MariaDB
99
+ * Oracle
100
+ * DB2
101
+ * SQLite
102
+ * SQLServer
103
+
104
+ == Tests
105
+
106
+ To run tests you first need to install the appropriate gems for the database you want to test. Database gems are
107
+ divided into the following bundler groups:
108
+
109
+ * mysql
110
+ * oracle
111
+ * postgresql
112
+ * sqlite
113
+ * sqlserver
114
+
115
+ Since it is likely you do not have all the above databases installed on your computer, you want to install just the
116
+ gems for your database. For example, to test postgresql you would install the appropriate gems like this:
117
+
118
+ bundler config set --local without "mysql oracle sqlite sqlserver"
119
+ bundler install
120
+
121
+ Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
122
+ command for each database. Using our example:
123
+
124
+ rake postgresql:build_database
125
+
126
+ You can also rebuild the database if it already exists using this command:
127
+
128
+ rake postgresql:rebuild_database
129
+
130
+ To get a list of commands for your database use:
131
+
132
+ Rake -T
133
+
134
+ Finally, to run tests:
135
+
136
+ rake postgresql:test
137
+
138
+ Travis build status: {<img src="https://travis-ci.com/composite-primary-keys/composite_primary_keys.svg" alt="Build Status" />}[https://travis-ci.com/composite-primary-keys/composite_primary_keys]
139
+
140
+ === DB2
141
+
142
+ DB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build. Thus tests
143
+ have not been run against db2.
144
+
145
+ === MariaDb (mysql)
146
+
147
+ MariaDb is fully supported with all tests passing.
148
+
149
+ === Oracle
150
+
151
+ Oracle is fully supported with all tests passing.
152
+
153
+ === Postgresql
154
+
155
+ Postgresql is fully supported with all tests passing.
156
+
157
+ === Sqlite 3
158
+
159
+ The sqlite database is created at the path composite_primary_keys/db. Note you must *first* create the database using the
160
+ built-in rake task before running tests:
161
+
162
+ rake sqlite:build_database
163
+
164
+ For sqlite3 to work correctly, you must manually require 'composite_primary_keys/connection_adapters/sqlite3_adapter' after
165
+ loading the CPK gem.
166
+
167
+ === SqlServer
168
+
169
+ SqlServer is partially supported. There are a number of failing tests - patches welcomed.
170
+
171
+ == Questions, Discussion and Contributions
172
+
173
+ For help please visit https://github.com/composite-primary-keys/composite_primary_keys.
174
+
175
+ == Author
176
+
177
+ First version was written by Dr Nic Williams.
178
+
179
+ Maintained by Charlie Savage
180
+
181
+ Contributions by many!
182
+
data/Rakefile CHANGED
@@ -23,7 +23,7 @@ Dir.glob('tasks/**/*.rake').each do |rake_file|
23
23
  end
24
24
 
25
25
  # Set up test tasks for each supported connection adapter
26
- %w(mysql sqlite oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
26
+ %w(mysql sqlite oracle oracle_enhanced postgresql ibm_db sqlserver trilogy).each do |adapter|
27
27
  namespace adapter do
28
28
  desc "Run tests using the #{adapter} adapter"
29
29
  task "test" do
@@ -11,8 +11,8 @@ module ActiveRecord
11
11
  attributes[key1] = owner[key2]
12
12
  end
13
13
 
14
- if reflection.options[:as]
15
- attributes[reflection.type] = owner.class.base_class.name
14
+ if reflection.type
15
+ attributes[reflection.type] = owner.class.polymorphic_name
16
16
  end
17
17
  end
18
18
 
@@ -64,4 +64,4 @@ module ActiveRecord
64
64
  end
65
65
  end
66
66
  end
67
- end
67
+ end
@@ -1,39 +1,39 @@
1
- module CompositePrimaryKeys
2
- module CollectionAssociation
3
- def ids_writer(ids)
4
- primary_key = reflection.association_primary_key
5
- ids = Array(ids).reject(&:blank?)
6
-
7
- # CPK-
8
- if primary_key.is_a?(Array)
9
- ids = ids.map { |id| CompositePrimaryKeys::CompositeKeys.parse(id) }
10
- primary_key.each_with_index do |key, i|
11
- pk_type = klass.type_for_attribute(key)
12
- ids.each { |id| id[i] = pk_type.cast(id[i]) }
13
- end
14
-
15
- predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
16
- records = klass.where(predicate).index_by do |r|
17
- reflection.association_primary_key.map{ |k| r.send(k) }
18
- end.values_at(*ids).compact
19
- else
20
- pk_type = klass.type_for_attribute(primary_key)
21
- ids.map! { |i| pk_type.cast(i) }
22
-
23
- records = klass.where(primary_key => ids).index_by do |r|
24
- r.public_send(primary_key)
25
- end.values_at(*ids).compact
26
- end
27
-
28
- if records.size != ids.size
29
- found_ids = records.map { |record| record.public_send(primary_key) }
30
- not_found_ids = ids - found_ids
31
- klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
32
- else
33
- replace(records)
34
- end
35
- end
36
- end
37
- end
38
-
1
+ module CompositePrimaryKeys
2
+ module CollectionAssociation
3
+ def ids_writer(ids)
4
+ primary_key = reflection.association_primary_key
5
+ ids = Array(ids).reject(&:blank?)
6
+
7
+ # CPK-
8
+ if primary_key.is_a?(Array)
9
+ ids = ids.map { |id| CompositePrimaryKeys::CompositeKeys.parse(id) }
10
+ primary_key.each_with_index do |key, i|
11
+ pk_type = klass.type_for_attribute(key)
12
+ ids.each { |id| id[i] = pk_type.cast(id[i]) }
13
+ end
14
+
15
+ predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
16
+ records = klass.where(predicate).index_by do |r|
17
+ reflection.association_primary_key.map{ |k| r.send(k) }
18
+ end.values_at(*ids).compact
19
+ else
20
+ pk_type = klass.type_for_attribute(primary_key)
21
+ ids.map! { |i| pk_type.cast(i) }
22
+
23
+ records = klass.where(primary_key => ids).index_by do |r|
24
+ r.public_send(primary_key)
25
+ end.values_at(*ids).compact
26
+ end
27
+
28
+ if records.size != ids.size
29
+ found_ids = records.map { |record| record.public_send(primary_key) }
30
+ not_found_ids = ids - found_ids
31
+ klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
32
+ else
33
+ replace(records)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
39
  ActiveRecord::Associations::CollectionAssociation.prepend CompositePrimaryKeys::CollectionAssociation
@@ -23,6 +23,25 @@ module ActiveRecord
23
23
  end
24
24
  end
25
25
  end
26
+
27
+ alias :original_construct_join_attributes :construct_join_attributes
28
+
29
+ def construct_join_attributes(*records)
30
+ # CPK
31
+ if !self.source_reflection.polymorphic? && source_reflection.klass.composite?
32
+ ensure_mutable
33
+
34
+ ids = records.map do |record|
35
+ source_reflection.association_primary_key(reflection.klass).map do |key|
36
+ record.send(key)
37
+ end
38
+ end
39
+
40
+ cpk_in_predicate(through_association.scope.klass.arel_table, source_reflection.foreign_key, ids)
41
+ else
42
+ original_construct_join_attributes(*records)
43
+ end
44
+ end
26
45
  end
27
46
  end
28
47
  end
@@ -1,52 +1,52 @@
1
- module ActiveRecord
2
- module Associations
3
- class Preloader
4
- class Association
5
-
6
- class LoaderQuery
7
- def load_records_for_keys(keys, &block)
8
- # CPK
9
- if association_key_name.is_a?(Array)
10
- predicate = cpk_in_predicate(scope.klass.arel_table, association_key_name, keys)
11
- scope.where(predicate).load(&block)
12
- else
13
- scope.where(association_key_name => keys).load(&block)
14
- end
15
- end
16
- end
17
-
18
- # TODO: is records_for needed anymore? Rails' implementation has changed significantly
19
- def records_for(ids)
20
- records = if association_key_name.is_a?(Array)
21
- predicate = cpk_in_predicate(klass.arel_table, association_key_name, ids)
22
- scope.where(predicate)
23
- else
24
- scope.where(association_key_name => ids)
25
- end
26
- records.load do |record|
27
- # Processing only the first owner
28
- # because the record is modified but not an owner
29
- owner = owners_by_key[convert_key(record[association_key_name])].first
30
- association = owner.association(reflection.name)
31
- association.set_inverse_instance(record)
32
- end
33
- end
34
-
35
- def owners_by_key
36
- @owners_by_key ||= owners.each_with_object({}) do |owner, result|
37
- # CPK
38
- # key = convert_key(owner[owner_key_name])
39
- key = if owner_key_name.is_a?(Array)
40
- Array(owner_key_name).map do |key_name|
41
- convert_key(owner[key_name])
42
- end
43
- else
44
- convert_key(owner[owner_key_name])
45
- end
46
- (result[key] ||= []) << owner if key
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end
1
+ module ActiveRecord
2
+ module Associations
3
+ class Preloader
4
+ class Association
5
+
6
+ class LoaderQuery
7
+ def load_records_for_keys(keys, &block)
8
+ # CPK
9
+ if association_key_name.is_a?(Array)
10
+ predicate = cpk_in_predicate(scope.klass.arel_table, association_key_name, keys)
11
+ scope.where(predicate).load(&block)
12
+ else
13
+ scope.where(association_key_name => keys).load(&block)
14
+ end
15
+ end
16
+ end
17
+
18
+ # TODO: is records_for needed anymore? Rails' implementation has changed significantly
19
+ def records_for(ids)
20
+ records = if association_key_name.is_a?(Array)
21
+ predicate = cpk_in_predicate(klass.arel_table, association_key_name, ids)
22
+ scope.where(predicate)
23
+ else
24
+ scope.where(association_key_name => ids)
25
+ end
26
+ records.load do |record|
27
+ # Processing only the first owner
28
+ # because the record is modified but not an owner
29
+ owner = owners_by_key[convert_key(record[association_key_name])].first
30
+ association = owner.association(reflection.name)
31
+ association.set_inverse_instance(record)
32
+ end
33
+ end
34
+
35
+ def owners_by_key
36
+ @owners_by_key ||= owners.each_with_object({}) do |owner, result|
37
+ # CPK
38
+ # key = convert_key(owner[owner_key_name])
39
+ key = if owner_key_name.is_a?(Array)
40
+ Array(owner_key_name).map do |key_name|
41
+ convert_key(owner[key_name])
42
+ end
43
+ else
44
+ convert_key(owner[owner_key_name])
45
+ end
46
+ (result[key] ||= []) << owner if key
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end