activerecord_where_assoc 1.1.5 → 1.2.0

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: 366ed8e028f8ed692b13cd8c487385ec380c57f41ce51edcd3370a2412ffe8f3
4
- data.tar.gz: c6d2c1b2111cda267e7c5c6555692317ad0188cd748972f262acbdcba02eee7e
3
+ metadata.gz: 7ef06d95cd1fa0b3de7d06e745fc1a527bdc86415adb2e6b36b86b1f1477e804
4
+ data.tar.gz: 36a723c284fbb1e0b44ea9af5368995d504a53026817f545a14235212d4ae7bf
5
5
  SHA512:
6
- metadata.gz: 1dd9b3955c50ec5ed37ca032b7aa20d93eac2ab035a914e1032efe1527a7b86cd33cd3832c3fa68010bfafef4780f2dfba473e99fa0f089bb3649c8aed578c27
7
- data.tar.gz: 6bae922127f45e21c2484c3ef28f0d47512cb1d2cc0b1e94a97a628277698eceb04595b3f96759d367be1e94e4441ad72e8e2901aabf44169994989579b68d00
6
+ metadata.gz: 8a26660a18b817b6a5cd106e71413e064f899e6fb9c73e9f502a14fc5aeb2896412368bc932028300611c94adac234ebbff85d0e2fd3ed8b900c01fa6c5e2670
7
+ data.tar.gz: febe936611a53bc9f2f75aeb639fe8d8b905304b9b910e71f5cc9020ab1c0fae6803cd303f0cd7b830e29aa092778df21ce0303150f876930a02be2ed831821e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Unreleased
2
2
 
3
+ # 1.2.0 - 2024-08-31
4
+
5
+ * Add support for composite primary keys in Rails 7.2
6
+
3
7
  # 1.1.5 - 2024-05-18
4
8
 
5
9
  * Add compatibility for Rails 7.2
@@ -22,12 +26,12 @@
22
26
 
23
27
  # 1.1.0 - 2020-02-24
24
28
 
25
- * Added methods which return the SQL used by this gem: `assoc_exists_sql`, `assoc_not_exists_sql`, `compare_assoc_count_sql`, `only_assoc_count_sql`
29
+ * Added methods which return the SQL used by this gem: `assoc_exists_sql`, `assoc_not_exists_sql`, `compare_assoc_count_sql`, `only_assoc_count_sql`
26
30
  [Documentation for them](https://maxlap.github.io/activerecord_where_assoc/ActiveRecordWhereAssoc/SqlReturningMethods.html)
27
31
 
28
32
  # 1.0.1
29
33
 
30
- * Fix broken urls in error messages
34
+ * Fix broken urls in error messages
31
35
 
32
36
  # 1.0.0
33
37
 
@@ -35,16 +39,16 @@
35
39
 
36
40
  # 0.1.3
37
41
 
38
- * Use `SELECT 1` instead of `SELECT 0`...
42
+ * Use `SELECT 1` instead of `SELECT 0`...
39
43
  ... it just seems more natural that way.
40
44
  * Bugfixes
41
45
 
42
46
  # 0.1.2
43
47
 
44
- * It is now possible to pass a `Range` as first argument to `#where_assoc_count`.
48
+ * It is now possible to pass a `Range` as first argument to `#where_assoc_count`.
45
49
  Ex: Users that have between 10 and 20 posts
46
50
  `User.where_assoc_count(10..20, :==, :posts)`
47
- The operator in that case must be either :== or :!=.
48
- This will use `BETWEEN` and `NOT BETWEEN`.
51
+ The operator in that case must be either :== or :!=.
52
+ This will use `BETWEEN` and `NOT BETWEEN`.
49
53
  Ranges that exclude the last value, i.e. `5...10`, are also supported, resulting in `BETWEEN 5 and 9`.
50
54
  Ranges with infinities are also supported.
@@ -364,6 +364,9 @@ module ActiveRecordWhereAssoc
364
364
  if reflection.klass.table_name.include?(".") || option_value(options, :never_alias_limit)
365
365
  # We use unscoped to avoid duplicating the conditions in the query, which is noise. (unless it
366
366
  # could helps the query planner of the DB, if someone can show it to be worth it, then this can be changed.)
367
+ if reflection.klass.primary_key.is_a?(Array)
368
+ raise NeverAliasLimitDoesntWorkWithCompositePrimaryKeysError, "Sorry, it just doesn't work..."
369
+ end
367
370
 
368
371
  reflection.klass.unscoped.where(reflection.klass.primary_key.to_sym => current_scope)
369
372
  else
@@ -395,8 +398,13 @@ module ActiveRecordWhereAssoc
395
398
 
396
399
  alias_scope = foreign_klass.base_class.unscoped
397
400
  alias_scope = alias_scope.from("#{table.name} #{ALIAS_TABLE.name}")
398
- alias_scope = alias_scope.where(table[primary_key].eq(ALIAS_TABLE[primary_key]))
399
- alias_scope
401
+
402
+ primary_key_constraints =
403
+ Array(primary_key).map do |a_primary_key|
404
+ table[a_primary_key].eq(ALIAS_TABLE[a_primary_key])
405
+ end
406
+
407
+ alias_scope.where(primary_key_constraints.inject(&:and))
400
408
  end
401
409
 
402
410
  def self.wrapper_and_join_constraints(record_class, reflection, options = {})
@@ -424,7 +432,18 @@ module ActiveRecordWhereAssoc
424
432
  foreign_table = ALIAS_TABLE
425
433
  end
426
434
 
427
- constraints = table[key].eq(foreign_table[foreign_key])
435
+ constraint_keys = Array.wrap(key)
436
+ constraint_foreign_keys = Array.wrap(foreign_key)
437
+ constraint_key_map = constraint_keys.zip(constraint_foreign_keys)
438
+
439
+ primary_foreign_key_constraints =
440
+ constraint_key_map.map do |primary_and_foreign_keys|
441
+ a_primary_key, a_foreign_key = primary_and_foreign_keys
442
+
443
+ table[a_primary_key].eq(foreign_table[a_foreign_key])
444
+ end
445
+
446
+ constraints = primary_foreign_key_constraints.inject(&:and)
428
447
 
429
448
  if reflection.type
430
449
  # Handling of the polymorphic has_many/has_one's type column
@@ -6,4 +6,7 @@ module ActiveRecordWhereAssoc
6
6
 
7
7
  class PolymorphicBelongsToWithoutClasses < StandardError
8
8
  end
9
+
10
+ class NeverAliasLimitDoesntWorkWithCompositePrimaryKeysError < StandardError
11
+ end
9
12
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordWhereAssoc
4
- VERSION = "1.1.5".freeze
4
+ VERSION = "1.2.0".freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_where_assoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxime Handfield Lapointe
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-18 00:00:00.000000000 Z
11
+ date: 2024-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -181,7 +181,7 @@ homepage: https://github.com/MaxLap/activerecord_where_assoc
181
181
  licenses:
182
182
  - MIT
183
183
  metadata: {}
184
- post_install_message:
184
+ post_install_message:
185
185
  rdoc_options: []
186
186
  require_paths:
187
187
  - lib
@@ -196,8 +196,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
196
  - !ruby/object:Gem::Version
197
197
  version: '0'
198
198
  requirements: []
199
- rubygems_version: 3.4.10
200
- signing_key:
199
+ rubygems_version: 3.5.11
200
+ signing_key:
201
201
  specification_version: 4
202
202
  summary: Make ActiveRecord do conditions on your associations
203
203
  test_files: []