where_exists 2.0.1 → 2.0.2

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: 70bb8d0cc4affb99051fe748c883841abd8635f78faab07572208dd495ae9e0e
4
- data.tar.gz: 56be70dc7847f0beddf4a19284485e8e50c38709e2b0db4f64a0250c967b0a33
3
+ metadata.gz: d9957b5787ab2fda2505560b01ffeca8f2e1d24bf0d0d38e09f4f25969a30ab6
4
+ data.tar.gz: 4718917be7477f54afb0e322a588adadb4d2d375c6b7938d922b89ef0629ea27
5
5
  SHA512:
6
- metadata.gz: e86824a1c85aa3a4006bc20035444fe36d1895c7cb17fd3019e5da5431753ba45fd735bbd41ae903f807bc7e45e36d415c887390d2fab8805d44e8afdcd37f2c
7
- data.tar.gz: 5a4814a20b678da5ee28b9cdbccd99ef653f4ea6284fd42f5203d0074301d35b3627524e00e89623000595fdb7f9c110873224f163edc2b0a112ae0deeba18b9
6
+ metadata.gz: 839de9969500c67df98512ee5f5043b4d34a8e337263e3dcc79dd081e6730a0b1bb6bce3c632a764372d6dabe143f9b3c7ffcbc55092f55727d8746d40eafaf3
7
+ data.tar.gz: a4c38490f6caf945889d2f92075e1069cef92706524c299bd4d800c4b856fd62fda39738539fee816daa955a5ef3908e49c7aed9ed4e3be97af6bf8c419f3b07
@@ -1,3 +1,3 @@
1
1
  module WhereExists
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.2"
3
3
  end
data/lib/where_exists.rb CHANGED
@@ -36,11 +36,11 @@ module WhereExists
36
36
 
37
37
  case association.macro
38
38
  when :belongs_to
39
- queries = where_exists_for_belongs_to_query(association, where_parameters)
39
+ queries = where_exists_for_belongs_to_query(association, where_parameters, &block)
40
40
  when :has_many, :has_one
41
- queries = where_exists_for_has_many_query(association, where_parameters)
41
+ queries = where_exists_for_has_many_query(association, where_parameters, &block)
42
42
  when :has_and_belongs_to_many
43
- queries = where_exists_for_habtm_query(association, where_parameters)
43
+ queries = where_exists_for_habtm_query(association, where_parameters, &block)
44
44
  else
45
45
  inspection = nil
46
46
  begin
@@ -53,13 +53,12 @@ module WhereExists
53
53
 
54
54
  queries_sql =
55
55
  queries.map do |query|
56
- query = yield query if block_given?
57
56
  "EXISTS (" + query.to_sql + ")"
58
57
  end
59
58
  queries_sql.join(" OR ")
60
59
  end
61
60
 
62
- def where_exists_for_belongs_to_query(association, where_parameters)
61
+ def where_exists_for_belongs_to_query(association, where_parameters, &block)
63
62
  polymorphic = association.options[:polymorphic].present?
64
63
 
65
64
  association_scope = association.scope
@@ -93,13 +92,14 @@ module WhereExists
93
92
 
94
93
  query = query.where("#{self_type} IN (?)", other_types.uniq)
95
94
  end
95
+ query = yield query if block_given?
96
96
  queries.push query
97
97
  end
98
98
 
99
99
  queries
100
100
  end
101
101
 
102
- def where_exists_for_has_many_query(association, where_parameters, next_association = {})
102
+ def where_exists_for_has_many_query(association, where_parameters, next_association = {}, &block)
103
103
  if association.through_reflection
104
104
  raise ArgumentError.new(association) unless association.source_reflection
