composite_primary_keys 14.0.3 → 14.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +49 -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 +10 -3
  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 -68
  10. data/lib/composite_primary_keys/autosave_association.rb +1 -1
  11. data/lib/composite_primary_keys/composite_arrays.rb +2 -0
  12. data/lib/composite_primary_keys/composite_predicates.rb +121 -71
  13. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +1 -2
  14. data/lib/composite_primary_keys/nested_attributes.rb +2 -2
  15. data/lib/composite_primary_keys/persistence.rb +96 -96
  16. data/lib/composite_primary_keys/relation/calculations.rb +110 -110
  17. data/lib/composite_primary_keys/relation/query_methods.rb +14 -16
  18. data/lib/composite_primary_keys/relation.rb +4 -2
  19. data/lib/composite_primary_keys/validations/uniqueness.rb +10 -2
  20. data/lib/composite_primary_keys/version.rb +1 -1
  21. data/lib/composite_primary_keys.rb +117 -119
  22. data/scripts/console.rb +2 -2
  23. data/tasks/databases/trilogy.rake +23 -0
  24. data/test/abstract_unit.rb +124 -118
  25. data/test/connections/databases.ci.yml +10 -0
  26. data/test/fixtures/admin.rb +4 -0
  27. data/test/fixtures/comments.yml +6 -0
  28. data/test/fixtures/db_definitions/db2-create-tables.sql +34 -0
  29. data/test/fixtures/db_definitions/db2-drop-tables.sql +7 -1
  30. data/test/fixtures/db_definitions/mysql.sql +23 -0
  31. data/test/fixtures/db_definitions/oracle.drop.sql +4 -0
  32. data/test/fixtures/db_definitions/oracle.sql +21 -0
  33. data/test/fixtures/db_definitions/postgresql.sql +23 -0
  34. data/test/fixtures/db_definitions/sqlite.sql +21 -0
  35. data/test/fixtures/db_definitions/sqlserver.sql +23 -0
  36. data/test/fixtures/department.rb +20 -16
  37. data/test/fixtures/moderator.rb +4 -0
  38. data/test/fixtures/room.rb +4 -1
  39. data/test/fixtures/room_assignment.rb +6 -2
  40. data/test/fixtures/staff_room.rb +6 -0
  41. data/test/fixtures/staff_room_key.rb +6 -0
  42. data/test/fixtures/user.rb +3 -0
  43. data/test/fixtures/user_with_polymorphic_name.rb +9 -0
  44. data/test/test_associations.rb +403 -372
  45. data/test/test_composite_arrays.rb +6 -0
  46. data/test/test_create.rb +219 -218
  47. data/test/test_has_one_through.rb +30 -0
  48. data/test/test_nested_attributes.rb +23 -0
  49. data/test/test_polymorphic.rb +6 -0
  50. data/test/test_predicates.rb +130 -60
  51. metadata +13 -6
  52. 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: c9cb00a73781e630f40a83391449a048e05a28e6c93ab41b71af55ad7d0e5aa1
4
- data.tar.gz: ef9cdf02f59b135080e2eefa621d35764a7a4002904aaa066031675b2bf05873
3
+ metadata.gz: d4675084d63630478cb5ad93843fe68c06e1820cc228fd8a1f95338851b901bd
4
+ data.tar.gz: ed8a13dcf2aee66e3fcf20f89e14049660eb7ee00341f9967a95cfd23dbbd259
5
5
  SHA512:
6
- metadata.gz: 422d8d5bc43d9b477e2d8dee49235580ff532c961ce1947ac8a09e47b13b2e6eaa484e0ee8045f4fc9bdebf1c0ffb50ca197e243a4d8e93c43909d16640a8433
7
- data.tar.gz: 7cc5b56bbb3749d9edf5aef12d4e231e07496eb5f76a605cb7de436b99958a7b6a9aaab9160bdf51b43404b15175a7c93ee02ce6d00d5140e92a68bd7b42f0cc
6
+ metadata.gz: 18089b861f3fd8bf7d6adf998c92235f34e3e786aa7de12590988f68580a63eb0551e338b59cc491e6d322a429e143f67245bebc85137d11f5bc087faa5abbfe
7
+ data.tar.gz: 5bb22276acefd46ed09e078bb999df86d7e1885304b855c7e6493733d13f859cd9a31b58177c971825d874b57fb5388d33dc6d3f715d58aa344b1f3f8e415f67
data/History.rdoc CHANGED
@@ -1,3 +1,35 @@
1
+ == 14.0.9 (2023-12-03)
2
+ * Fix Error for Array-Based Composite Keys in nested_attribute (T.S.)
3
+
4
+ == 14.0.8 (2023-11-24)
5
+ * Support polymorphic_name (Vladimir Kochnev)
6
+ * Fix HasOneThrough association (kyori19)
7
+
8
+ == 14.0.7 (2023-11-04)
9
+ * Add support for Trilogy Adapter (Zack Mariscal)
10
+ * Add Dockerfile and Docker compose support (Zack Mariscal)
11
+ * Support polymorphic_name (Vladimir Kochnev)
12
+ * Fix HasOneThrough association (kyori19)
13
+
14
+ == 14.0.6 (2023-02-04)
15
+ * Port fix for #573 (Charlie Savage)
16
+ * Port fix for fix #577 (Charlie Savage)
17
+
18
+ == 14.0.5 (2023-02-04)
19
+ * Improve query generation for cpk_in_predicate. This reduces the length of
20
+ queries when loading many keys and enables Postgres to use index scans
21
+ more frequently. (Andrew Kiellor)
22
+ * Add Ruby 3.2 to CI and update checkout action versions (Peter Goldstein)
23
+ * Fix grammatical correction in README.rdoc (Sam Corl)
24
+ * Add an assertion for previously_new_record? (Akinori Musha)
25
+ * Fix validate_each (Benjamin Fleischer)
26
+ * Reduce ambiguity by avoiding #normalize (Mitsuhiro Shibuya)
27
+ * Accept strings in has_many ids assignment (Mitsuhiro Shibuya)
28
+ * Make CompositeKeys respond to #to_param consistently with ActiveRecord::Base (Mitsuhiro Shibuya)
29
+
30
+ == 14.0.4 (2022-02-13)
31
+ * Fix for changed method in Rails 7.0.2 (Yota)
32
+
1
33
  == 14.0.3 (2022-01-09)
