activerecord_where_assoc 1.1.4 → 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: 4d915a4b0f214283acc2a6ba098018b17fc786f2b801eb2703eba31448b127b9
4
- data.tar.gz: 614b3c59444a0f6676b63f2499718b16f95ac1d1b010e18e322b10d25be21e1a
3
+ metadata.gz: 7ef06d95cd1fa0b3de7d06e745fc1a527bdc86415adb2e6b36b86b1f1477e804
4
+ data.tar.gz: 36a723c284fbb1e0b44ea9af5368995d504a53026817f545a14235212d4ae7bf
5
5
  SHA512:
6
- metadata.gz: 1baf41c6ae6094045794f1c5a01c4ff3ab1218d9bc027b51e038667a76d210aa7c8819a1eb61404032fb906233400a9da2f7112a94e0610e5b546c1ef7373934
7
- data.tar.gz: a1fbc573c244b8ea12cf8bd96b0de0ab248bc14ef5eeb5f1503715951e746ccae9e103fa98baee2f666c971496732bd4f938ece8d77cea6770ad1614d3feed43
6
+ metadata.gz: 8a26660a18b817b6a5cd106e71413e064f899e6fb9c73e9f502a14fc5aeb2896412368bc932028300611c94adac234ebbff85d0e2fd3ed8b900c01fa6c5e2670
7
+ data.tar.gz: febe936611a53bc9f2f75aeb639fe8d8b905304b9b910e71f5cc9020ab1c0fae6803cd303f0cd7b830e29aa092778df21ce0303150f876930a02be2ed831821e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
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
+
7
+ # 1.1.5 - 2024-05-18
8
+
9
+ * Add compatibility for Rails 7.2
10
+
3
11
  # 1.1.4 - 2023-10-10
4
12
 
5
13
  * Add compatibility for Rails 7.1
@@ -18,12 +26,12 @@
18
26
 
19
27
  # 1.1.0 - 2020-02-24
20
28
 
21
- * 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`
22
30
  [Documentation for them](https://maxlap.github.io/activerecord_where_assoc/ActiveRecordWhereAssoc/SqlReturningMethods.html)
23
31
 
24
32
  # 1.0.1
25
33
 
26
- * Fix broken urls in error messages
34
+ * Fix broken urls in error messages
27
35
 
28
36
  # 1.0.0
29
37
 
@@ -31,16 +39,16 @@
31
39
 
32
40
  # 0.1.3
33
41
 
34
- * Use `SELECT 1` instead of `SELECT 0`...
42
+ * Use `SELECT 1` instead of `SELECT 0`...
35
43
  ... it just seems more natural that way.
36
44
  * Bugfixes
37
45
 
38
46
  # 0.1.2
39
47
 
40
- * 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`.
41
49
  Ex: Users that have between 10 and 20 posts
42
50
  `User.where_assoc_count(10..20, :==, :posts)`
43
- The operator in that case must be either :== or :!=.
44
- 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`.
45
53
  Ranges that exclude the last value, i.e. `5...10`, are also supported, resulting in `BETWEEN 5 and 9`.
46
54
  Ranges with infinities are also supported.
data/EXAMPLES.md CHANGED
@@ -1,4 +1,3 @@
1
- SELECT "users".* FROM "users"
2
1
  Here are some example usages of the gem, along with the generated SQL.
3
2
 
4
3
  Each of those methods can be chained with scoping methods, so they can be used on `Post`, `my_user.posts`, `Post.where('hello')` or inside a scope. Note that for the `*_sql` variants, those should preferably be used on classes only, because otherwise, it could be confusing for a reader.
@@ -72,7 +72,7 @@ module ActiveRecordWhereAssoc
72
72
  end
73
73
  end
74
74
 
75
- if ActiveRecord.gem_version >= Gem::Version.new("4.2")
75
+ if ActiveRecord.gem_version >= Gem::Version.new("4.2") && ActiveRecord.gem_version < Gem::Version.new("7.2.0.alpha")
76
76
  def self.normalize_association_name(association_name)
77
77
  association_name.to_s
78
78
  end
@@ -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.4".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.4
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: 2023-10-10 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: []