105
105
  next_association = {
@@ -111,9 +111,9 @@ module WhereExists
111
111
 
112
112
  case association.macro
113
113
  when :has_many, :has_one
114
- return where_exists_for_has_many_query(association, {}, next_association)
114
+ return where_exists_for_has_many_query(association, {}, next_association, &block)
115
115
  when :has_and_belongs_to_many
116
- return where_exists_for_habtm_query(association, {}, next_association)
116
+ return where_exists_for_habtm_query(association, {}, next_association, &block)
117
117
  else
118
118
  inspection = nil
119
119
  begin
@@ -148,17 +148,18 @@ module WhereExists
148
148
  end
149
149
 
150
150
  if next_association[:association]
151
- return loop_nested_association(result, next_association)
151
+ return loop_nested_association(result, next_association, &block)
152
152
  end
153
153
 
154
154
  if where_parameters != []
155
155
  result = result.where(*where_parameters)
156
156
  end
157
157
 
158
+ result = yield result if block_given?
158
159
  [result]
159
160
  end
160
161
 
161
- def where_exists_for_habtm_query(association, where_parameters, next_association = {})
162
+ def where_exists_for_habtm_query(association, where_parameters, next_association = {}, &block)
162
163
  association_scope = association.scope
163
164
 
164
165
  associated_model = association.klass
@@ -182,7 +183,7 @@ module WhereExists
182
183
  where("#{join_ids} = #{self_ids}")
183
184
 
184
185
  if next_association[:association]
185
- return loop_nested_association(result, next_association)
186
+ return loop_nested_association(result, next_association, &block)
186
187
  end
187
188
 
188
189
  if where_parameters != []
@@ -193,15 +194,18 @@ module WhereExists
193
194
  result = result.instance_exec(&association_scope)
194
195
  end
195
196
 
197
+ result = yield result if block_given?
198
+
196
199
  [result]
197
200
  end
198
201
 
199
- def loop_nested_association(query, next_association = {}, nested = false)
202
+ def loop_nested_association(query, next_association = {}, nested = false, &block)
200
203
  str = query.klass.build_exists_string(
201
204
  next_association[:association].name,
202
205
  *[
203
206
  *next_association[:params]
204
207
  ],
208
+ &block
205
209
  )
206
210
 
207
211
  if next_association[:next_association] && next_association[:next_association][:association]
@@ -210,7 +214,8 @@ module WhereExists
210
214
  "(#{subq} AND (#{loop_nested_association(
211
215
  next_association[:association],
212
216
  next_association[:next_association],
213
- true
217
+ true,
218
+ &block
214
219
  )}))"
215
220
  end
216
221
  end
@@ -52,8 +52,6 @@ class Blob < ActiveRecord::Base
52
52
  end
53
53
  end
54
54
 
55
-
56
-
57
55
  class Project < ActiveRecord::Base
58
56
  has_many :tasks
59
57
  has_many :invoices, :through => :tasks
@@ -99,8 +97,6 @@ class HasManyThroughTest < Minitest::Test
99
97
  end
100
98
 
101
99
  def test_one_level_through
102
- ActiveRecord::Base.descendants.each(&:delete_all)
103
-
104
100
  project = Project.create!
105
101
  irrelevant_project = Project.create!
106
102
 
@@ -121,8 +117,6 @@ class HasManyThroughTest < Minitest::Test
121
117
  end
122
118
 
123
119
  def test_deep_through
124
- ActiveRecord::Base.descendants.each(&:delete_all)
125
-
126
120
  project = Project.create! name: 'relevant'
127
121
  irrelevant_project = Project.create! name: 'irrelevant'
128
122
 
@@ -188,6 +182,14 @@ class HasManyThroughTest < Minitest::Test
188
182
  result = Project.where_not_exists(:blobs)
189
183
 
190
184
  assert_equal 0, result.length
185
+ end
191
186
 
187
+ def test_with_yield
188
+ project = Project.create! name: 'example_project'
189
+ task = Task.create!(project: project)
190
+ line_item = LineItem.create!(name: 'example_line_item', task: task)
191
+ result = Project.where_exists(:project_line_items) { |scope| scope.where(name: 'example_line_item') }
192
+
193
+ assert_equal 1, result.length
192
194
  end
193
195
  end
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: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-11 00:00:00.000000000 Z
11
+ date: 2022-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -114,7 +114,6 @@ files:
114
114
  - lib/where_exists/version.rb
115
115
  - test/belongs_to_polymorphic_test.rb
116
116
  - test/belongs_to_test.rb
117
- - test/db/test.db
118
117
  - test/documentation_test.rb
119
118
  - test/has_and_belongs_to_many.rb
120
119
  - test/has_many_polymorphic_test.rb
@@ -149,7 +148,6 @@ test_files:
149
148
  - test/belongs_to_polymorphic_test.rb
150
149
  - test/belongs_to_test.rb
151
150
  - test/has_many_polymorphic_test.rb
152
- - test/db/test.db
153
151
  - test/has_many_through_test.rb
154
152
  - test/test_helper.rb
155
153
  - test/has_and_belongs_to_many.rb
data/test/db/test.db DELETED
Binary file