query_filter 0.1.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 +7 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.rubocop.yml +46 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +87 -0
- data/Guardfile +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +173 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/query_filter/base.rb +62 -0
- data/lib/query_filter/rules/date_range.rb +42 -0
- data/lib/query_filter/rules/order_by.rb +23 -0
- data/lib/query_filter/rules/range.rb +38 -0
- data/lib/query_filter/rules/scope.rb +46 -0
- data/lib/query_filter/rules/splitter_range.rb +44 -0
- data/lib/query_filter/utils/date_period.rb +47 -0
- data/lib/query_filter/utils/scope_range.rb +43 -0
- data/lib/query_filter/version.rb +3 -0
- data/lib/query_filter.rb +20 -0
- data/query_filter.gemspec +33 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b13ac6d3558af97041e796a73c5c9e3b63f7bedc
|
4
|
+
data.tar.gz: ba74f0d4bb7a58049719c7da163cdb7d936c325a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76080943fcb0583e0d248df8897de1d7e03aad2c8867d84d2ce4e66f9c496000ac2f959c7a80ded270573b62afdbb962bdae2fb06f20faae427510a23680e764
|
7
|
+
data.tar.gz: 4f11da6d3c541a9715a070be3d0d2b2a21dae8c864f10d3a4794e39e31d1fa90daab4f612422e60bf6c93bb7323fd3457b9aa93a20610b6d8f65a718c94a7986
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
##################### Styles ##################################
|
5
|
+
|
6
|
+
Style/Documentation:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/SymbolArray:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/ClassAndModuleChildren:
|
13
|
+
Exclude:
|
14
|
+
- "app/controllers/*_controller.rb"
|
15
|
+
- "app/controllers/**/*_controller.rb"
|
16
|
+
|
17
|
+
#################### Lint ##################################
|
18
|
+
|
19
|
+
Lint/AmbiguousBlockAssociation:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
##################### Metrics ##################################
|
23
|
+
|
24
|
+
Metrics/LineLength:
|
25
|
+
Max: 110
|
26
|
+
|
27
|
+
Metrics/ClassLength:
|
28
|
+
Max: 200
|
29
|
+
|
30
|
+
Metrics/ModuleLength:
|
31
|
+
Max: 200
|
32
|
+
Exclude:
|
33
|
+
- "**/*_spec.rb"
|
34
|
+
|
35
|
+
Metrics/BlockLength:
|
36
|
+
Max: 50
|
37
|
+
Exclude:
|
38
|
+
- "**/*_spec.rb"
|
39
|
+
|
40
|
+
##################### Rails ##################################
|
41
|
+
|
42
|
+
Rails:
|
43
|
+
Enabled: true
|
44
|
+
|
45
|
+
Rails/SkipsModelValidations:
|
46
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
query_filter (0.1.0)
|
5
|
+
activesupport (>= 4.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.1.2)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
coderay (1.1.1)
|
16
|
+
concurrent-ruby (1.0.5)
|
17
|
+
diff-lcs (1.3)
|
18
|
+
ffi (1.9.18)
|
19
|
+
formatador (0.2.5)
|
20
|
+
guard (2.14.1)
|
21
|
+
formatador (>= 0.2.4)
|
22
|
+
listen (>= 2.7, < 4.0)
|
23
|
+
lumberjack (~> 1.0)
|
24
|
+
nenv (~> 0.1)
|
25
|
+
notiffany (~> 0.0)
|
26
|
+
pry (>= 0.9.12)
|
27
|
+
shellany (~> 0.0)
|
28
|
+
thor (>= 0.18.1)
|
29
|
+
guard-compat (1.2.1)
|
30
|
+
guard-rspec (4.7.3)
|
31
|
+
guard (~> 2.1)
|
32
|
+
guard-compat (~> 1.1)
|
33
|
+
rspec (>= 2.99.0, < 4.0)
|
34
|
+
i18n (0.8.4)
|
35
|
+
listen (3.1.5)
|
36
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
37
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
38
|
+
ruby_dep (~> 1.2)
|
39
|
+
lumberjack (1.0.12)
|
40
|
+
method_source (0.8.2)
|
41
|
+
minitest (5.10.2)
|
42
|
+
nenv (0.3.0)
|
43
|
+
notiffany (0.1.1)
|
44
|
+
nenv (~> 0.1)
|
45
|
+
shellany (~> 0.0)
|
46
|
+
pry (0.10.4)
|
47
|
+
coderay (~> 1.1.0)
|
48
|
+
method_source (~> 0.8.1)
|
49
|
+
slop (~> 3.4)
|
50
|
+
rake (10.5.0)
|
51
|
+
rb-fsevent (0.9.8)
|
52
|
+
rb-inotify (0.9.10)
|
53
|
+
ffi (>= 0.5.0, < 2)
|
54
|
+
rspec (3.6.0)
|
55
|
+
rspec-core (~> 3.6.0)
|
56
|
+
rspec-expectations (~> 3.6.0)
|
57
|
+
rspec-mocks (~> 3.6.0)
|
58
|
+
rspec-core (3.6.0)
|
59
|
+
rspec-support (~> 3.6.0)
|
60
|
+
rspec-expectations (3.6.0)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.6.0)
|
63
|
+
rspec-mocks (3.6.0)
|
64
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
65
|
+
rspec-support (~> 3.6.0)
|
66
|
+
rspec-support (3.6.0)
|
67
|
+
ruby_dep (1.5.0)
|
68
|
+
shellany (0.0.1)
|
69
|
+
slop (3.6.0)
|
70
|
+
thor (0.19.4)
|
71
|
+
thread_safe (0.3.6)
|
72
|
+
tzinfo (1.2.3)
|
73
|
+
thread_safe (~> 0.1)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
bundler (~> 1.15)
|
80
|
+
guard-rspec (~> 4.7, >= 4.7.3)
|
81
|
+
query_filter!
|
82
|
+
rake (~> 10.0)
|
83
|
+
rb-fsevent (= 0.9.8)
|
84
|
+
rspec (~> 3.0)
|
85
|
+
|
86
|
+
BUNDLED WITH
|
87
|
+
1.15.1
|
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
# QueryFilter
|
2
|
+
|
3
|
+
This gem provides DSL to write custom complex filters for ActiveRecord models. It automatically parses whitelisted params and adds filters to query.
|
4
|
+
|
5
|
+
Instead of writing this:
|
6
|
+
```ruby
|
7
|
+
class Order < ActiveRecord::Base
|
8
|
+
def self.filter(params)
|
9
|
+
query = all
|
10
|
+
query = query.with_state(params[:state]) if params[:state].present?
|
11
|
+
query
|
12
|
+
end
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
you can write this:
|
17
|
+
```ruby
|
18
|
+
class Order < ActiveRecord::Base
|
19
|
+
def self.filter(params)
|
20
|
+
OrderFilter.new(all, params).to_query
|
21
|
+
end
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'query_filter'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install query_filter
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
This gem support next types of filter params:
|
44
|
+
* scope - simple filter by column
|
45
|
+
```ruby
|
46
|
+
scope :state
|
47
|
+
|
48
|
+
def scope_state(value)
|
49
|
+
# your code
|
50
|
+
end
|
51
|
+
```
|
52
|
+
Keyword `scope` defines type of filter, and argument `:state` is a key from params to use for filter. Can be restricted using array of allowed values
|
53
|
+
|
54
|
+
* range - between, greater than or equal, less than or equal
|
55
|
+
For range inputs, can be used with 2 arguments or with 1, for example if both params supplied as
|
56
|
+
```ruby
|
57
|
+
{ price_from: 100, price_to: 200 }
|
58
|
+
```
|
59
|
+
this will produce a query like
|
60
|
+
```sql
|
61
|
+
orders.price BETWEEN 100 AND 200
|
62
|
+
```
|
63
|
+
With one parameter `price_from` a query would look like:
|
64
|
+
```sql
|
65
|
+
orders.price >= 100
|
66
|
+
```
|
67
|
+
or `price_to`:
|
68
|
+
```sql
|
69
|
+
orders.price <= 200
|
70
|
+
```
|
71
|
+
|
72
|
+
* splitter range - the same as range, but with one param
|
73
|
+
Some JS slider libraries join ranges with splitter, this filter could be used for that.
|
74
|
+
Sample params:
|
75
|
+
```ruby
|
76
|
+
{ price: '100;200' }
|
77
|
+
```
|
78
|
+
query:
|
79
|
+
```sql
|
80
|
+
orders.price BETWEEN 100 AND 200
|
81
|
+
```
|
82
|
+
|
83
|
+
* date range - the same as range, but for dates
|
84
|
+
Sample params:
|
85
|
+
```ruby
|
86
|
+
{ created_at: '06/24/2017 to 06/30/2017' }
|
87
|
+
```
|
88
|
+
query:
|
89
|
+
```sql
|
90
|
+
orders.created_at BETWEEN '2017-06-24 04:00:00.000000' AND '2017-07-01 03:59:59.999999'
|
91
|
+
```
|
92
|
+
|
93
|
+
* order by
|
94
|
+
```ruby
|
95
|
+
order_by :sort_column, via: :sort_mode
|
96
|
+
|
97
|
+
def order_by_sort_column(column, direction)
|
98
|
+
# your code
|
99
|
+
end
|
100
|
+
```
|
101
|
+
Sample params:
|
102
|
+
```ruby
|
103
|
+
{ sort_column: 'created_at', sort_mode: 'desc' }
|
104
|
+
```
|
105
|
+
query:
|
106
|
+
```sql
|
107
|
+
ORDER BY "orders"."created_at" DESC
|
108
|
+
```
|
109
|
+
|
110
|
+
### Sample class with usage examples
|
111
|
+
|
112
|
+
To use scope filter with Order model define your filter class as `app/filters/order_filter.rb`
|
113
|
+
```ruby
|
114
|
+
class OrderFilter < QueryFilter::Base
|
115
|
+
# can be restricted using array of values
|
116
|
+
scope :state, only: [:pending, :completed]
|
117
|
+
scope :deleted, only: TRUE_ARRAY
|
118
|
+
|
119
|
+
range :price
|
120
|
+
|
121
|
+
splitter_range :price
|
122
|
+
|
123
|
+
date_range :created_at
|
124
|
+
|
125
|
+
order_by :sort_column, via: :sort_mode
|
126
|
+
|
127
|
+
# Filter will be applied when following params present
|
128
|
+
# { state: :pending }
|
129
|
+
def scope_state(value)
|
130
|
+
query.with_state(value)
|
131
|
+
end
|
132
|
+
|
133
|
+
def scope_deleted(_value)
|
134
|
+
query.where(deleted: true)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Filter will be applied when following params present
|
138
|
+
# { price_from: 100, price_to: 200 }
|
139
|
+
def range_price(range)
|
140
|
+
# You should pass SQL column name to `query` method
|
141
|
+
# this will result with following query:
|
142
|
+
# 'orders.price BETWEEN 100 AND 200'
|
143
|
+
query.where(range.query('orders.price'))
|
144
|
+
end
|
145
|
+
|
146
|
+
def splitter_range_price(values)
|
147
|
+
# 'orders.price BETWEEN 100 AND 200'
|
148
|
+
query.where(price: value.range)
|
149
|
+
end
|
150
|
+
|
151
|
+
def date_range_created_at(period)
|
152
|
+
query.where(created_at: period.range)
|
153
|
+
end
|
154
|
+
|
155
|
+
def order_by_sort_column(column, direction)
|
156
|
+
query.reorder("orders.#{column} #{direction} NULLS LAST")
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
## Development
|
162
|
+
|
163
|
+
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.
|
164
|
+
|
165
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
166
|
+
|
167
|
+
## Contributing
|
168
|
+
|
169
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/query_filter.
|
170
|
+
|
171
|
+
## License
|
172
|
+
|
173
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "query_filter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Build select query based on params hash
|
2
|
+
#
|
3
|
+
module QueryFilter
|
4
|
+
class Base
|
5
|
+
TRUE_ARRAY = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
|
6
|
+
|
7
|
+
class_attribute :filters
|
8
|
+
|
9
|
+
attr_reader :query
|
10
|
+
|
11
|
+
def initialize(scope, params)
|
12
|
+
@query = scope
|
13
|
+
@params = (params || {})
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_query
|
17
|
+
active_filters do |filter, values|
|
18
|
+
condition = send(filter.endpoint, *values)
|
19
|
+
next if condition.nil?
|
20
|
+
|
21
|
+
@query = condition
|
22
|
+
end
|
23
|
+
|
24
|
+
@query
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.scope(*args)
|
28
|
+
filter(Rules::Scope, *args)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.range(*args)
|
32
|
+
filter(RangeFilter, *args)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.date_range(*args)
|
36
|
+
filter(DateRangeFilter, *args)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.splitter_range(*args)
|
40
|
+
filter(Rules::SplitterRange, *args)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.order_by(*args)
|
44
|
+
filter(OrderByFilter, *args)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.filter(class_name, *args)
|
48
|
+
options = args.extract_options!
|
49
|
+
|
50
|
+
self.filters ||= []
|
51
|
+
self.filters << class_name.new(args, options)
|
52
|
+
end
|
53
|
+
|
54
|
+
protected
|
55
|
+
|
56
|
+
def active_filters
|
57
|
+
self.class.filters.each do |filter|
|
58
|
+
yield filter, filter.normalize_params(@params) if filter.valid?(@params)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Parse date range params
|
2
|
+
#
|
3
|
+
# date_range :created_at, keys: [:start_date, :end_date]
|
4
|
+
#
|
5
|
+
# date_range :last_login_date
|
6
|
+
#
|
7
|
+
module QueryFilter::Rules
|
8
|
+
class DateRange < Scope
|
9
|
+
def name
|
10
|
+
'date_range'.freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?(values)
|
14
|
+
period = build_period_from_params(values)
|
15
|
+
!(period.nil? || period.default?)
|
16
|
+
end
|
17
|
+
|
18
|
+
def normalize_params(values)
|
19
|
+
build_period_from_params(values)
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def build_period_from_params(params)
|
25
|
+
if params[key].present?
|
26
|
+
QueryFilter::Utils::DatePeriod.parse_from_string(params[key], @options[:format])
|
27
|
+
elsif keys_start_end_dates_exists?(params)
|
28
|
+
QueryFilter::Utils::DatePeriod.new(*values_start_end_dates(params))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def keys_start_end_dates_exists?(params)
|
33
|
+
values = values_start_end_dates(params)
|
34
|
+
!values.nil? && values.size == 2
|
35
|
+
end
|
36
|
+
|
37
|
+
def values_start_end_dates(params)
|
38
|
+
return if @options[:keys].nil?
|
39
|
+
params.values_at(*@options[:keys])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Define order by filter rule
|
2
|
+
#
|
3
|
+
module QueryFilter::Rules
|
4
|
+
class OrderBy < Scope
|
5
|
+
DIRECTIONS = %w[asc desc].freeze
|
6
|
+
|
7
|
+
def name
|
8
|
+
'order_by'.freeze
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?(params)
|
12
|
+
params[key].present? && DIRECTIONS.include?(params[direction_key])
|
13
|
+
end
|
14
|
+
|
15
|
+
def direction_key
|
16
|
+
@direction_key ||= (@options[:via] || 'sort_direction').to_sym
|
17
|
+
end
|
18
|
+
|
19
|
+
def normalize_params(values)
|
20
|
+
[values[key], values[direction_key]]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Define range filter rule
|
2
|
+
#
|
3
|
+
module QueryFilter::Rules
|
4
|
+
class Range < Scope
|
5
|
+
def initialize(keys, options = {})
|
6
|
+
@key = Array(keys).first
|
7
|
+
@keys = [key_from, key_to]
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def name
|
12
|
+
'range'.freeze
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?(values)
|
16
|
+
filter = build_range_from_params(values)
|
17
|
+
filter.valid?
|
18
|
+
end
|
19
|
+
|
20
|
+
def normalize_params(values)
|
21
|
+
build_range_from_params(values)
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def build_range_from_params(params)
|
27
|
+
QueryFilter::Utils::ScopeRange.new(key, params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def key_from
|
31
|
+
@key_from ||= "#{key}_from".to_sym
|
32
|
+
end
|
33
|
+
|
34
|
+
def key_to
|
35
|
+
@key_to ||= "#{key}_to".to_sym
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Define filter rules
|
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)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module QueryFilter::Rules
|
2
|
+
class SplitterRange < Scope
|
3
|
+
class RangeParam
|
4
|
+
SPLITTER = /[;-]/
|
5
|
+
|
6
|
+
def initialize(value)
|
7
|
+
@value = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def valid?
|
11
|
+
@value.present? && items.size == 2
|
12
|
+
end
|
13
|
+
|
14
|
+
def range
|
15
|
+
Range.new(*items)
|
16
|
+
end
|
17
|
+
|
18
|
+
def items
|
19
|
+
@items ||= @value.split(SPLITTER).map(&:strip).map(&:to_i)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def name
|
24
|
+
'spliter_range'.freeze
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid?(values)
|
28
|
+
period = build_period_from_params(values)
|
29
|
+
!period.nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
def normalize_params(values)
|
33
|
+
build_period_from_params(values)
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def build_period_from_params(values)
|
39
|
+
param = RangeParam.new(values[key])
|
40
|
+
return unless param.valid?
|
41
|
+
param
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module QueryFilter::Utils
|
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
|
14
|
+
|
15
|
+
def range
|
16
|
+
@range ||= date_from..date_to
|
17
|
+
end
|
18
|
+
|
19
|
+
def default?
|
20
|
+
@default
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.parse_from_string(value, format = DATE_FORMAT)
|
24
|
+
return value if value.is_a?(DatePeriod)
|
25
|
+
|
26
|
+
if value.blank?
|
27
|
+
new(nil, nil, format)
|
28
|
+
else
|
29
|
+
dates = value.to_s.split(SPLITTER).map(&:strip)
|
30
|
+
new(dates.first, dates.last, format)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def normalize_date(date)
|
37
|
+
return date if date.is_a?(Time) || date.is_a?(DateTime)
|
38
|
+
|
39
|
+
begin
|
40
|
+
time = DateTime.strptime(date, @format)
|
41
|
+
Time.zone.parse(time.strftime('%Y-%m-%d %H:%M:%S'))
|
42
|
+
rescue StandardError => _e
|
43
|
+
Time.zone.today
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module QueryFilter::Utils
|
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
|
12
|
+
|
13
|
+
def name_from
|
14
|
+
@name_from ||= "#{@name}_from".to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
def name_to
|
18
|
+
@name_to ||= "#{@name}_to".to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
def value_from
|
22
|
+
@options[name_from]
|
23
|
+
end
|
24
|
+
|
25
|
+
def value_to
|
26
|
+
@options[name_to]
|
27
|
+
end
|
28
|
+
|
29
|
+
def valid?
|
30
|
+
@options && (value_from.present? || value_to.present?)
|
31
|
+
end
|
32
|
+
|
33
|
+
def query(column)
|
34
|
+
if value_from && value_to
|
35
|
+
["#{column} BETWEEN ? AND ?", value_from, value_to]
|
36
|
+
elsif value_from
|
37
|
+
["#{column} >= ?", value_from]
|
38
|
+
elsif value_to
|
39
|
+
["#{column} <= ?", value_to]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/query_filter.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'query_filter/version'
|
4
|
+
|
5
|
+
module QueryFilter
|
6
|
+
autoload :Base, 'query_filter/base'
|
7
|
+
|
8
|
+
module Rules
|
9
|
+
autoload :Scope, 'query_filter/rules/scope'
|
10
|
+
autoload :Range, 'query_filter/rules/range'
|
11
|
+
autoload :DateRange, 'query_filter/rules/date_range'
|
12
|
+
autoload :SplitterRange, 'query_filter/rules/splitter_range'
|
13
|
+
autoload :OrderBy, 'query_filter/rules/order_by'
|
14
|
+
end
|
15
|
+
|
16
|
+
module Utils
|
17
|
+
autoload :DatePeriod, 'query_filter/utils/date_period'
|
18
|
+
autoload :ScopeRange, 'query_filter/utils/scope_range'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'query_filter/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'query_filter'
|
10
|
+
spec.version = QueryFilter::VERSION
|
11
|
+
spec.authors = ['Igor Galeta', 'Igor Malinovskiy']
|
12
|
+
spec.email = ['igor.malinovskiy@netfix.xyz']
|
13
|
+
|
14
|
+
spec.summary = 'ActiveRecord query filter gem'
|
15
|
+
spec.description = 'This gem provides DSL to write custom complex filters for ActiveRecord models'
|
16
|
+
spec.homepage = 'http://rubygems.org/gems/query_filter'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.7', '>= 4.7.3'
|
30
|
+
spec.add_development_dependency 'rb-fsevent', '0.9.8'
|
31
|
+
|
32
|
+
spec.add_dependency 'activesupport', '>= 4.0'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: query_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Galeta
|
8
|
+
- Igor Malinovskiy
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-06-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.15'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.15'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: guard-rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '4.7'
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 4.7.3
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - "~>"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '4.7'
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.7.3
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: rb-fsevent
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.9.8
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.9.8
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: activesupport
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.0'
|
97
|
+
type: :runtime
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.0'
|
104
|
+
description: This gem provides DSL to write custom complex filters for ActiveRecord
|
105
|
+
models
|
106
|
+
email:
|
107
|
+
- igor.malinovskiy@netfix.xyz
|
108
|
+
executables: []
|
109
|
+
extensions: []
|
110
|
+
extra_rdoc_files: []
|
111
|
+
files:
|
112
|
+
- ".gitignore"
|
113
|
+
- ".rspec"
|
114
|
+
- ".rubocop.yml"
|
115
|
+
- ".travis.yml"
|
116
|
+
- Gemfile
|
117
|
+
- Gemfile.lock
|
118
|
+
- Guardfile
|
119
|
+
- LICENSE.txt
|
120
|
+
- README.md
|
121
|
+
- Rakefile
|
122
|
+
- bin/console
|
123
|
+
- bin/setup
|
124
|
+
- lib/query_filter.rb
|
125
|
+
- lib/query_filter/base.rb
|
126
|
+
- lib/query_filter/rules/date_range.rb
|
127
|
+
- lib/query_filter/rules/order_by.rb
|
128
|
+
- lib/query_filter/rules/range.rb
|
129
|
+
- lib/query_filter/rules/scope.rb
|
130
|
+
- lib/query_filter/rules/splitter_range.rb
|
131
|
+
- lib/query_filter/utils/date_period.rb
|
132
|
+
- lib/query_filter/utils/scope_range.rb
|
133
|
+
- lib/query_filter/version.rb
|
134
|
+
- query_filter.gemspec
|
135
|
+
homepage: http://rubygems.org/gems/query_filter
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.5.1
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: ActiveRecord query filter gem
|
159
|
+
test_files: []
|