where_exists 2.0.1 → 2.0.3

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: 70bb8d0cc4affb99051fe748c883841abd8635f78faab07572208dd495ae9e0e
4
- data.tar.gz: 56be70dc7847f0beddf4a19284485e8e50c38709e2b0db4f64a0250c967b0a33
3
+ metadata.gz: 15979f86fa103995db4baecef959d5b401f32471a1aae8fcc126191f8938b1b9
4
+ data.tar.gz: 74ea2303b2851d8c07041cb33f06493b23db39be619c7a17882fc84e33473632
5
5
  SHA512:
6
- metadata.gz: e86824a1c85aa3a4006bc20035444fe36d1895c7cb17fd3019e5da5431753ba45fd735bbd41ae903f807bc7e45e36d415c887390d2fab8805d44e8afdcd37f2c
7
- data.tar.gz: 5a4814a20b678da5ee28b9cdbccd99ef653f4ea6284fd42f5203d0074301d35b3627524e00e89623000595fdb7f9c110873224f163edc2b0a112ae0deeba18b9
6
+ metadata.gz: fca8f47b863efa329a1a9fed4d66d5c8319daa1b370eb5e0618dd06c61ef224641372c2eb339c9af3ce36f78669b337fcca7a78363cdcc752fe868bc9021ca8e
7
+ data.tar.gz: 7e7bc8a06cc1c3aa3d16b00fc9fd2919cdb0986a93b7d2643cf25798f705d31db7b84790939b8b89cd0ff945e37aac7ea30052701d3c10588fa8b3b4bc864e51
@@ -1,3 +1,3 @@
1
1
  module WhereExists
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.3"
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
@@ -48,18 +48,17 @@ module WhereExists
48
48
  rescue
49
49
  inspection = association.macro
50
50
  end
51
- raise ArgumentError.new("where_exists: not supported association #{inspection}")
51
+ raise ArgumentError.new("where_exists: not supported association - #{inspection}")
52
52
  end
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
@@ -121,7 +121,7 @@ module WhereExists
121
121
  rescue
122
122
  inspection = association.macro
123
123
  end
124
- raise ArgumentError.new("where_exists: not supported association #{inspection}")
124
+ raise ArgumentError.new("where_exists: not supported association - #{inspection}")
125
125
  end
126
126
  end
127
127
 
@@ -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
data/test/test_helper.rb CHANGED
@@ -5,10 +5,16 @@ Bundler.require(:default)
5
5
  require 'active_record'
6
6
  require File.dirname(__FILE__) + '/../lib/where_exists'
7
7
 
8
- ActiveRecord::Base.default_timezone = :utc
8
+ # Rails < 7.1
9
+ if ActiveRecord::Base.respond_to?(:default_timezone=)
10
+ ActiveRecord::Base.default_timezone = :utc
11
+ else
12
+ ActiveRecord.default_timezone = :utc
13
+ end
14
+
9
15
  ActiveRecord::Base.time_zone_aware_attributes = true
10
16
 
11
17
  ActiveRecord::Base.establish_connection(
12
18
  :adapter => 'sqlite3',
13
- :database => File.dirname(__FILE__) + "/db/test.db"
19
+ :database => File.dirname(__FILE__) + '/db/test.db'
14
20
  )
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-11 00:00:00.000000000 Z
11
+ date: 2023-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,7 +17,7 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '5.2'
20
- - - "<"
20
+ - - "<="
21
21
  - !ruby/object:Gem::Version
22
22
  version: '7.1'
23
23
  type: :runtime
@@ -27,7 +27,7 @@ dependencies:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '5.2'
30
- - - "<"
30
+ - - "<="
31
31
  - !ruby/object:Gem::Version
32
32
  version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
@@ -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
@@ -125,7 +124,7 @@ homepage: http://github.com/eugzol/where_exists
125
124
  licenses:
126
125
  - MIT
127
126
  metadata: {}
128
- post_install_message:
127
+ post_install_message:
129
128
  rdoc_options: []
130
129
  require_paths:
131
130
  - lib
@@ -140,17 +139,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
139
  - !ruby/object:Gem::Version
141
140
  version: '0'
142
141
  requirements: []
143
- rubygems_version: 3.1.6
144
- signing_key:
142
+ rubygems_version: 3.4.6
143
+ signing_key:
145
144
  specification_version: 4
146
145
  summary: "#where_exists extension of ActiveRecord"
147
146
  test_files:
148
- - test/documentation_test.rb
149
147
  - test/belongs_to_polymorphic_test.rb
150
148
  - test/belongs_to_test.rb
149
+ - test/documentation_test.rb
150
+ - test/has_and_belongs_to_many.rb
151
151
  - test/has_many_polymorphic_test.rb
152
- - test/db/test.db
152
+ - test/has_many_test.rb
153
153
  - test/has_many_through_test.rb
154
154
  - test/test_helper.rb
155
- - test/has_and_belongs_to_many.rb
156
- - test/has_many_test.rb
data/test/db/test.db DELETED
Binary file