2
34
  * Remove override on ActiveRecord::Base#to_param. That method has moved to Integration
3
35
  so no longer works. #541. (Charlie Savage)
@@ -12,6 +44,23 @@
12
44
  == 14.0.0 (2022-01-9)
13
45
  * Update to ActiveRecord 7.0 (Sammy Larbi)
14
46
 
47
+ ==13.0.8 (2023-11-24)
48
+ Fix ‘polymorphic_name` support (Vladimir Kochnev)
49
+
50
+ == 13.0.7 (2023-02-04)
51
+ * Fix #573 (Charlie Savage)
52
+
53
+ == 13.0.6 (2023-02-04)
54
+ * Fix #577 (Charlie Savage)
55
+
56
+ == 13.0.5 (2023-02-04)
57
+ * Improve query generation for cpk_in_predicate. This reduces the length of
58
+ queries when loading many keys and enables Postgres to use index scans
59
+ more frequently. (Andrew Kiellor)
60
+
61
+ == 13.0.4 (2022-12-05)
62
+ * Fix previously_new_record? not being set to true after create (Akinori MUSHA)
63
+
15
64
  == 13.0.3 (2022-01-09)
16
65
  * Remove override on ActiveRecord::Base#to_param. That method has moved to Integration
17
66
  so no longer works. #541. (Charlie Savage)
data/README.rdoc CHANGED
@@ -1,182 +1,182 @@
1
- = Composite Primary Keys for ActiveRecords
2
-
3
- == Summary
4
-
5
- ActiveRecords 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
@@ -2,17 +2,24 @@ module CompositePrimaryKeys
2
2
  module CollectionAssociation
3
3
  def ids_writer(ids)
4
4
  primary_key = reflection.association_primary_key
5
- pk_type = klass.type_for_attribute(primary_key)
6
5
  ids = Array(ids).reject(&:blank?)
7
- ids.map! { |i| pk_type.cast(i) }
8
6
 
9
7
  # CPK-
10
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
+
11
15
  predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
12
16
  records = klass.where(predicate).index_by do |r|
13
17
  reflection.association_primary_key.map{ |k| r.send(k) }
14
- end.values_at(*ids)
18
+ end.values_at(*ids).compact
15
19
  else
20
+ pk_type = klass.type_for_attribute(primary_key)
21
+ ids.map! { |i| pk_type.cast(i) }
22
+
16
23
  records = klass.where(primary_key => ids).index_by do |r|
17
24
  r.public_send(primary_key)
18
25
  end.values_at(*ids).compact
@@ -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,68 +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
-
50
- # TODO: is records_by_owner needed anymore? Rails' implementation has changed significantly
51
- def records_by_owner
52
- @records_by_owner ||= preloaded_records.each_with_object({}) do |record, result|
53
- key = if association_key_name.is_a?(Array)
54
- Array(record[association_key_name]).map do |key|
55
- convert_key(key)
56
- end
57
- else
58
- convert_key(record[association_key_name])
59
- end
60
- owners_by_key[key].each do |owner|
61
- (result[owner] ||= []) << record
62
- end
63
- end
64
- end
65
- end
66
- end
67
- end
68
- 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
@@ -14,7 +14,7 @@ module ActiveRecord
14
14
  #key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
15
15
  key = reflection.options[:primary_key] ? self[reflection.options[:primary_key]] : id
16
16
 
17
- if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key)
17
+ if (autosave && record.changed_for_autosave?) || new_record? || _record_changed?(reflection, record, key)
18
18
  unless reflection.through_reflection
19
19
  record[reflection.foreign_key] = key
20
20
  if inverse_reflection = reflection.inverse_of
@@ -53,6 +53,8 @@ module CompositePrimaryKeys
53
53
  # Doing this makes it easier to parse Base#[](attr_name)
54
54
  map { |key| Utils.escape_string_key(key.to_s) }.join(ID_SEP)
55
55
  end
56
+
57
+ alias_method :to_param, :to_s
56
58
  end
57
59
 
58
60
  module Utils