activerecord-filter 6.0.0.4 → 6.0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record/filter.rb +43 -30
- data/lib/active_record/filter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ef3877f5047f8f16789ea877a6c421cbeeba6cfba9bea4890bcce2aaaec09b2
|
4
|
+
data.tar.gz: e16bb046aa1e24771725827fddeaaf303e47482ce25433c1a143cc5df8e89bb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 491c8cbdc786ae5ee5ad860e65c5ee58ac7d8b73095e0796dfc95d151ccab22f490b50bcc6b56089725fda38dd897100b02a6398ac41e119c194733eb0e420a4
|
7
|
+
data.tar.gz: 65ff30c9eee91d80198d78ad4b86cb71a14cacec38cc181af1081b3f1a69af84453f61c56413111b0da01affd5fe2fd89174dda10c4032f1ec81f54a6b0e254a
|
data/lib/active_record/filter.rb
CHANGED
@@ -7,23 +7,23 @@ end
|
|
7
7
|
module ActiveRecord::Filter
|
8
8
|
|
9
9
|
delegate :filter, :filter_for, to: :all
|
10
|
-
|
10
|
+
|
11
11
|
def inherited(subclass)
|
12
12
|
super
|
13
13
|
subclass.instance_variable_set('@filters', HashWithIndifferentAccess.new)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def filters
|
17
17
|
@filters
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def filter_on(name, dependent_joins=nil, &block)
|
21
21
|
@filters[name.to_s] = {
|
22
22
|
joins: dependent_joins,
|
23
23
|
block: block
|
24
24
|
}
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
end
|
28
28
|
|
29
29
|
module ActiveRecord
|
@@ -33,7 +33,7 @@ module ActiveRecord
|
|
33
33
|
custom = []
|
34
34
|
[build_filter_joins(klass, filters, [], custom), custom]
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def self.build_filter_joins(klass, filters, relations=[], custom=[])
|
38
38
|
if filters.is_a?(Array)
|
39
39
|
filters.each { |f| build_filter_joins(klass, f, relations, custom) }.compact
|
@@ -41,7 +41,7 @@ module ActiveRecord
|
|
41
41
|
filters.each do |key, value|
|
42
42
|
if klass.filters.has_key?(key.to_sym)
|
43
43
|
js = klass.filters.dig(key.to_sym, :joins)
|
44
|
-
|
44
|
+
|
45
45
|
if js.is_a?(Array)
|
46
46
|
js.each do |j|
|
47
47
|
if j.is_a?(String)
|
@@ -62,6 +62,12 @@ module ActiveRecord
|
|
62
62
|
relations << {
|
63
63
|
key => build_filter_joins(reflection.klass, value, [], custom)
|
64
64
|
}
|
65
|
+
elsif value.is_a?(Array)
|
66
|
+
value.each do |v|
|
67
|
+
relations << {
|
68
|
+
key => build_filter_joins(reflection.klass, v, [], custom)
|
69
|
+
}
|
70
|
+
end
|
65
71
|
elsif value != true && value != false && value != 'true' && value != 'false' && !value.nil?
|
66
72
|
relations << key
|
67
73
|
end
|
@@ -73,10 +79,10 @@ module ActiveRecord
|
|
73
79
|
end
|
74
80
|
end
|
75
81
|
end
|
76
|
-
|
82
|
+
|
77
83
|
relations
|
78
84
|
end
|
79
|
-
|
85
|
+
|
80
86
|
def build_from_filter_hash(attributes, relation_trail, alias_tracker)
|
81
87
|
if attributes.is_a?(Array)
|
82
88
|
node = build_from_filter_hash(attributes.shift, relation_trail, alias_tracker)
|
@@ -92,12 +98,19 @@ module ActiveRecord
|
|
92
98
|
end
|
93
99
|
elsif n[0] == 'OR'
|
94
100
|
node = Arel::Nodes::Grouping.new(node).or(Arel::Nodes::Grouping.new(n[1]))
|
101
|
+
elsif !n[0].is_a?(String)
|
102
|
+
n[0] = build_from_filter_hash(n[0], relation_trail, alias_tracker)
|
103
|
+
if node.is_a?(Arel::Nodes::And)
|
104
|
+
node.children.push(n[0])
|
105
|
+
else
|
106
|
+
node = node.and(n[0])
|
107
|
+
end
|
95
108
|
else
|
96
109
|
raise 'lll'
|
97
110
|
end
|
98
111
|
n = attributes.shift(2)
|
99
112
|
end
|
100
|
-
|
113
|
+
|
101
114
|
node
|
102
115
|
elsif attributes.is_a?(Hash)
|
103
116
|
expand_from_filter_hash(attributes, relation_trail, alias_tracker)
|
@@ -105,10 +118,10 @@ module ActiveRecord
|
|
105
118
|
expand_from_filter_hash({id: attributes}, relation_trail, alias_tracker)
|
106
119
|
end
|
107
120
|
end
|
108
|
-
|
121
|
+
|
109
122
|
def expand_from_filter_hash(attributes, relation_trail, alias_tracker)
|
110
123
|
klass = table.send(:klass)
|
111
|
-
|
124
|
+
|
112
125
|
children = attributes.flat_map do |key, value|
|
113
126
|
if custom_filter = klass.filters[key]
|
114
127
|
self.instance_exec(klass, table, key, value, relation_trail, alias_tracker, &custom_filter[:block])
|
@@ -124,7 +137,7 @@ module ActiveRecord
|
|
124
137
|
raise ActiveRecord::UnkownFilterError.new("Unkown filter \"#{key}\" for #{klass}.")
|
125
138
|
end
|
126
139
|
end
|
127
|
-
|
140
|
+
|
128
141
|
children.compact!
|
129
142
|
if children.size > 1
|
130
143
|
Arel::Nodes::And.new(children)
|
@@ -132,19 +145,19 @@ module ActiveRecord
|
|
132
145
|
children.first
|
133
146
|
end
|
134
147
|
end
|
135
|
-
|
148
|
+
|
136
149
|
def expand_filter_for_column(key, column, value, relation_trail)
|
137
150
|
attribute = table.arel_attribute(column.name)
|
138
151
|
relation_trail.each do |rt|
|
139
152
|
attribute = Arel::Attributes::Relation.new(attribute, rt)
|
140
153
|
end
|
141
|
-
|
154
|
+
|
142
155
|
if column.type == :json || column.type == :jsonb
|
143
156
|
names = key.to_s.split('.')
|
144
157
|
names.shift
|
145
158
|
attribute = attribute.dig(names)
|
146
159
|
end
|
147
|
-
|
160
|
+
|
148
161
|
if value.is_a?(Hash)
|
149
162
|
nodes = value.map do |subkey, subvalue|
|
150
163
|
expand_filter_for_arel_attribute(column, attribute, subkey, subvalue)
|
@@ -164,9 +177,9 @@ module ActiveRecord
|
|
164
177
|
else
|
165
178
|
raise ActiveRecord::UnkownFilterError.new("Unkown type for #{column}. (type #{value.class})")
|
166
179
|
end
|
167
|
-
|
180
|
+
|
168
181
|
end
|
169
|
-
|
182
|
+
|
170
183
|
def expand_filter_for_arel_attribute(column, attribute, key, value)
|
171
184
|
case key.to_sym
|
172
185
|
when :contains
|
@@ -197,7 +210,7 @@ module ActiveRecord
|
|
197
210
|
# elsif # EWKT
|
198
211
|
# elsif # WKT
|
199
212
|
# end
|
200
|
-
|
213
|
+
|
201
214
|
# TODO us above if to determin if SRID sent
|
202
215
|
geometry_value = if value.is_a?(Hash)
|
203
216
|
Arel::Nodes::NamedFunction.new('ST_SetSRID', [Arel::Nodes::NamedFunction.new('ST_GeomFromGeoJSON', [Arel::Nodes.build_quoted(JSON.generate(subvalue))]), 4326])
|
@@ -244,7 +257,7 @@ module ActiveRecord
|
|
244
257
|
raise "Not Supported: #{key.to_sym} on column \"#{column.name}\" of type #{column.type}"
|
245
258
|
end
|
246
259
|
end
|
247
|
-
|
260
|
+
|
248
261
|
def expand_filter_for_relationship(relation, value, relation_trail, alias_tracker)
|
249
262
|
case relation.macro
|
250
263
|
when :has_many
|
@@ -282,14 +295,14 @@ module ActiveRecord
|
|
282
295
|
))
|
283
296
|
builder.build_from_filter_hash(value, relation_trail + [relation.name], alias_tracker)
|
284
297
|
end
|
285
|
-
|
286
|
-
|
298
|
+
|
299
|
+
|
287
300
|
def expand_filter_for_join_table(relation, value, relation_trail, alias_tracker)
|
288
301
|
relation = relation.active_record._reflections[relation.active_record._reflections[relation.name.to_s].send(:delegate_reflection).options[:through].to_s]
|
289
302
|
STDOUT.puts [
|
290
303
|
relation.table_name,
|
291
304
|
relation.alias_candidate(table.send(:arel_table).name)
|
292
|
-
|
305
|
+
|
293
306
|
].inspect
|
294
307
|
builder = self.class.new(TableMetadata.new(
|
295
308
|
relation.klass,
|
@@ -321,7 +334,7 @@ module ActiveRecord
|
|
321
334
|
else
|
322
335
|
raise ArgumentError, "Unsupported argument type: #{filters.inspect} (#{filters.class})"
|
323
336
|
end
|
324
|
-
|
337
|
+
|
325
338
|
WhereClause.new(parts)
|
326
339
|
end
|
327
340
|
|
@@ -339,12 +352,12 @@ class ActiveRecord::Relation
|
|
339
352
|
@filters = []
|
340
353
|
super
|
341
354
|
end
|
342
|
-
|
355
|
+
|
343
356
|
def initialize_copy(other)
|
344
357
|
@filters = @filters.deep_dup
|
345
358
|
super
|
346
359
|
end
|
347
|
-
|
360
|
+
|
348
361
|
def clean_filters(value)
|
349
362
|
if value.class.name == 'ActionController::Parameters'.freeze
|
350
363
|
value.to_unsafe_h
|
@@ -357,32 +370,32 @@ class ActiveRecord::Relation
|
|
357
370
|
|
358
371
|
def filter(filters)
|
359
372
|
filters = clean_filters(filters)
|
360
|
-
|
373
|
+
|
361
374
|
if filters.nil? || filters.empty?
|
362
375
|
self
|
363
376
|
else
|
364
377
|
spawn.filter!(filters)
|
365
378
|
end
|
366
379
|
end
|
367
|
-
|
380
|
+
|
368
381
|
def filter!(filters)
|
369
382
|
js = ActiveRecord::PredicateBuilder.filter_joins(klass, filters)
|
370
383
|
js.each { |j| joins!(j) if j.present? }
|
371
384
|
@filters << filters
|
372
385
|
self
|
373
386
|
end
|
374
|
-
|
387
|
+
|
375
388
|
def filter_clause_factory
|
376
389
|
@filter_clause_factory ||= FilterClauseFactory.new(klass, predicate_builder)
|
377
390
|
end
|
378
|
-
|
391
|
+
|
379
392
|
def build_arel(aliases)
|
380
393
|
arel = super
|
381
394
|
my_alias_tracker = ActiveRecord::Associations::AliasTracker.create(connection, table.name, [])
|
382
395
|
build_filters(arel, my_alias_tracker)
|
383
396
|
arel
|
384
397
|
end
|
385
|
-
|
398
|
+
|
386
399
|
def build_filters(manager, aliases)
|
387
400
|
@filters.each do |filters|
|
388
401
|
manager.where(filter_clause_factory.build(filters, alias_tracker).ast)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.0.0.
|
4
|
+
version: 6.0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Bracy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|