composite_primary_keys 14.0.7 → 14.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de97dfff0f237f22dd32a32189824739087b30c0ea0e14cdd996bd8b69359caf
4
- data.tar.gz: 722a9e0d02b6fa8ee2010d47e5a37705a43b302df6abf7c7a6d199b0a84b0201
3
+ metadata.gz: d4675084d63630478cb5ad93843fe68c06e1820cc228fd8a1f95338851b901bd
4
+ data.tar.gz: ed8a13dcf2aee66e3fcf20f89e14049660eb7ee00341f9967a95cfd23dbbd259
5
5
  SHA512:
6
- metadata.gz: e57b5330dc2b0cdb7374f14ed6c7efed22c269cf42298527efc1ec26ee2e2ea8ab9edd97e97a7f4c656662e54a1faa5da28fef365c602060878d375e4caa646a
7
- data.tar.gz: c2981385774399cdd3e64385d9f7595dd2a3972706b04e825b604f83ed4fe59438df2f114f196ff0fb6ceb389060554259cab7132c2a75073a382101da533bd0
6
+ metadata.gz: 18089b861f3fd8bf7d6adf998c92235f34e3e786aa7de12590988f68580a63eb0551e338b59cc491e6d322a429e143f67245bebc85137d11f5bc087faa5abbfe
7
+ data.tar.gz: 5bb22276acefd46ed09e078bb999df86d7e1885304b855c7e6493733d13f859cd9a31b58177c971825d874b57fb5388d33dc6d3f715d58aa344b1f3f8e415f67
data/History.rdoc CHANGED
@@ -1,3 +1,10 @@
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
+
1
8
  == 14.0.7 (2023-11-04)
2
9
  * Add support for Trilogy Adapter (Zack Mariscal)
3
10
  * Add Dockerfile and Docker compose support (Zack Mariscal)
@@ -37,6 +44,9 @@ more frequently. (Andrew Kiellor)
37
44
  == 14.0.0 (2022-01-9)
38
45
  * Update to ActiveRecord 7.0 (Sammy Larbi)
39
46
 
47
+ ==13.0.8 (2023-11-24)
48
+ Fix ‘polymorphic_name` support (Vladimir Kochnev)
49
+
40
50
  == 13.0.7 (2023-02-04)
41
51
  * Fix #573 (Charlie Savage)
42
52
 
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
+
@@ -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
@@ -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