query_filter 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +11 -0
- data/lib/query_filter/base.rb +2 -2
- data/lib/query_filter/rules/scope.rb +53 -41
- data/lib/query_filter/utils/user_conditions.rb +74 -0
- data/lib/query_filter/version.rb +1 -1
- data/lib/query_filter.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ff61e6a1116f9f3945907c8369903c79828ed79
|
4
|
+
data.tar.gz: 907e50842ca587c954365f506905a78bd255bd69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37d2d592b08101e2fdc405ed6a4f1181796af6959d69d73e999c6b4b459011e9dd2c52d01bffe443b7b224a9213e5ae688edf326dfe9e3c64e390f92e53aa068
|
7
|
+
data.tar.gz: f25f30ed5d2e8226e5185c46b37e7652dfe3928bcb3dc45e7bf237168f85af76c0a46bc0ef0fc65f0bdd008c8798e81e4a283c46f1b2684beefbfbc51198f654
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -119,6 +119,7 @@ class OrderFilter < QueryFilter::Base
|
|
119
119
|
# can be restricted using array of values
|
120
120
|
scope :state, only: [:pending, :completed]
|
121
121
|
scope :deleted, only: TRUE_ARRAY
|
122
|
+
scope :archived, if: :allow_archived?
|
122
123
|
|
123
124
|
range :price
|
124
125
|
|
@@ -138,6 +139,10 @@ class OrderFilter < QueryFilter::Base
|
|
138
139
|
query.where(deleted: true)
|
139
140
|
end
|
140
141
|
|
142
|
+
def scope_archived(_)
|
143
|
+
query.where(archived: true)
|
144
|
+
end
|
145
|
+
|
141
146
|
# Filter will be applied when following params present
|
142
147
|
# { price_from: 100, price_to: 200 }
|
143
148
|
def range_price(range)
|
@@ -159,6 +164,12 @@ class OrderFilter < QueryFilter::Base
|
|
159
164
|
def order_by_sort_column(column, direction)
|
160
165
|
query.reorder("orders.#{column} #{direction} NULLS LAST")
|
161
166
|
end
|
167
|
+
|
168
|
+
protected
|
169
|
+
|
170
|
+
def allow_archived?
|
171
|
+
@params[:old] == '1' || params[:state] == 'archived'
|
172
|
+
end
|
162
173
|
end
|
163
174
|
```
|
164
175
|
|
data/lib/query_filter/base.rb
CHANGED
@@ -6,7 +6,7 @@ module QueryFilter
|
|
6
6
|
|
7
7
|
class_attribute :filters
|
8
8
|
|
9
|
-
attr_reader :query
|
9
|
+
attr_reader :query, :params
|
10
10
|
|
11
11
|
def initialize(scope, params)
|
12
12
|
@query = scope
|
@@ -55,7 +55,7 @@ module QueryFilter
|
|
55
55
|
|
56
56
|
def active_filters
|
57
57
|
self.class.filters.each do |filter|
|
58
|
-
yield filter, filter.normalize_params(@params) if filter.
|
58
|
+
yield filter, filter.normalize_params(@params) if filter.can_apply?(self)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
@@ -1,46 +1,58 @@
|
|
1
1
|
# Define filter rules
|
2
2
|
#
|
3
|
-
module QueryFilter
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
params
|
3
|
+
module QueryFilter
|
4
|
+
module Rules
|
5
|
+
class Scope
|
6
|
+
VALIDATON_KEYS = [:in, :only, :format, :if, :unless].freeze
|
7
|
+
|
8
|
+
attr_reader :keys
|
9
|
+
|
10
|
+
def initialize(keys, options = {})
|
11
|
+
@keys = Array(keys)
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def blank_validation?
|
16
|
+
(@options.keys & VALIDATON_KEYS).empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid?(params)
|
20
|
+
values = normalize_params(params)
|
21
|
+
|
22
|
+
checks = []
|
23
|
+
checks << @options[:in].include?(values.first) if @options[:in]
|
24
|
+
checks << @options[:only].include?(values.first) if @options[:only]
|
25
|
+
checks << !values.map(&:blank?).all? if blank_validation?
|
26
|
+
checks << @options[:intersect] & Array.wrap(values) if @options[:intersect]
|
27
|
+
|
28
|
+
!checks.empty? && checks.all?
|
29
|
+
end
|
30
|
+
|
31
|
+
def endpoint
|
32
|
+
@options[:to] || "#{name}_#{key}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def name
|
36
|
+
'scope'.freeze
|
37
|
+
end
|
38
|
+
|
39
|
+
def key
|
40
|
+
@key ||= @keys.first
|
41
|
+
end
|
42
|
+
|
43
|
+
def normalize_params(params)
|
44
|
+
params.values_at(*keys)
|
45
|
+
end
|
46
|
+
|
47
|
+
def can_apply?(target)
|
48
|
+
conditions = QueryFilter::Utils::UserConditions.new(target, @options)
|
49
|
+
|
50
|
+
if conditions.present?
|
51
|
+
conditions.passed?
|
52
|
+
else
|
53
|
+
valid?(target.params)
|
54
|
+
end
|
55
|
+
end
|
44
56
|
end
|
45
57
|
end
|
46
58
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module QueryFilter
|
4
|
+
module Utils
|
5
|
+
class UserConditions
|
6
|
+
attr_reader :target
|
7
|
+
|
8
|
+
def initialize(target, options = {})
|
9
|
+
@target = target
|
10
|
+
@options = options
|
11
|
+
|
12
|
+
@if = Array(options[:if])
|
13
|
+
@unless = Array(options[:unless])
|
14
|
+
end
|
15
|
+
|
16
|
+
def passed?
|
17
|
+
return true if empty?
|
18
|
+
value = target.params
|
19
|
+
|
20
|
+
conditions_lambdas.all? { |c| c.call(target, value) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def empty?
|
24
|
+
@if.empty? && @unless.empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
def present?
|
28
|
+
!empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
def conditions_lambdas
|
32
|
+
@if.map { |c| make_lambda c } +
|
33
|
+
@unless.map { |c| invert_lambda make_lambda c }
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def invert_lambda(l)
|
39
|
+
->(*args, &blk) { !l.call(*args, &blk) }
|
40
|
+
end
|
41
|
+
|
42
|
+
# Filters support:
|
43
|
+
#
|
44
|
+
# Symbols:: A method to call.
|
45
|
+
# Procs:: A proc to call with the object.
|
46
|
+
#
|
47
|
+
# All of these objects are converted into a lambda and handled
|
48
|
+
# the same after this point.
|
49
|
+
def make_lambda(filter)
|
50
|
+
case filter
|
51
|
+
when Symbol
|
52
|
+
->(target, _, &blk) { target.send filter, &blk }
|
53
|
+
when ::Proc
|
54
|
+
make_proc(filter)
|
55
|
+
else
|
56
|
+
raise ArgumentError
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def make_proc(filter)
|
61
|
+
if filter.arity > 1
|
62
|
+
lambda do |target, _, &block|
|
63
|
+
raise ArgumentError unless block
|
64
|
+
target.instance_exec(target, block, &filter)
|
65
|
+
end
|
66
|
+
elsif filter.arity <= 0
|
67
|
+
->(target, _) { target.instance_exec(&filter) }
|
68
|
+
else
|
69
|
+
->(target, _) { target.instance_exec(target, &filter) }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/query_filter/version.rb
CHANGED
data/lib/query_filter.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: query_filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Galeta
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-07-
|
12
|
+
date: 2017-07-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- lib/query_filter/rules/splitter_range.rb
|
165
165
|
- lib/query_filter/utils/date_period.rb
|
166
166
|
- lib/query_filter/utils/scope_range.rb
|
167
|
+
- lib/query_filter/utils/user_conditions.rb
|
167
168
|
- lib/query_filter/version.rb
|
168
169
|
- query_filter.gemspec
|
169
170
|
homepage: https://github.com/psyipm/query_filter
|