query_filter 0.1.4 → 0.1.5
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 +15 -0
- data/lib/query_filter.rb +19 -0
- data/lib/query_filter/utils/date_period.rb +68 -35
- data/lib/query_filter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3be0d7ad318960b8718eb59befce928685961643
|
4
|
+
data.tar.gz: a78a2e95239421f221293bd64539c2b7112e1dbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0a67510e6629f6f188c388ecdcdb70adf8527f2f43a14a23d4c364e6805dfc9a9d6f9a7092e98008c1c1df6b188aedd2e0a6a3fd006fd90f69d0387ce0e032d
|
7
|
+
data.tar.gz: 929fdde8fb5003292a4eb85d3af88d3eb361ba9628abcdfc89e27d75cb8658a39bf96639cdce79727b53a89718d7a5b2c8cae3c7316c2986c73419302ebf786c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -173,6 +173,21 @@ class OrderFilter < QueryFilter::Base
|
|
173
173
|
end
|
174
174
|
```
|
175
175
|
|
176
|
+
### Configuration
|
177
|
+
|
178
|
+
You can set up some options in general
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
# config/initializers/query_filter.rb
|
182
|
+
QueryFilter.setup do |config|
|
183
|
+
# Date period format, by default: %m/%d/%Y
|
184
|
+
config.date_period_format = '%d-%m-%Y'
|
185
|
+
|
186
|
+
# Splitter to parse date period values, by default 'to'
|
187
|
+
config.date_period_splitter = 'until'
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
176
191
|
## Development
|
177
192
|
|
178
193
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/query_filter.rb
CHANGED
@@ -21,4 +21,23 @@ module QueryFilter
|
|
21
21
|
autoload :ScopeRange, 'query_filter/utils/scope_range'
|
22
22
|
autoload :UserConditions, 'query_filter/utils/user_conditions'
|
23
23
|
end
|
24
|
+
|
25
|
+
# Configurable date period format
|
26
|
+
mattr_accessor :date_period_format
|
27
|
+
self.date_period_format = '%m/%d/%Y'
|
28
|
+
|
29
|
+
# Splitter to parse date period values
|
30
|
+
mattr_accessor :date_period_splitter
|
31
|
+
self.date_period_splitter = 'to'
|
32
|
+
|
33
|
+
# Default way to setup QueryFilter
|
34
|
+
# @example
|
35
|
+
# QueryFilter.setup do |config|
|
36
|
+
# config.date_period_format = '%d-%m-%Y'
|
37
|
+
# config.date_period_splitter = 'until'
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
def self.setup
|
41
|
+
yield self
|
42
|
+
end
|
24
43
|
end
|
@@ -1,46 +1,79 @@
|
|
1
|
-
|
2
|
-
class DatePeriod
|
3
|
-
attr_reader :date_from, :date_to, :format
|
4
|
-
|
5
|
-
SPLITTER = 'to'.freeze
|
6
|
-
DATE_FORMAT = '%m/%d/%Y'.freeze
|
7
|
-
|
8
|
-
def initialize(date_from = nil, date_to = nil, format = nil)
|
9
|
-
@format = format || DATE_FORMAT
|
10
|
-
@default = (date_from.blank? && date_to.blank?)
|
11
|
-
@date_from = normalize_date(date_from || Time.zone.now).beginning_of_day
|
12
|
-
@date_to = normalize_date(date_to || Time.zone.now).end_of_day
|
13
|
-
end
|
1
|
+
# frozen_string_literal: true
|
14
2
|
|
15
|
-
|
16
|
-
|
17
|
-
|
3
|
+
module QueryFilter
|
4
|
+
module Utils
|
5
|
+
class DatePeriod
|
6
|
+
attr_reader :date_from, :date_to, :format
|
18
7
|
|
19
|
-
|
20
|
-
@default
|
21
|
-
end
|
8
|
+
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
|
22
9
|
|
23
|
-
|
24
|
-
|
10
|
+
def initialize(date_from = nil, date_to = nil, format = nil)
|
11
|
+
@format = (format.blank? ? QueryFilter.date_period_format : format)
|
12
|
+
@date_from_raw = date_from
|
13
|
+
@date_to_raw = date_to
|
25
14
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
15
|
+
@date_from = normalize_date(date_from).beginning_of_day
|
16
|
+
@date_to = normalize_date(date_to).end_of_day
|
17
|
+
end
|
18
|
+
|
19
|
+
def range
|
20
|
+
@range ||= date_from..date_to
|
21
|
+
end
|
22
|
+
|
23
|
+
def title
|
24
|
+
@title ||= to_human
|
25
|
+
end
|
26
|
+
|
27
|
+
def datefrom
|
28
|
+
@datefrom ||= I18n.l(@date_from, format: @format)
|
29
|
+
end
|
30
|
+
|
31
|
+
def dateto
|
32
|
+
@dateto ||= I18n.l(@date_to, format: @format)
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_param
|
36
|
+
[datefrom, dateto].join(QueryFilter.date_period_splitter)
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_human
|
40
|
+
[datefrom, dateto].join(" #{QueryFilter.date_period_splitter} ")
|
41
|
+
end
|
42
|
+
|
43
|
+
def default?
|
44
|
+
@date_from_raw.blank? && @date_to_raw.blank?
|
45
|
+
end
|
46
|
+
|
47
|
+
def as_json(options = nil)
|
48
|
+
{
|
49
|
+
date_from: datefrom,
|
50
|
+
date_to: dateto
|
51
|
+
}.merge(options || {})
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.parse_from_string(value, format = nil)
|
55
|
+
return value if value.is_a?(DatePeriod)
|
56
|
+
|
57
|
+
if value.blank?
|
58
|
+
new(nil, nil, format)
|
59
|
+
else
|
60
|
+
dates = value.to_s.split(QueryFilter.date_period_splitter).map(&:strip)
|
61
|
+
new(dates[0], dates[1], format)
|
62
|
+
end
|
31
63
|
end
|
32
|
-
end
|
33
64
|
|
34
|
-
|
65
|
+
private
|
35
66
|
|
36
|
-
|
37
|
-
|
67
|
+
def normalize_date(date)
|
68
|
+
return date if date.is_a?(Time) || date.is_a?(DateTime)
|
69
|
+
return Time.zone.today if date.blank?
|
38
70
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
71
|
+
begin
|
72
|
+
time = DateTime.strptime(date, @format)
|
73
|
+
Time.zone.parse(time.strftime(TIME_FORMAT))
|
74
|
+
rescue ArgumentError => _e
|
75
|
+
Time.zone.today
|
76
|
+
end
|
44
77
|
end
|
45
78
|
end
|
46
79
|
end
|
data/lib/query_filter/version.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.5
|
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-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|