where_exists 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6107147002f4b860ab5570fc7f23a5fc38ed39425b38c581b1951394986c925c
4
- data.tar.gz: c7ed7a2d3432032a0c719e8298293ba38d875992b7abe76832654678023eccc5
3
+ metadata.gz: d4749154c4976b45791c28a6e55d69016f57b09ccc47c0ada04d438d7851edf3
4
+ data.tar.gz: 67112fa90f4536950ebad7cad399dd2c5ad5cd6c9effc8758c21c22f637c3ccf
5
5
  SHA512:
6
- metadata.gz: c4cd31754b8044d2ad4ff7d55e84bd3959fa742a4527c0955994e3ba2797a1f34b6ab1bcd5fb347543fde2d8fc5d2e97e633a13bbdd26b985cb039b45ab220c2
7
- data.tar.gz: 419074f831ceac51d19aaf53cda7ffc4ebd8d14e4246f5439702e251ab424068c30f9e4f8fc5344235fb03848cd1d4a92452c7007df2d35be399181b66cd2b40
6
+ metadata.gz: 8b57e52011f115272e0b13b0c24994ef0f3e7680082e57d3730712b73fd19e5ab36962e7a7b6d5b1a20b65bf316424d69da011cd0cf02b6576be6516fad09014
7
+ data.tar.gz: db7a5a208d5af8b72d6e49c84198ea1a8ceebeeea1a975aa295ec876b7d1c3d19ef547141c028d6f62b0673eb80dc1dff3f23a8961541d2a233a95719f093a98
@@ -1,3 +1,3 @@
1
1
  module WhereExists
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
data/lib/where_exists.rb CHANGED
@@ -105,7 +105,8 @@ module WhereExists
105
105
  next_association = {
106
106
  association: association.source_reflection,
107
107
  params: where_parameters,
108
- next_association: next_association
108
+ next_association: next_association,
109
+ scope: association.scope
109
110
  }
110
111
  association = association.through_reflection
111
112
 
@@ -125,7 +126,7 @@ module WhereExists
125
126
  end
126
127
  end
127
128
 
128
- association_scope = next_association[:scope] || association.scope
129
+ association_scope = association.scope
129
130
 
130
131
  associated_model = association.klass
131
132
 
@@ -207,12 +208,18 @@ module WhereExists
207
208
  end
208
209
 
209
210
  def loop_nested_association(query, next_association = {}, nested = false, &block)
211
+ scope = next_association[:scope] || -> { self }
212
+ block ||= ->(it) { it }
213
+ block_with_scope =
214
+ lambda do |it|
215
+ block.call(it.instance_exec(&scope))
216
+ end
210
217
  str = query.klass.build_exists_string(
211
218
  next_association[:association].name,
212
219
  *[
213
220
  *next_association[:params]
214
- ],
215
- &block
221
+ ].compact,
222
+ &block_with_scope
216
223
  )
217
224
 
218
225
  if next_association[:next_association] && next_association[:next_association][:association]
@@ -222,7 +229,7 @@ module WhereExists
222
229
  next_association[:association],
223
230
  next_association[:next_association],
224
231
  true,
225
- &block
232
+ &block_with_scope
226
233
  )}))"
227
234
  end
228
235
  end
data/test/db/test.db CHANGED
Binary file
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ ActiveRecord::Migration.create_table :posts, :force => true do |t|
4
+ t.boolean :archived, default: false, null: false
5
+ end
6
+
7
+ ActiveRecord::Migration.create_table :comments, :force => true do |t|
8
+ t.integer :post_id
9
+ t.integer :commentator_id
10
+ end
11
+
12
+ ActiveRecord::Migration.create_table :commentators, :force => true do |t|
13
+ end
14
+
15
+ class Post < ActiveRecord::Base
16
+ has_many :comments
17
+ end
18
+
19
+ class Comment < ActiveRecord::Base
20
+ belongs_to :post
21
+ belongs_to :commentator
22
+ end
23
+
24
+ class Commentator < ActiveRecord::Base
25
+ has_many :comments
26
+ has_many :posts, -> { where(archived: false) }, through: :comments
27
+ end
28
+
29
+ class HasManyThroughConditionsTest < Minitest::Test
30
+ def test_where_exists
31
+ post = Post.create!
32
+ archived_post = Post.create! archived: true
33
+
34
+ commentator = Commentator.create! posts: [post]
35
+ commentator2 = Commentator.create! posts: [archived_post]
36
+ commentator3 = Commentator.create!
37
+
38
+ assert_equal [commentator], Commentator.where_exists(:posts).to_a # fail: also includes commentator2
39
+ assert_equal [commentator2, commentator3], Commentator.where_not_exists(:posts).to_a # fail: does not include commentator2
40
+ end
41
+ end
data/test/test_helper.rb CHANGED
@@ -13,8 +13,5 @@ else
13
13
  end
14
14
 
15
15
  ActiveRecord::Base.time_zone_aware_attributes = true
16
-
17
- ActiveRecord::Base.establish_connection(
18
- :adapter => 'sqlite3',
19
- :database => File.dirname(__FILE__) + '/db/test.db'
20
- )
16
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
17
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_exists
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-09-18 00:00:00.000000000 Z
11
+ date: 2025-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -120,6 +120,7 @@ files:
120
120
  - test/has_many_polymorphic_test.rb
121
121
  - test/has_many_test.rb
122
122
  - test/has_many_through_belongs_to_test.rb
123
+ - test/has_many_through_conditions_test.rb
123
124
  - test/has_many_through_test.rb
124
125
  - test/test_helper.rb
125
126
  homepage: http://github.com/eugzol/where_exists
@@ -147,6 +148,7 @@ specification_version: 4
147
148
  summary: "#where_exists extension of ActiveRecord"
148
149
  test_files:
149
150
  - test/documentation_test.rb
151
+ - test/has_many_through_conditions_test.rb
150
152
  - test/belongs_to_polymorphic_test.rb
151
153
  - test/belongs_to_test.rb
152
154
  - test/has_many_polymorphic_test.rb