query_filter 0.1.8 → 0.2.0
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 +5 -5
- data/.ruby-version +1 -0
- data/.travis.yml +1 -1
- data/Gemfile.lock +39 -40
- data/README.md +31 -0
- data/lib/query_filter/base.rb +2 -0
- data/lib/query_filter/rules/date_range.rb +31 -26
- data/lib/query_filter/rules/order_by.rb +18 -14
- data/lib/query_filter/rules/range.rb +30 -26
- data/lib/query_filter/rules/scope.rb +3 -1
- data/lib/query_filter/rules/splitter_range.rb +35 -30
- data/lib/query_filter/utils/date_normalizer.rb +21 -8
- data/lib/query_filter/utils/date_period.rb +10 -8
- data/lib/query_filter/utils/scope_range.rb +37 -33
- data/lib/query_filter/utils/user_conditions.rb +4 -3
- data/lib/query_filter/version.rb +3 -1
- data/lib/query_filter.rb +12 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f281a64c42dfafb2f5478b7dc850e4d09fbc177357d85590a45e2abaa55b91ea
|
|
4
|
+
data.tar.gz: c9823c4351c0757a97eeb0d851e22980da495ecdc13784002f6e2ef3390f6403
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 38bdd2e28381998445c90a2c86fff1f0d3513df6b995ffbafc1399cf3b3460d5fb25b462602ff3b48426597e0a1aba0951e2adcc4cc3eb4c42ca943bf0c0bff8
|
|
7
|
+
data.tar.gz: a1aeac38a34238ecb73269246e716501e01897f420181c1a1cdd632a2b5b803a976d26c6d6e012389e9ca2e7b681f573081835df489ed9acbe85959aae928cc8
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.5.5
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
query_filter (0.
|
|
4
|
+
query_filter (0.2.0)
|
|
5
5
|
activesupport (>= 4.0)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
activemodel (5.
|
|
11
|
-
activesupport (= 5.
|
|
12
|
-
activerecord (5.
|
|
13
|
-
activemodel (= 5.
|
|
14
|
-
activesupport (= 5.
|
|
15
|
-
arel (
|
|
16
|
-
activesupport (5.
|
|
10
|
+
activemodel (5.2.3)
|
|
11
|
+
activesupport (= 5.2.3)
|
|
12
|
+
activerecord (5.2.3)
|
|
13
|
+
activemodel (= 5.2.3)
|
|
14
|
+
activesupport (= 5.2.3)
|
|
15
|
+
arel (>= 9.0)
|
|
16
|
+
activesupport (5.2.3)
|
|
17
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
18
|
-
i18n (
|
|
18
|
+
i18n (>= 0.7, < 2)
|
|
19
19
|
minitest (~> 5.1)
|
|
20
20
|
tzinfo (~> 1.1)
|
|
21
|
-
arel (
|
|
22
|
-
coderay (1.1.
|
|
23
|
-
concurrent-ruby (1.
|
|
21
|
+
arel (9.0.0)
|
|
22
|
+
coderay (1.1.2)
|
|
23
|
+
concurrent-ruby (1.1.5)
|
|
24
24
|
diff-lcs (1.3)
|
|
25
|
-
ffi (1.
|
|
25
|
+
ffi (1.10.0)
|
|
26
26
|
formatador (0.2.5)
|
|
27
|
-
guard (2.
|
|
27
|
+
guard (2.15.0)
|
|
28
28
|
formatador (>= 0.2.4)
|
|
29
29
|
listen (>= 2.7, < 4.0)
|
|
30
|
-
lumberjack (
|
|
30
|
+
lumberjack (>= 1.0.12, < 2.0)
|
|
31
31
|
nenv (~> 0.1)
|
|
32
32
|
notiffany (~> 0.0)
|
|
33
33
|
pry (>= 0.9.12)
|
|
@@ -38,46 +38,45 @@ GEM
|
|
|
38
38
|
guard (~> 2.1)
|
|
39
39
|
guard-compat (~> 1.1)
|
|
40
40
|
rspec (>= 2.99.0, < 4.0)
|
|
41
|
-
i18n (
|
|
41
|
+
i18n (1.6.0)
|
|
42
|
+
concurrent-ruby (~> 1.0)
|
|
42
43
|
listen (3.1.5)
|
|
43
44
|
rb-fsevent (~> 0.9, >= 0.9.4)
|
|
44
45
|
rb-inotify (~> 0.9, >= 0.9.7)
|
|
45
46
|
ruby_dep (~> 1.2)
|
|
46
|
-
lumberjack (1.0.
|
|
47
|
-
method_source (0.
|
|
48
|
-
minitest (5.
|
|
47
|
+
lumberjack (1.0.13)
|
|
48
|
+
method_source (0.9.2)
|
|
49
|
+
minitest (5.11.3)
|
|
49
50
|
nenv (0.3.0)
|
|
50
51
|
notiffany (0.1.1)
|
|
51
52
|
nenv (~> 0.1)
|
|
52
53
|
shellany (~> 0.0)
|
|
53
|
-
pry (0.
|
|
54
|
+
pry (0.12.2)
|
|
54
55
|
coderay (~> 1.1.0)
|
|
55
|
-
method_source (~> 0.
|
|
56
|
-
slop (~> 3.4)
|
|
56
|
+
method_source (~> 0.9.0)
|
|
57
57
|
rake (10.5.0)
|
|
58
58
|
rb-fsevent (0.9.8)
|
|
59
|
-
rb-inotify (0.
|
|
60
|
-
ffi (
|
|
61
|
-
rspec (3.
|
|
62
|
-
rspec-core (~> 3.
|
|
63
|
-
rspec-expectations (~> 3.
|
|
64
|
-
rspec-mocks (~> 3.
|
|
65
|
-
rspec-core (3.
|
|
66
|
-
rspec-support (~> 3.
|
|
67
|
-
rspec-expectations (3.
|
|
59
|
+
rb-inotify (0.10.0)
|
|
60
|
+
ffi (~> 1.0)
|
|
61
|
+
rspec (3.8.0)
|
|
62
|
+
rspec-core (~> 3.8.0)
|
|
63
|
+
rspec-expectations (~> 3.8.0)
|
|
64
|
+
rspec-mocks (~> 3.8.0)
|
|
65
|
+
rspec-core (3.8.0)
|
|
66
|
+
rspec-support (~> 3.8.0)
|
|
67
|
+
rspec-expectations (3.8.3)
|
|
68
68
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
69
|
-
rspec-support (~> 3.
|
|
70
|
-
rspec-mocks (3.
|
|
69
|
+
rspec-support (~> 3.8.0)
|
|
70
|
+
rspec-mocks (3.8.0)
|
|
71
71
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
72
|
-
rspec-support (~> 3.
|
|
73
|
-
rspec-support (3.
|
|
72
|
+
rspec-support (~> 3.8.0)
|
|
73
|
+
rspec-support (3.8.0)
|
|
74
74
|
ruby_dep (1.5.0)
|
|
75
75
|
shellany (0.0.1)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
thor (0.19.4)
|
|
76
|
+
sqlite3 (1.4.1)
|
|
77
|
+
thor (0.20.3)
|
|
79
78
|
thread_safe (0.3.6)
|
|
80
|
-
tzinfo (1.2.
|
|
79
|
+
tzinfo (1.2.5)
|
|
81
80
|
thread_safe (~> 0.1)
|
|
82
81
|
|
|
83
82
|
PLATFORMS
|
|
@@ -94,4 +93,4 @@ DEPENDENCIES
|
|
|
94
93
|
sqlite3 (~> 1.3, >= 1.3.13)
|
|
95
94
|
|
|
96
95
|
BUNDLED WITH
|
|
97
|
-
1.16.
|
|
96
|
+
1.16.2
|
data/README.md
CHANGED
|
@@ -26,6 +26,37 @@ class Order < ActiveRecord::Base
|
|
|
26
26
|
end
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
Where OrderFilter class looks like:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
# app/filters/order_filter.rb
|
|
33
|
+
#
|
|
34
|
+
class OrderFilter < QueryFilter::Base
|
|
35
|
+
scope :customer_id
|
|
36
|
+
scope :service
|
|
37
|
+
|
|
38
|
+
range :total
|
|
39
|
+
|
|
40
|
+
date_range :completed_at
|
|
41
|
+
|
|
42
|
+
def scope_customer_id(value)
|
|
43
|
+
query.where(customer_id: value)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def scope_service(value)
|
|
47
|
+
query.where(service_id: value.to_i)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def range_total(range)
|
|
51
|
+
query.where(range.query('orders.total'))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def date_range_completed_at(period)
|
|
55
|
+
query.where(completed_at: period.range_original)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
29
60
|
## Installation
|
|
30
61
|
|
|
31
62
|
Add this line to your application's Gemfile:
|
data/lib/query_filter/base.rb
CHANGED
|
@@ -1,42 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Parse date range params
|
|
2
4
|
#
|
|
3
5
|
# date_range :created_at, keys: [:start_date, :end_date]
|
|
4
6
|
#
|
|
5
7
|
# date_range :last_login_date
|
|
6
8
|
#
|
|
7
|
-
module QueryFilter
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
module QueryFilter
|
|
10
|
+
module Rules
|
|
11
|
+
class DateRange < Scope
|
|
12
|
+
def name
|
|
13
|
+
'date_range'
|
|
14
|
+
end
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
def valid?(values)
|
|
17
|
+
period = build_period_from_params(values)
|
|
18
|
+
!(period.nil? || period.default?)
|
|
19
|
+
end
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
def normalize_params(values)
|
|
22
|
+
build_period_from_params(values)
|
|
23
|
+
end
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
protected
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
def build_period_from_params(params)
|
|
28
|
+
if params[key].present?
|
|
29
|
+
QueryFilter::Utils::DatePeriod.parse_from_string(params[key], @options[:format])
|
|
30
|
+
elsif keys_start_end_dates_exists?(params)
|
|
31
|
+
QueryFilter::Utils::DatePeriod.new(*values_start_end_dates(params), @options[:format])
|
|
32
|
+
end
|
|
29
33
|
end
|
|
30
|
-
end
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
def keys_start_end_dates_exists?(params)
|
|
36
|
+
values = values_start_end_dates(params)
|
|
37
|
+
!values.nil? && values.size == 2
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def values_start_end_dates(params)
|
|
41
|
+
return unless @options[:keys]
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
params.values_at(*@options[:keys])
|
|
43
|
+
params.values_at(*@options[:keys])
|
|
44
|
+
end
|
|
40
45
|
end
|
|
41
46
|
end
|
|
42
47
|
end
|
|
@@ -1,23 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Define order by filter rule
|
|
2
4
|
#
|
|
3
|
-
module QueryFilter
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
module QueryFilter
|
|
6
|
+
module Rules
|
|
7
|
+
class OrderBy < Scope
|
|
8
|
+
DIRECTIONS = %w[asc desc].freeze
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
def name
|
|
11
|
+
'order_by'
|
|
12
|
+
end
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
def valid?(params)
|
|
15
|
+
params[key].present? && DIRECTIONS.include?(params[direction_key].try(:downcase))
|
|
16
|
+
end
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
def direction_key
|
|
19
|
+
@direction_key ||= (@options[:via] || 'sort_direction').to_sym
|
|
20
|
+
end
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
def normalize_params(values)
|
|
23
|
+
[values[key], values[direction_key]]
|
|
24
|
+
end
|
|
21
25
|
end
|
|
22
26
|
end
|
|
23
27
|
end
|
|
@@ -1,38 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Define range filter rule
|
|
2
4
|
#
|
|
3
|
-
module QueryFilter
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
module QueryFilter
|
|
6
|
+
module Rules
|
|
7
|
+
class Range < Scope
|
|
8
|
+
def initialize(keys, options = {})
|
|
9
|
+
@key = Array(keys).first
|
|
10
|
+
@keys = [key_from, key_to]
|
|
11
|
+
@options = options
|
|
12
|
+
end
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
def name
|
|
15
|
+
'range'
|
|
16
|
+
end
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
def valid?(values)
|
|
19
|
+
filter = build_range_from_params(values)
|
|
20
|
+
filter.valid?
|
|
21
|
+
end
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
def normalize_params(values)
|
|
24
|
+
build_range_from_params(values)
|
|
25
|
+
end
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
protected
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
def build_range_from_params(params)
|
|
30
|
+
QueryFilter::Utils::ScopeRange.new(key, params)
|
|
31
|
+
end
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
def key_from
|
|
34
|
+
@key_from ||= "#{key}_from".to_sym
|
|
35
|
+
end
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
def key_to
|
|
38
|
+
@key_to ||= "#{key}_to".to_sym
|
|
39
|
+
end
|
|
36
40
|
end
|
|
37
41
|
end
|
|
38
42
|
end
|
|
@@ -1,44 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
class SplitterRange < Scope
|
|
3
|
-
class RangeParam
|
|
4
|
-
SPLITTER = /[;-]/
|
|
1
|
+
# frozen_string_literal: true
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
module QueryFilter
|
|
4
|
+
module Rules
|
|
5
|
+
class SplitterRange < Scope
|
|
6
|
+
class RangeParam
|
|
7
|
+
SPLITTER = /[;-]/.freeze
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
def initialize(value)
|
|
10
|
+
@value = value
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def valid?
|
|
14
|
+
@value.present? && items.size == 2
|
|
15
|
+
end
|
|
13
16
|
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
def range
|
|
18
|
+
::Range.new(*items)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def items
|
|
22
|
+
@items ||= @value.split(SPLITTER).map(&:strip).map(&:to_i)
|
|
23
|
+
end
|
|
16
24
|
end
|
|
17
25
|
|
|
18
|
-
def
|
|
19
|
-
|
|
26
|
+
def name
|
|
27
|
+
'splitter_range'
|
|
20
28
|
end
|
|
21
|
-
end
|
|
22
29
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
def valid?(values)
|
|
31
|
+
period = build_period_from_params(values)
|
|
32
|
+
!period.nil?
|
|
33
|
+
end
|
|
26
34
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
end
|
|
35
|
+
def normalize_params(values)
|
|
36
|
+
build_period_from_params(values)
|
|
37
|
+
end
|
|
31
38
|
|
|
32
|
-
|
|
33
|
-
build_period_from_params(values)
|
|
34
|
-
end
|
|
39
|
+
protected
|
|
35
40
|
|
|
36
|
-
|
|
41
|
+
def build_period_from_params(values)
|
|
42
|
+
param = RangeParam.new(values[key])
|
|
43
|
+
return unless param.valid?
|
|
37
44
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return unless param.valid?
|
|
41
|
-
param
|
|
45
|
+
param
|
|
46
|
+
end
|
|
42
47
|
end
|
|
43
48
|
end
|
|
44
49
|
end
|
|
@@ -1,20 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module QueryFilter
|
|
2
4
|
module Utils
|
|
3
5
|
class DateNormalizer
|
|
4
|
-
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
|
|
5
6
|
PG_MIN_YEAR = -4713
|
|
6
|
-
PG_MAX_YEAR =
|
|
7
|
+
PG_MAX_YEAR = 294_276
|
|
7
8
|
|
|
8
9
|
attr_reader :date, :format
|
|
9
10
|
|
|
10
|
-
def initialize(date, format)
|
|
11
|
+
def initialize(date, format = nil)
|
|
11
12
|
@date = date
|
|
12
13
|
@format = format
|
|
13
|
-
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def parsed_value
|
|
17
|
+
@parsed_value ||= parse
|
|
14
18
|
end
|
|
15
19
|
|
|
16
20
|
def normalize
|
|
17
|
-
valid?(
|
|
21
|
+
valid?(parsed_value) ? parsed_value : default_date
|
|
18
22
|
end
|
|
19
23
|
|
|
20
24
|
private
|
|
@@ -31,13 +35,22 @@ module QueryFilter
|
|
|
31
35
|
|
|
32
36
|
def parse
|
|
33
37
|
return date if date?
|
|
38
|
+
return default_date if date.blank?
|
|
39
|
+
|
|
40
|
+
[@format].concat(QueryFilter.datetime_formats).compact.each do |format|
|
|
41
|
+
value = safe_parse_date(date, format)
|
|
42
|
+
return value if value
|
|
43
|
+
end
|
|
34
44
|
|
|
35
|
-
time = DateTime.strptime(date, format)
|
|
36
|
-
Time.zone.parse(time.strftime(TIME_FORMAT))
|
|
37
|
-
rescue StandardError => _e
|
|
38
45
|
default_date
|
|
39
46
|
end
|
|
40
47
|
|
|
48
|
+
def safe_parse_date(string, format)
|
|
49
|
+
DateTime.strptime(string, format)
|
|
50
|
+
rescue ArgumentError => _e
|
|
51
|
+
nil
|
|
52
|
+
end
|
|
53
|
+
|
|
41
54
|
def default_date
|
|
42
55
|
Time.zone.today
|
|
43
56
|
end
|
|
@@ -3,14 +3,12 @@
|
|
|
3
3
|
module QueryFilter
|
|
4
4
|
module Utils
|
|
5
5
|
class DatePeriod
|
|
6
|
-
attr_reader :date_from_raw, :date_to_raw
|
|
7
|
-
|
|
8
|
-
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
|
|
6
|
+
attr_reader :date_from_raw, :date_to_raw
|
|
9
7
|
|
|
10
8
|
def initialize(date_from = nil, date_to = nil, format = nil)
|
|
11
|
-
@format = (format.blank? ? QueryFilter.date_period_format : format)
|
|
12
9
|
@date_from_raw = date_from
|
|
13
10
|
@date_to_raw = date_to
|
|
11
|
+
@format = format
|
|
14
12
|
end
|
|
15
13
|
|
|
16
14
|
def range
|
|
@@ -42,11 +40,11 @@ module QueryFilter
|
|
|
42
40
|
end
|
|
43
41
|
|
|
44
42
|
def datefrom
|
|
45
|
-
@datefrom ||= I18n.l(date_from, format:
|
|
43
|
+
@datefrom ||= I18n.l(date_from, format: date_display_format)
|
|
46
44
|
end
|
|
47
45
|
|
|
48
46
|
def dateto
|
|
49
|
-
@dateto ||= I18n.l(date_to, format:
|
|
47
|
+
@dateto ||= I18n.l(date_to, format: date_display_format)
|
|
50
48
|
end
|
|
51
49
|
|
|
52
50
|
def to_param
|
|
@@ -72,15 +70,19 @@ module QueryFilter
|
|
|
72
70
|
return value if value.is_a?(DatePeriod)
|
|
73
71
|
|
|
74
72
|
if value.blank?
|
|
75
|
-
new
|
|
73
|
+
new
|
|
76
74
|
else
|
|
77
75
|
dates = value.to_s.split(QueryFilter.date_period_splitter).map(&:strip)
|
|
78
|
-
new(dates[0], dates[1], format)
|
|
76
|
+
new(dates[0], dates[1], format || QueryFilter.date_period_format)
|
|
79
77
|
end
|
|
80
78
|
end
|
|
81
79
|
|
|
82
80
|
private
|
|
83
81
|
|
|
82
|
+
def date_display_format
|
|
83
|
+
@format || QueryFilter.date_display_format
|
|
84
|
+
end
|
|
85
|
+
|
|
84
86
|
def normalize_date(date)
|
|
85
87
|
QueryFilter::Utils::DateNormalizer.new(date, @format).normalize
|
|
86
88
|
end
|
|
@@ -1,42 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
# Usage:
|
|
3
|
-
#
|
|
4
|
-
# range = Utils::ScopeRange.new(:orders, { orders_from: 1, orders_to: 44 })
|
|
5
|
-
# range.query('orders_count')
|
|
6
|
-
#
|
|
7
|
-
class ScopeRange
|
|
8
|
-
def initialize(name, options = {})
|
|
9
|
-
@name = name
|
|
10
|
-
@options = options
|
|
11
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
12
2
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
module QueryFilter
|
|
4
|
+
module Utils
|
|
5
|
+
# Usage:
|
|
6
|
+
#
|
|
7
|
+
# range = Utils::ScopeRange.new(:orders, { orders_from: 1, orders_to: 44 })
|
|
8
|
+
# range.query('orders_count')
|
|
9
|
+
#
|
|
10
|
+
class ScopeRange
|
|
11
|
+
def initialize(name, options = {})
|
|
12
|
+
@name = name
|
|
13
|
+
@options = options
|
|
14
|
+
end
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
def name_from
|
|
17
|
+
@name_from ||= "#{@name}_from".to_sym
|
|
18
|
+
end
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
def name_to
|
|
21
|
+
@name_to ||= "#{@name}_to".to_sym
|
|
22
|
+
end
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
def value_from
|
|
25
|
+
@options[name_from]
|
|
26
|
+
end
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
def value_to
|
|
29
|
+
@options[name_to]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def valid?
|
|
33
|
+
@options && (value_from.present? || value_to.present?)
|
|
34
|
+
end
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
def query(column)
|
|
37
|
+
if value_from.present? && value_to.present?
|
|
38
|
+
["#{column} BETWEEN ? AND ?", value_from, value_to]
|
|
39
|
+
elsif value_from.present?
|
|
40
|
+
["#{column} >= ?", value_from]
|
|
41
|
+
elsif value_to.present?
|
|
42
|
+
["#{column} <= ?", value_to]
|
|
43
|
+
end
|
|
40
44
|
end
|
|
41
45
|
end
|
|
42
46
|
end
|
|
@@ -15,8 +15,8 @@ module QueryFilter
|
|
|
15
15
|
|
|
16
16
|
def passed?
|
|
17
17
|
return true if empty?
|
|
18
|
-
value = target.params
|
|
19
18
|
|
|
19
|
+
value = target.params
|
|
20
20
|
conditions_lambdas.all? { |c| c.call(target, value) }
|
|
21
21
|
end
|
|
22
22
|
|
|
@@ -35,8 +35,8 @@ module QueryFilter
|
|
|
35
35
|
|
|
36
36
|
private
|
|
37
37
|
|
|
38
|
-
def invert_lambda(
|
|
39
|
-
->(*args, &blk) { !
|
|
38
|
+
def invert_lambda(lblock)
|
|
39
|
+
->(*args, &blk) { !lblock.call(*args, &blk) }
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# Filters support:
|
|
@@ -61,6 +61,7 @@ module QueryFilter
|
|
|
61
61
|
if filter.arity > 1
|
|
62
62
|
lambda do |target, _, &block|
|
|
63
63
|
raise ArgumentError unless block
|
|
64
|
+
|
|
64
65
|
target.instance_exec(target, block, &filter)
|
|
65
66
|
end
|
|
66
67
|
elsif filter.arity <= 0
|
data/lib/query_filter/version.rb
CHANGED
data/lib/query_filter.rb
CHANGED
|
@@ -31,6 +31,18 @@ module QueryFilter
|
|
|
31
31
|
mattr_accessor :date_period_splitter
|
|
32
32
|
self.date_period_splitter = 'to'
|
|
33
33
|
|
|
34
|
+
mattr_accessor :date_display_format
|
|
35
|
+
self.date_display_format = '%Y-%m-%d %H:%M'
|
|
36
|
+
|
|
37
|
+
mattr_accessor :datetime_formats
|
|
38
|
+
self.datetime_formats = %w[
|
|
39
|
+
%Y-%m-%dT%H:%M:%S.%L%z
|
|
40
|
+
%Y-%m-%dT%H:%M:%S%z
|
|
41
|
+
%Y-%m-%d %H:%M:%S
|
|
42
|
+
%Y-%m-%d %H:%M
|
|
43
|
+
%Y-%m-%d
|
|
44
|
+
]
|
|
45
|
+
|
|
34
46
|
# Default way to setup QueryFilter
|
|
35
47
|
# @example
|
|
36
48
|
# QueryFilter.setup do |config|
|
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.
|
|
4
|
+
version: 0.2.0
|
|
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:
|
|
12
|
+
date: 2019-05-10 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: bundler
|
|
@@ -146,6 +146,7 @@ files:
|
|
|
146
146
|
- ".gitignore"
|
|
147
147
|
- ".rspec"
|
|
148
148
|
- ".rubocop.yml"
|
|
149
|
+
- ".ruby-version"
|
|
149
150
|
- ".travis.yml"
|
|
150
151
|
- Gemfile
|
|
151
152
|
- Gemfile.lock
|
|
@@ -188,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
188
189
|
version: '0'
|
|
189
190
|
requirements: []
|
|
190
191
|
rubyforge_project:
|
|
191
|
-
rubygems_version: 2.
|
|
192
|
+
rubygems_version: 2.7.6.2
|
|
192
193
|
signing_key:
|
|
193
194
|
specification_version: 4
|
|
194
195
|
summary: ActiveRecord query filter gem
|