active_record_extended 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/active_record_extended/active_record/relation_patch.rb +16 -4
- data/lib/active_record_extended/active_record.rb +6 -6
- data/lib/active_record_extended/arel/nodes.rb +1 -1
- data/lib/active_record_extended/arel/visitors/postgresql_decorator.rb +16 -12
- data/lib/active_record_extended/query_methods/any_of.rb +1 -1
- data/lib/active_record_extended/query_methods/either.rb +2 -2
- data/lib/active_record_extended/query_methods/unionize.rb +3 -3
- data/lib/active_record_extended/query_methods/where_chain.rb +5 -3
- data/lib/active_record_extended/query_methods/with_cte.rb +2 -2
- data/lib/active_record_extended/version.rb +1 -1
- metadata +8 -56
- data/lib/active_record_extended/patch/5_1/where_clause.rb +0 -11
- data/spec/active_record_extended_spec.rb +0 -7
- data/spec/query_methods/any_of_spec.rb +0 -131
- data/spec/query_methods/array_query_spec.rb +0 -64
- data/spec/query_methods/either_spec.rb +0 -70
- data/spec/query_methods/hash_query_spec.rb +0 -45
- data/spec/query_methods/inet_query_spec.rb +0 -112
- data/spec/query_methods/json_spec.rb +0 -157
- data/spec/query_methods/select_spec.rb +0 -115
- data/spec/query_methods/unionize_spec.rb +0 -165
- data/spec/query_methods/window_spec.rb +0 -51
- data/spec/query_methods/with_cte_spec.rb +0 -50
- data/spec/spec_helper.rb +0 -28
- data/spec/sql_inspections/any_of_sql_spec.rb +0 -41
- data/spec/sql_inspections/arel/aggregate_function_name_spec.rb +0 -41
- data/spec/sql_inspections/arel/array_spec.rb +0 -63
- data/spec/sql_inspections/arel/inet_spec.rb +0 -66
- data/spec/sql_inspections/contains_sql_queries_spec.rb +0 -47
- data/spec/sql_inspections/either_sql_spec.rb +0 -71
- data/spec/sql_inspections/json_sql_spec.rb +0 -82
- data/spec/sql_inspections/unionize_sql_spec.rb +0 -124
- data/spec/sql_inspections/window_sql_spec.rb +0 -98
- data/spec/sql_inspections/with_cte_sql_spec.rb +0 -95
- data/spec/support/database_cleaner.rb +0 -15
- data/spec/support/models.rb +0 -80
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f8d2f193f9e0e50c6e83e462c1df30a9b948779ebf3e00f64c735f165bdb5f3
|
4
|
+
data.tar.gz: 0ee6d70e9b6714c6ab3431da2df340ff63fca28bc0ceb6fc2c3e45593b9e87e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 282ad3881cb0ed581dbe1aa0dd605dbc33cb7182d560a015f2de5a57a1ca49f3db8e5b5515229d527130e6f7f35321105bface3669edb15340b1bb76e333d7c4
|
7
|
+
data.tar.gz: c7b38bca14c8ed07faba4f2996a18c455b85dec7def64e184c1a8dc9defa86681c1650b9097112264248c66580685f2f90825e9cf04d981e48b47333e091bde2
|
data/README.md
CHANGED
@@ -52,12 +52,12 @@ Active Record Extended is essentially providing users with the other half of Pos
|
|
52
52
|
## Compatibility
|
53
53
|
|
54
54
|
This package is designed align and work with any officially supported Ruby and Rails versions.
|
55
|
-
- Minimum Ruby Version: 2.
|
56
|
-
- Minimum Rails Version: 5.
|
57
|
-
- Minimum Postgres Version:
|
55
|
+
- Minimum Ruby Version: 2.5.x **(EOL warning!)**
|
56
|
+
- Minimum Rails Version: 5.2.x **(EOL warning!)**
|
57
|
+
- Minimum Postgres Version: 10.x **(EOL warning!)**
|
58
58
|
- Latest Ruby supported: 3.0.x
|
59
59
|
- Latest Rails supported: 7.0.x
|
60
|
-
- Postgres:
|
60
|
+
- Postgres: 10-current(14) (probably works with most older versions to a certain point)
|
61
61
|
|
62
62
|
## Installation
|
63
63
|
|
@@ -13,15 +13,27 @@ module ActiveRecordExtended
|
|
13
13
|
end
|
14
14
|
|
15
15
|
module Merger
|
16
|
-
def normal_values
|
17
|
-
super + [:union, :define_window]
|
18
|
-
end
|
19
|
-
|
20
16
|
def merge
|
21
17
|
merge_ctes!
|
18
|
+
merge_union!
|
19
|
+
merge_windows!
|
22
20
|
super
|
23
21
|
end
|
24
22
|
|
23
|
+
def merge_union!
|
24
|
+
return if other.unionize_storage.empty?
|
25
|
+
|
26
|
+
relation.union_values += other.union_values
|
27
|
+
relation.union_operations += other.union_operations
|
28
|
+
relation.union_ordering_values += other.union_ordering_values
|
29
|
+
end
|
30
|
+
|
31
|
+
def merge_windows!
|
32
|
+
return unless other.window_values?
|
33
|
+
|
34
|
+
relation.window_values |= other.window_values
|
35
|
+
end
|
36
|
+
|
25
37
|
def merge_ctes!
|
26
38
|
return unless other.with_values?
|
27
39
|
|
@@ -5,6 +5,11 @@ require "active_record/relation"
|
|
5
5
|
require "active_record/relation/merger"
|
6
6
|
require "active_record/relation/query_methods"
|
7
7
|
|
8
|
+
module ActiveRecordExtended
|
9
|
+
# TODO: Deprecate <= AR 6.0 methods & routines
|
10
|
+
AR_VERSION_GTE_6_1 = Gem::Requirement.new(">= 6.1").satisfied_by?(ActiveRecord.gem_version)
|
11
|
+
end
|
12
|
+
|
8
13
|
require "active_record_extended/predicate_builder/array_handler_decorator"
|
9
14
|
|
10
15
|
require "active_record_extended/active_record/relation_patch"
|
@@ -17,9 +22,4 @@ require "active_record_extended/query_methods/either"
|
|
17
22
|
require "active_record_extended/query_methods/inet"
|
18
23
|
require "active_record_extended/query_methods/json"
|
19
24
|
require "active_record_extended/query_methods/select"
|
20
|
-
|
21
|
-
if Gem::Requirement.new("~> 5.1.0").satisfied_by?(ActiveRecord.gem_version)
|
22
|
-
require "active_record_extended/patch/5_1/where_clause"
|
23
|
-
else
|
24
|
-
require "active_record_extended/patch/5_2/where_clause"
|
25
|
-
end
|
25
|
+
require "active_record_extended/patch/5_2/where_clause"
|
@@ -5,7 +5,7 @@ require "arel/nodes/function"
|
|
5
5
|
|
6
6
|
module Arel
|
7
7
|
module Nodes
|
8
|
-
|
8
|
+
unless ActiveRecordExtended::AR_VERSION_GTE_6_1
|
9
9
|
["Contains", "Overlaps"].each { |binary_node_name| const_set(binary_node_name, Class.new(::Arel::Nodes::Binary)) }
|
10
10
|
end
|
11
11
|
|
@@ -9,21 +9,25 @@ module ActiveRecordExtended
|
|
9
9
|
|
10
10
|
# rubocop:disable Naming/MethodName
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
unless ActiveRecordExtended::AR_VERSION_GTE_6_1
|
13
|
+
def visit_Arel_Nodes_Overlaps(object, collector)
|
14
|
+
infix_value object, collector, " && "
|
15
|
+
end
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
unless ActiveRecordExtended::AR_VERSION_GTE_6_1
|
19
|
+
def visit_Arel_Nodes_Contains(object, collector)
|
20
|
+
left_column = object.left.relation.name.classify.constantize.columns.detect do |col|
|
21
|
+
matchable_column?(col, object)
|
22
|
+
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
if [:hstore, :jsonb].include?(left_column&.type)
|
25
|
+
visit_Arel_Nodes_ContainsHStore(object, collector)
|
26
|
+
elsif left_column.try(:array)
|
27
|
+
visit_Arel_Nodes_ContainsArray(object, collector)
|
28
|
+
else
|
29
|
+
visit_Arel_Nodes_Inet_Contains(object, collector)
|
30
|
+
end
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
@@ -29,7 +29,7 @@ module ActiveRecordExtended
|
|
29
29
|
|
30
30
|
def hash_map_queries(queries)
|
31
31
|
if queries.size == 1 && queries.first.is_a?(Hash)
|
32
|
-
queries.first.each_pair.map { |attr, predicate|
|
32
|
+
queries.first.each_pair.map { |attr, predicate| { attr => predicate } }
|
33
33
|
else
|
34
34
|
queries
|
35
35
|
end
|
@@ -26,7 +26,7 @@ module ActiveRecordExtended
|
|
26
26
|
private
|
27
27
|
|
28
28
|
def xor_field_sql(options)
|
29
|
-
XOR_FIELD_SQL %
|
29
|
+
XOR_FIELD_SQL % xor_field_options(options).to_h
|
30
30
|
end
|
31
31
|
|
32
32
|
def sort_order_sql(dir)
|
@@ -35,7 +35,7 @@ module ActiveRecordExtended
|
|
35
35
|
|
36
36
|
def xor_field_options(options)
|
37
37
|
str_args = options.flatten.take(XOR_FIELD_KEYS.size).map(&:to_s)
|
38
|
-
|
38
|
+
XOR_FIELD_KEYS.zip(str_args).to_h
|
39
39
|
end
|
40
40
|
|
41
41
|
def map_columns_to_tables(associations_and_columns)
|
@@ -107,7 +107,7 @@ module ActiveRecordExtended
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def union(opts = :chain, *args)
|
110
|
-
return UnionChain.new(spawn) if
|
110
|
+
return UnionChain.new(spawn) if opts == :chain
|
111
111
|
|
112
112
|
opts.nil? ? self : spawn.union!(opts, *args, chain_method: __callee__)
|
113
113
|
end
|
@@ -121,7 +121,7 @@ module ActiveRecordExtended
|
|
121
121
|
def union!(opts = :chain, *args, chain_method: :union)
|
122
122
|
union_chain = UnionChain.new(self)
|
123
123
|
chain_method ||= :union
|
124
|
-
return union_chain if
|
124
|
+
return union_chain if opts == :chain
|
125
125
|
|
126
126
|
union_chain.public_send(chain_method, *([opts] + args))
|
127
127
|
end
|
@@ -178,7 +178,7 @@ module ActiveRecordExtended
|
|
178
178
|
def build_union_nodes!(raise_error = true)
|
179
179
|
unionize_error_or_warn!(raise_error)
|
180
180
|
union_values.each_with_index.reduce(nil) do |union_node, (relation_node, index)|
|
181
|
-
next resolve_relation_node(relation_node) if union_node.nil?
|
181
|
+
next resolve_relation_node(relation_node) if union_node.nil? # rubocop:disable Lint/UnmodifiedReduceAccumulator
|
182
182
|
|
183
183
|
operation = union_operations.fetch(index - 1, :union)
|
184
184
|
left = union_node
|
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
module ActiveRecordExtended
|
4
4
|
module WhereChain
|
5
|
-
AR_VERSION_AT_LEAST_6_1 = ActiveRecord.version >= Gem::Version.new('6.1')
|
6
|
-
|
7
5
|
# Finds Records that have an array column that contain any a set of values
|
8
6
|
# User.where.overlap(tags: [1,2])
|
9
7
|
# # SELECT * FROM users WHERE tags && {1,2}
|
@@ -47,6 +45,10 @@ module ActiveRecordExtended
|
|
47
45
|
# # SELECT tags.* FROM tags INNER JOIN user on user.id = tags.user_id WHERE user.data @> { nickname: 'chainer' }
|
48
46
|
#
|
49
47
|
def contains(opts, *rest)
|
48
|
+
if ActiveRecordExtended::AR_VERSION_GTE_6_1
|
49
|
+
return substitute_comparisons(opts, rest, Arel::Nodes::Contains, "contains")
|
50
|
+
end
|
51
|
+
|
50
52
|
build_where_chain(opts, rest) do |arel|
|
51
53
|
case arel
|
52
54
|
when Arel::Nodes::In, Arel::Nodes::Equality
|
@@ -108,7 +110,7 @@ module ActiveRecordExtended
|
|
108
110
|
end
|
109
111
|
|
110
112
|
def build_where_clause_for(scope, opts, rest)
|
111
|
-
if
|
113
|
+
if ActiveRecordExtended::AR_VERSION_GTE_6_1
|
112
114
|
scope.send(:build_where_clause, opts, rest)
|
113
115
|
else
|
114
116
|
scope.send(:where_clause_factory).build(opts, rest)
|
@@ -113,14 +113,14 @@ module ActiveRecordExtended
|
|
113
113
|
|
114
114
|
# @param [Hash, WithCTE] opts
|
115
115
|
def with(opts = :chain, *rest)
|
116
|
-
return WithChain.new(spawn) if
|
116
|
+
return WithChain.new(spawn) if opts == :chain
|
117
117
|
|
118
118
|
opts.blank? ? self : spawn.with!(opts, *rest)
|
119
119
|
end
|
120
120
|
|
121
121
|
# @param [Hash, WithCTE] opts
|
122
122
|
def with!(opts = :chain, *_rest)
|
123
|
-
return WithChain.new(self) if
|
123
|
+
return WithChain.new(self) if opts == :chain
|
124
124
|
|
125
125
|
tap do |scope|
|
126
126
|
scope.cte ||= WithCTE.new(self)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_extended
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- George Protacio-Karaszi
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-
|
13
|
+
date: 2022-07-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '5.
|
21
|
+
version: '5.2'
|
22
22
|
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
24
|
version: 7.1.0
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version: '5.
|
31
|
+
version: '5.2'
|
32
32
|
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: 7.1.0
|
@@ -155,7 +155,6 @@ files:
|
|
155
155
|
- lib/active_record_extended/arel/predications.rb
|
156
156
|
- lib/active_record_extended/arel/sql_literal.rb
|
157
157
|
- lib/active_record_extended/arel/visitors/postgresql_decorator.rb
|
158
|
-
- lib/active_record_extended/patch/5_1/where_clause.rb
|
159
158
|
- lib/active_record_extended/patch/5_2/where_clause.rb
|
160
159
|
- lib/active_record_extended/predicate_builder/array_handler_decorator.rb
|
161
160
|
- lib/active_record_extended/query_methods/any_of.rb
|
@@ -170,34 +169,11 @@ files:
|
|
170
169
|
- lib/active_record_extended/utilities/order_by.rb
|
171
170
|
- lib/active_record_extended/utilities/support.rb
|
172
171
|
- lib/active_record_extended/version.rb
|
173
|
-
- spec/active_record_extended_spec.rb
|
174
|
-
- spec/query_methods/any_of_spec.rb
|
175
|
-
- spec/query_methods/array_query_spec.rb
|
176
|
-
- spec/query_methods/either_spec.rb
|
177
|
-
- spec/query_methods/hash_query_spec.rb
|
178
|
-
- spec/query_methods/inet_query_spec.rb
|
179
|
-
- spec/query_methods/json_spec.rb
|
180
|
-
- spec/query_methods/select_spec.rb
|
181
|
-
- spec/query_methods/unionize_spec.rb
|
182
|
-
- spec/query_methods/window_spec.rb
|
183
|
-
- spec/query_methods/with_cte_spec.rb
|
184
|
-
- spec/spec_helper.rb
|
185
|
-
- spec/sql_inspections/any_of_sql_spec.rb
|
186
|
-
- spec/sql_inspections/arel/aggregate_function_name_spec.rb
|
187
|
-
- spec/sql_inspections/arel/array_spec.rb
|
188
|
-
- spec/sql_inspections/arel/inet_spec.rb
|
189
|
-
- spec/sql_inspections/contains_sql_queries_spec.rb
|
190
|
-
- spec/sql_inspections/either_sql_spec.rb
|
191
|
-
- spec/sql_inspections/json_sql_spec.rb
|
192
|
-
- spec/sql_inspections/unionize_sql_spec.rb
|
193
|
-
- spec/sql_inspections/window_sql_spec.rb
|
194
|
-
- spec/sql_inspections/with_cte_sql_spec.rb
|
195
|
-
- spec/support/database_cleaner.rb
|
196
|
-
- spec/support/models.rb
|
197
172
|
homepage: https://github.com/georgekaraszi/ActiveRecordExtended
|
198
173
|
licenses:
|
199
174
|
- MIT
|
200
|
-
metadata:
|
175
|
+
metadata:
|
176
|
+
rubygems_mfa_required: 'true'
|
201
177
|
post_install_message:
|
202
178
|
rdoc_options: []
|
203
179
|
require_paths:
|
@@ -206,7 +182,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
206
182
|
requirements:
|
207
183
|
- - ">="
|
208
184
|
- !ruby/object:Gem::Version
|
209
|
-
version: '2.
|
185
|
+
version: '2.5'
|
210
186
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
211
187
|
requirements:
|
212
188
|
- - ">="
|
@@ -217,28 +193,4 @@ rubygems_version: 3.2.15
|
|
217
193
|
signing_key:
|
218
194
|
specification_version: 4
|
219
195
|
summary: Adds extended functionality to Activerecord Postgres implementation
|
220
|
-
test_files:
|
221
|
-
- spec/active_record_extended_spec.rb
|
222
|
-
- spec/query_methods/any_of_spec.rb
|
223
|
-
- spec/query_methods/array_query_spec.rb
|
224
|
-
- spec/query_methods/either_spec.rb
|
225
|
-
- spec/query_methods/hash_query_spec.rb
|
226
|
-
- spec/query_methods/inet_query_spec.rb
|
227
|
-
- spec/query_methods/json_spec.rb
|
228
|
-
- spec/query_methods/select_spec.rb
|
229
|
-
- spec/query_methods/unionize_spec.rb
|
230
|
-
- spec/query_methods/window_spec.rb
|
231
|
-
- spec/query_methods/with_cte_spec.rb
|
232
|
-
- spec/spec_helper.rb
|
233
|
-
- spec/sql_inspections/any_of_sql_spec.rb
|
234
|
-
- spec/sql_inspections/arel/aggregate_function_name_spec.rb
|
235
|
-
- spec/sql_inspections/arel/array_spec.rb
|
236
|
-
- spec/sql_inspections/arel/inet_spec.rb
|
237
|
-
- spec/sql_inspections/contains_sql_queries_spec.rb
|
238
|
-
- spec/sql_inspections/either_sql_spec.rb
|
239
|
-
- spec/sql_inspections/json_sql_spec.rb
|
240
|
-
- spec/sql_inspections/unionize_sql_spec.rb
|
241
|
-
- spec/sql_inspections/window_sql_spec.rb
|
242
|
-
- spec/sql_inspections/with_cte_sql_spec.rb
|
243
|
-
- spec/support/database_cleaner.rb
|
244
|
-
- spec/support/models.rb
|
196
|
+
test_files: []
|
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecordExtended
|
4
|
-
module WhereClause
|
5
|
-
def modified_predicates(&block)
|
6
|
-
::ActiveRecord::Relation::WhereClause.new(predicates.map(&block), binds)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
ActiveRecord::Relation::WhereClause.prepend(ActiveRecordExtended::WhereClause)
|
@@ -1,131 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe "Active Record Any / None of Methods" do
|
6
|
-
let!(:one) { User.create!(personal_id: 1) }
|
7
|
-
let!(:two) { User.create!(personal_id: 2) }
|
8
|
-
let!(:three) { User.create!(personal_id: 3) }
|
9
|
-
|
10
|
-
let!(:tag_one) { Tag.create!(user_id: one.id) }
|
11
|
-
let!(:tag_two) { Tag.create!(user_id: two.id) }
|
12
|
-
let!(:tag_three) { Tag.create!(user_id: three.id) }
|
13
|
-
|
14
|
-
describe "where.any_of/1" do
|
15
|
-
it "Should return queries that match any of the outlined queries" do
|
16
|
-
query = User.where.any_of({ personal_id: 1 }, { personal_id: 2 })
|
17
|
-
expect(query).to include(one, two)
|
18
|
-
expect(query).to_not include(three)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "Should accept where query predicates" do
|
22
|
-
personal_one = User.where(personal_id: 1)
|
23
|
-
personal_two = User.where(personal_id: 2)
|
24
|
-
query = User.where.any_of(personal_one, personal_two)
|
25
|
-
|
26
|
-
expect(query).to include(one, two)
|
27
|
-
expect(query).to_not include(three)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "Should accept query strings" do
|
31
|
-
personal_one = User.where(personal_id: 1)
|
32
|
-
|
33
|
-
query = User.where.any_of(personal_one, "personal_id > 2")
|
34
|
-
expect(query).to include(one, three)
|
35
|
-
expect(query).to_not include(two)
|
36
|
-
|
37
|
-
query = User.where.any_of(["personal_id >= ?", 2])
|
38
|
-
expect(query).to include(two, three)
|
39
|
-
expect(query).to_not include(one)
|
40
|
-
end
|
41
|
-
|
42
|
-
context "Relationship queries" do
|
43
|
-
it "Finds records that are queried from two or more has_many associations" do
|
44
|
-
user_one_tag = Tag.create!(user_id: one.id)
|
45
|
-
user_two_tag = Tag.create!(user_id: two.id)
|
46
|
-
query = Tag.where.any_of(one.hm_tags, two.hm_tags)
|
47
|
-
|
48
|
-
expect(query).to include(tag_one, tag_two, user_one_tag, user_two_tag)
|
49
|
-
expect(query).to_not include(tag_three)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "Finds records that are dynamically joined" do
|
53
|
-
user_one_tag = Tag.where(users: { id: one.id }).includes(:user).references(:user)
|
54
|
-
user_two_tag = Tag.where(users: { id: two.id }).joins(:user)
|
55
|
-
query = Tag.where.any_of(user_one_tag, user_two_tag)
|
56
|
-
|
57
|
-
expect(query).to include(tag_one, tag_two)
|
58
|
-
expect(query).to_not include(tag_three)
|
59
|
-
end
|
60
|
-
|
61
|
-
it "Return matched records of a joined table on the parent level" do
|
62
|
-
query = Tag.joins(:user).where.any_of(
|
63
|
-
{ users: { personal_id: 1 } },
|
64
|
-
{ users: { personal_id: 3 } }
|
65
|
-
)
|
66
|
-
|
67
|
-
expect(query).to include(tag_one, tag_three)
|
68
|
-
expect(query).to_not include(tag_two)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
describe "where.none_of/1" do
|
74
|
-
it "Should return queries that match none of the outlined queries" do
|
75
|
-
query = User.where.none_of({ personal_id: 1 }, { personal_id: 2 })
|
76
|
-
expect(query).to include(three)
|
77
|
-
expect(query).to_not include(one, two)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "Should accept where query predicates" do
|
81
|
-
personal_one = User.where(personal_id: 1)
|
82
|
-
personal_two = User.where(personal_id: 2)
|
83
|
-
query = User.where.none_of(personal_one, personal_two)
|
84
|
-
|
85
|
-
expect(query).to include(three)
|
86
|
-
expect(query).to_not include(one, two)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "Should accept query strings" do
|
90
|
-
personal_one = User.where(personal_id: 1)
|
91
|
-
|
92
|
-
query = User.where.none_of(personal_one, "personal_id > 2")
|
93
|
-
expect(query).to include(two)
|
94
|
-
expect(query).to_not include(one, three)
|
95
|
-
|
96
|
-
query = User.where.none_of(["personal_id >= ?", 2])
|
97
|
-
expect(query).to include(one)
|
98
|
-
expect(query).to_not include(two, three)
|
99
|
-
end
|
100
|
-
|
101
|
-
context "Relationship queries" do
|
102
|
-
it "Finds records that are queried from two or more has_many associations" do
|
103
|
-
user_one_tag = Tag.create!(user_id: one.id)
|
104
|
-
user_two_tag = Tag.create!(user_id: two.id)
|
105
|
-
query = Tag.where.none_of(one.hm_tags, two.hm_tags)
|
106
|
-
|
107
|
-
expect(query).to include(tag_three)
|
108
|
-
expect(query).to_not include(tag_one, tag_two, user_one_tag, user_two_tag)
|
109
|
-
end
|
110
|
-
|
111
|
-
it "Finds records that are dynamically joined" do
|
112
|
-
user_one_tag = Tag.where(users: { id: one.id }).includes(:user).references(:user)
|
113
|
-
user_two_tag = Tag.where(users: { id: two.id }).joins(:user)
|
114
|
-
query = Tag.where.none_of(user_one_tag, user_two_tag)
|
115
|
-
|
116
|
-
expect(query).to include(tag_three)
|
117
|
-
expect(query).to_not include(tag_one, tag_two)
|
118
|
-
end
|
119
|
-
|
120
|
-
it "Return matched records of a joined table on the parent level" do
|
121
|
-
query = Tag.joins(:user).where.none_of(
|
122
|
-
{ users: { personal_id: 1 } },
|
123
|
-
{ users: { personal_id: 3 } }
|
124
|
-
)
|
125
|
-
|
126
|
-
expect(query).to include(tag_two)
|
127
|
-
expect(query).to_not include(tag_one, tag_three)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe "Active Record Array Query Methods" do
|
6
|
-
let!(:one) { User.create!(tags: [1, 2, 3], personal_id: 33) }
|
7
|
-
let!(:two) { User.create!(tags: [3, 1, 5], personal_id: 88) }
|
8
|
-
let!(:three) { User.create!(tags: [2, 8, 20], personal_id: 33) }
|
9
|
-
|
10
|
-
describe "#overlap" do
|
11
|
-
it "Should return matched records" do
|
12
|
-
query = User.where.overlap(tags: [1])
|
13
|
-
expect(query).to include(one, two)
|
14
|
-
expect(query).to_not include(three)
|
15
|
-
|
16
|
-
query = User.where.overlap(tags: [2, 3])
|
17
|
-
expect(query).to include(one, two, three)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe "#contains" do
|
22
|
-
it "returns records that contain elements in an array" do
|
23
|
-
query = User.where.contains(tags: [1, 3])
|
24
|
-
expect(query).to include(one, two)
|
25
|
-
expect(query).to_not include(three)
|
26
|
-
|
27
|
-
query = User.where.contains(tags: [8, 2])
|
28
|
-
expect(query).to include(three)
|
29
|
-
expect(query).to_not include(one, two)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "#any" do
|
34
|
-
it "should return any records that match" do
|
35
|
-
query = User.where.any(tags: 3)
|
36
|
-
expect(query).to include(one, two)
|
37
|
-
expect(query).to_not include(three)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "allows chaining" do
|
41
|
-
query = User.where.any(tags: 3).where(personal_id: 33)
|
42
|
-
expect(query).to include(one)
|
43
|
-
expect(query).to_not include(two, three)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "#all" do
|
48
|
-
let!(:contains_all) { User.create!(tags: [1], personal_id: 1) }
|
49
|
-
let!(:contains_all_two) { User.create!(tags: [1], personal_id: 2) }
|
50
|
-
let!(:contains_some) { User.create!(tags: [1, 2], personal_id: 2) }
|
51
|
-
|
52
|
-
it "should return any records that match" do
|
53
|
-
query = User.where.all(tags: 1)
|
54
|
-
expect(query).to include(contains_all, contains_all_two)
|
55
|
-
expect(query).to_not include(contains_some)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "allows chaining" do
|
59
|
-
query = User.where.all(tags: 1).where(personal_id: 1)
|
60
|
-
expect(query).to include(contains_all)
|
61
|
-
expect(query).to_not include(contains_all_two, contains_some)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe "Active Record Either Methods" do
|
6
|
-
let!(:one) { User.create! }
|
7
|
-
let!(:two) { User.create! }
|
8
|
-
let!(:three) { User.create! }
|
9
|
-
let!(:profile_l) { ProfileL.create!(user_id: one.id, likes: 100) }
|
10
|
-
let!(:profile_r) { ProfileR.create!(user_id: two.id, dislikes: 50) }
|
11
|
-
|
12
|
-
describe ".either_join/2" do
|
13
|
-
it "Should only only return records that belong to profile L or profile R" do
|
14
|
-
query = User.either_join(:profile_l, :profile_r)
|
15
|
-
expect(query).to include(one, two)
|
16
|
-
expect(query).to_not include(three)
|
17
|
-
end
|
18
|
-
|
19
|
-
context "Alias .either_joins/2" do
|
20
|
-
it "Should only only return records that belong to profile L or profile R" do
|
21
|
-
query = User.either_joins(:profile_l, :profile_r)
|
22
|
-
expect(query).to include(one, two)
|
23
|
-
expect(query).to_not include(three)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context "Through association .either_joins/2" do
|
28
|
-
let!(:four) { User.create! }
|
29
|
-
let!(:group) { Group.create!(users: [four]) }
|
30
|
-
|
31
|
-
it "Should only only return records that belong to profile L or has group" do
|
32
|
-
query = User.either_joins(:profile_l, :groups)
|
33
|
-
expect(query).to include(one, four)
|
34
|
-
expect(query).to_not include(three, two)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe ".either_order/2" do
|
40
|
-
it "Should not exclude anyone who does not have a relationship" do
|
41
|
-
query = User.either_order(:asc, profile_l: :likes, profile_r: :dislikes)
|
42
|
-
expect(query.count).to eq(3)
|
43
|
-
expect(query[0]).to eq(two)
|
44
|
-
expect(query[1]).to eq(one)
|
45
|
-
expect(query[2]).to eq(three)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "Should order users based on their likes and dislikes in ascended order" do
|
49
|
-
query = User.either_order(:asc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
|
50
|
-
expect(query.count).to eq(2)
|
51
|
-
expect(query.first).to eq(two)
|
52
|
-
expect(query.last).to eq(one)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "Should order users based on their likes and dislikes in descending order" do
|
56
|
-
query = User.either_order(:desc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
|
57
|
-
expect(query.first).to eq(one)
|
58
|
-
expect(query.last).to eq(two)
|
59
|
-
end
|
60
|
-
|
61
|
-
context "Alias .either_order/2" do
|
62
|
-
it "Should order users based on their likes and dislikes in ascended order" do
|
63
|
-
query = User.either_orders(:asc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
|
64
|
-
expect(query.count).to eq(2)
|
65
|
-
expect(query.first).to eq(two)
|
66
|
-
expect(query.last).to eq(one)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|