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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84c7c2dd2053a60050b2d42501e1b45d3733e4d0
4
- data.tar.gz: d4083887f336028f260e9d93223052dd23e2fcbd
3
+ metadata.gz: 9ff61e6a1116f9f3945907c8369903c79828ed79
4
+ data.tar.gz: 907e50842ca587c954365f506905a78bd255bd69
5
5
  SHA512:
6
- metadata.gz: 742cf53b1f36fddbbbc5e020b63b194f08613b7b53c560c3497bb61a601324e6217110c7ee2d13d8ec7f1c0688126955657d2e02fc5b800bf474f83b1fc58a8c
7
- data.tar.gz: b99ce1d22816aac5a81e7ba72a0ddbb61b691a14475f56a89599101555b1cb350989c31c26cdda14541ab0fd55e9d015deb9448998360254ad2d7af316986692
6
+ metadata.gz: 37d2d592b08101e2fdc405ed6a4f1181796af6959d69d73e999c6b4b459011e9dd2c52d01bffe443b7b224a9213e5ae688edf326dfe9e3c64e390f92e53aa068
7
+ data.tar.gz: f25f30ed5d2e8226e5185c46b37e7652dfe3928bcb3dc45e7bf237168f85af76c0a46bc0ef0fc65f0bdd008c8798e81e4a283c46f1b2684beefbfbc51198f654
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_filter (0.1.3)
4
+ query_filter (0.1.4)
5
5
  activesupport (>= 4.0)
6
6
 
7
7
  GEM
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
 
@@ -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.valid?(@params)
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::Rules
4
- class Scope
5
- VALIDATON_KEYS = [:in, :only, :format].freeze
6
-
7
- attr_reader :keys
8
-
9
- def initialize(keys, options = {})
10
- @keys = Array(keys)
11
- @options = options
12
- end
13
-
14
- def blank_validation?
15
- (@options.keys & VALIDATON_KEYS).empty?
16
- end
17
-
18
- def valid?(params)
19
- values = normalize_params(params)
20
-
21
- checks = []
22
- checks << @options[:in].include?(values.first) if @options[:in]
23
- checks << @options[:only].include?(values.first) if @options[:only]
24
- checks << !values.map(&:blank?).all? if blank_validation?
25
- checks << @options[:intersect] & Array.wrap(values) if @options[:intersect]
26
-
27
- !checks.empty? && checks.all?
28
- end
29
-
30
- def endpoint
31
- @options[:to] || "#{name}_#{key}"
32
- end
33
-
34
- def name
35
- 'scope'.freeze
36
- end
37
-
38
- def key
39
- @key ||= @keys.first
40
- end
41
-
42
- def normalize_params(params)
43
- params.values_at(*keys)
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
@@ -1,3 +1,3 @@
1
1
  module QueryFilter
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
data/lib/query_filter.rb CHANGED
@@ -19,5 +19,6 @@ module QueryFilter
19
19
  module Utils
20
20
  autoload :DatePeriod, 'query_filter/utils/date_period'
21
21
  autoload :ScopeRange, 'query_filter/utils/scope_range'
22
+ autoload :UserConditions, 'query_filter/utils/user_conditions'
22
23
  end
23
24
  end
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.3
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-10 00:00:00.000000000 Z
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