where_exists 2.0.0 → 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: 5f3ba0936a289c595a6efec3b73bcc9f46f445981150a43fa72af419eb13dec8
4
- data.tar.gz: 022c543b783c601be1076b4de866e9052d1c6b352b727b52a793585a571aed61
3
+ metadata.gz: d9957b5787ab2fda2505560b01ffeca8f2e1d24bf0d0d38e09f4f25969a30ab6
4
+ data.tar.gz: 4718917be7477f54afb0e322a588adadb4d2d375c6b7938d922b89ef0629ea27
5
5
  SHA512:
6
- metadata.gz: 265df32741d7633eaaac17b5dfa27a33e813ba4680a98ca4c8fd05d58d839adf4439944dd5e0942b1b44af850a14fb2bf5b48848d73fb8c4beb6fb2b94766749
7
- data.tar.gz: 7dd2b69d952f63f4bb96999b0e4c86672fe08cdd77f37755a48a7d6d2aab0072bf0a65b5f6aed8183a6688dcbdff453fd00d41a035e29009762cfc69fcc8a261
6
+ metadata.gz: 839de9969500c67df98512ee5f5043b4d34a8e337263e3dcc79dd081e6730a0b1bb6bce3c632a764372d6dabe143f9b3c7ffcbc55092f55727d8746d40eafaf3
7
+ data.tar.gz: a4c38490f6caf945889d2f92075e1069cef92706524c299bd4d800c4b856fd62fda39738539fee816daa955a5ef3908e49c7aed9ed4e3be97af6bf8c419f3b07
data/MIT-LICENSE CHANGED
File without changes
data/README.markdown CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## Description
6
6
 
7
- <img src="http://i.imgur.com/psLfPoW.gif" alt="Exists" align="right" width="100" height="200">
7
+ <img src="exists.png" alt="Exists" align="right" width="100" height="200">
8
8
 
9
9
  This gem does exactly two things:
10
10
 
@@ -155,6 +155,8 @@ Also,
155
155
  * Submit pull request with new features or bug fixes
156
156
  * Enhance or clarify the documentation that you are reading
157
157
 
158
+ **Please ping me in addition to creating PR/issue** (just add "@EugZol" to the PR/issue text). Thank you!
159
+
158
160
  To run tests:
159
161
  ```
160
162
  > bundle exec appraisal install
data/Rakefile CHANGED
File without changes
@@ -1,3 +1,3 @@
1
1
  module WhereExists
2
- VERSION = "2.0.0"
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
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -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
data/test/test_helper.rb CHANGED
File without changes
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_exists
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
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: 2021-12-16 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
- name: rails
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -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
@@ -145,12 +144,11 @@ signing_key:
145
144
  specification_version: 4
146
145
  summary: "#where_exists extension of ActiveRecord"
147
146
  test_files:
147
+ - test/documentation_test.rb
148
148
  - test/belongs_to_polymorphic_test.rb
149
149
  - test/belongs_to_test.rb
150
- - test/db/test.db
151
- - test/documentation_test.rb
152
- - test/has_and_belongs_to_many.rb
153
150
  - test/has_many_polymorphic_test.rb
154
- - test/has_many_test.rb
155
151
  - test/has_many_through_test.rb
156
152
  - test/test_helper.rb
153
+ - test/has_and_belongs_to_many.rb
154
+ - test/has_many_test.rb
data/test/db/test.db DELETED
Binary file