simple-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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/simple_filter.rb +8 -0
- data/lib/simple_filter/base.rb +32 -0
- data/lib/simple_filter/filter.rb +30 -0
- data/lib/simple_filter/module_helper.rb +29 -0
- data/lib/simple_filter/version.rb +3 -0
- data/simple_filter.gemspec +27 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cc1fcde0783a3183de1f0564fe719aec2a5bee19
|
4
|
+
data.tar.gz: 4faaa88d0c96290aa96c4121dcd7e5cbeb636135
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b20f50f704b2a0217ca555ab122808935f57d30616c15de3ff84e1941367fd0b47d906be62359bb76c38d0f9c61c64afc6539439784c6237f9981aa3d665926d
|
7
|
+
data.tar.gz: 8130ba9f3c88b5d5a5876917550013d197fe0b69119bfbbaf13f7485cca75c7f898bfebb0322cf1f3a9c9cac8f714ac12e94361198cac6ae522f8e01c8a6acff
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Matias Leidemer
|
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,141 @@
|
|
1
|
+
[](https://codeclimate.com/github/matiasleidemer/simple_filter) [](https://travis-ci.org/matiasleidemer/simple_filter)
|
2
|
+
|
3
|
+
# SimpleFilter
|
4
|
+
|
5
|
+
SimpleFilter is a very simple ruby _DSL_ to write filter (or search) classes for ActiveRecord scopes. It's only responsability is to map parameters to defined scopes.
|
6
|
+
|
7
|
+
It currently works with ActiveRecord 4.0 or greater.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
To use search classes is pretty straightforward:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
FooSearch.new(params = {}).scoping(Model.all).search
|
15
|
+
```
|
16
|
+
|
17
|
+
`params` should be a hash containing all the desired filters, and `scope` is the current model scope that the filters will be applied into, for example:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class CampaignsController < ApplicationController
|
21
|
+
def index
|
22
|
+
@campaigns = CampaignSearcher.new(search_params).scoping(Campaign.all).search
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def search_params
|
28
|
+
params.slice(:name, :category, :whatever)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
The `search` method returns an `ActiveRecord::Relation`, making it easy to chain other scopes or conditions when needed.
|
34
|
+
|
35
|
+
### Filter
|
36
|
+
|
37
|
+
Filter is the class method that creates a new filter (really?!). Here's a simple example:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class FooSearch < SimpleFilter::Base
|
41
|
+
filter :active
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
# Usage
|
46
|
+
FooSearch.new(active: true).scoping(Foo.all).search
|
47
|
+
|
48
|
+
# Which is the same as
|
49
|
+
Foo.all.active
|
50
|
+
```
|
51
|
+
|
52
|
+
You can apply whatever scope needed:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
FooSearch.new(active: true).scoping(current_user.posts).search
|
56
|
+
# => current_user.posts.active
|
57
|
+
```
|
58
|
+
|
59
|
+
You can also create a filter that calls the scope method with the parameter value using the option `value_param: true`
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class FooSearch < SimpleFilter::Base
|
63
|
+
filter :active
|
64
|
+
filter :by_name, value_param: true
|
65
|
+
end
|
66
|
+
|
67
|
+
FooSearch.new(by_name: 'Matias').scoping(Foo.all).search
|
68
|
+
# => Foo.all.by_name('Matias')
|
69
|
+
|
70
|
+
FooSearch.new(by_name: 'Matias', active: true).scoping(Foo.all).search
|
71
|
+
# => Foo.all.active.by_name('Matias')
|
72
|
+
```
|
73
|
+
|
74
|
+
Of course you have to define those scopes in your ActiveRecord model:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class Foo < ActiceRecord::Base
|
78
|
+
scope :active, -> { where active: true }
|
79
|
+
scope :by_name, -> (name) { where 'name like ?', "%#{name}%" }
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
### Custom filters
|
84
|
+
|
85
|
+
Lastly, you can create fully customized filters. Let's say you want to apply a specific filter only when some condition is true
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class FooSearch < SimpleFilter::Base
|
89
|
+
filter :active
|
90
|
+
filter :name, value_param: true
|
91
|
+
filter :within_period
|
92
|
+
|
93
|
+
def within_period
|
94
|
+
return unless date_range?
|
95
|
+
|
96
|
+
scope.where('start_at >= ? and end_at <= ?', params[:start_at], params[:end_at])
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def date_range?
|
102
|
+
params[:start_at].present? && params[:end_at].present?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
FooSearch.new(start_at: '2015-05-08', end_at: '2015-05-31').scoping(Foo.all).search
|
107
|
+
```
|
108
|
+
|
109
|
+
Note that in the example above I'm using `params` and `scope` attributes. You can use it the create custom conditions and validation for your filters. It's important that your custom filters always return a `ActiveRecord::Relation` object, since it will chain it with the other filters.
|
110
|
+
|
111
|
+
It's also possible to call `super` when you define your custom filters, this happens because of the way filters are defined in the `SimpleFilter::Base` class.
|
112
|
+
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class FooSearch < SimpleFilter::Base
|
116
|
+
filter :active
|
117
|
+
|
118
|
+
def active
|
119
|
+
super.where 'some other condition'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
## Todo
|
125
|
+
|
126
|
+
- Ordering
|
127
|
+
- Pagination
|
128
|
+
|
129
|
+
## Development
|
130
|
+
|
131
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
132
|
+
|
133
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
134
|
+
|
135
|
+
## Contributing
|
136
|
+
|
137
|
+
1. Fork it ( https://github.com/matiasleidemer/simple_filter/fork )
|
138
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
139
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
140
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
141
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "simple_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
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module SimpleFilter
|
2
|
+
class Base
|
3
|
+
extend Filter
|
4
|
+
|
5
|
+
attr_reader :params, :scope
|
6
|
+
|
7
|
+
def initialize(params = {}, scope = nil)
|
8
|
+
@params = params
|
9
|
+
@scope = scope
|
10
|
+
end
|
11
|
+
|
12
|
+
def scoping(scope)
|
13
|
+
@scope = scope
|
14
|
+
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def search
|
19
|
+
conditions.scope
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def conditions
|
25
|
+
self.class.filters.each do |filter|
|
26
|
+
@scope.merge! send(filter) || @scope.where(nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
self
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SimpleFilter
|
2
|
+
module Filter
|
3
|
+
attr_accessor :filters
|
4
|
+
|
5
|
+
def filter(name, options = {})
|
6
|
+
method_module = ModuleHelper.module_for 'Filter', name, self
|
7
|
+
value_param = options.fetch :value_param, false
|
8
|
+
|
9
|
+
method_module.module_eval <<-CODE, __FILE__, __LINE__ + 1
|
10
|
+
def #{name}
|
11
|
+
return if !params[:#{name}] || params[:#{name}].to_s.blank?
|
12
|
+
|
13
|
+
args = [:#{name}]
|
14
|
+
args << params[:#{name}] if #{value_param}
|
15
|
+
|
16
|
+
scope.send *args
|
17
|
+
end
|
18
|
+
CODE
|
19
|
+
|
20
|
+
add_filter name
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def add_filter(name)
|
26
|
+
@filters ||= []
|
27
|
+
@filters << name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module SimpleFilter
|
2
|
+
module ModuleHelper
|
3
|
+
module_function def module_for(prefix, name, klass)
|
4
|
+
mod_name = ModuleName.new(prefix, name)
|
5
|
+
|
6
|
+
begin
|
7
|
+
mod = klass.send(:const_get, mod_name)
|
8
|
+
rescue NameError
|
9
|
+
mod = Module.new
|
10
|
+
klass.send(:const_set, mod_name, mod)
|
11
|
+
klass.send(:include, mod)
|
12
|
+
end
|
13
|
+
|
14
|
+
mod
|
15
|
+
end
|
16
|
+
|
17
|
+
class ModuleName
|
18
|
+
def initialize(prefix, name)
|
19
|
+
@prefix = prefix
|
20
|
+
@name = name
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
@prefix + @name.to_s.split('_').map(&:capitalize).join
|
25
|
+
end
|
26
|
+
alias to_str to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_filter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple-filter"
|
8
|
+
spec.version = SimpleFilter::VERSION
|
9
|
+
spec.authors = ["Matias Leidemer"]
|
10
|
+
spec.email = ["matiasleidemer@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple DSL to create filter classes for ActiveRecord scopes}
|
13
|
+
spec.description = %q{A simple DSL to create filter classes for ActiveRecord scopes}
|
14
|
+
spec.homepage = "https://github.com/matiasleidemer/simple_filter"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activerecord", "~> 4.0"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matias Leidemer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A simple DSL to create filter classes for ActiveRecord scopes
|
84
|
+
email:
|
85
|
+
- matiasleidemer@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/simple_filter.rb
|
100
|
+
- lib/simple_filter/base.rb
|
101
|
+
- lib/simple_filter/filter.rb
|
102
|
+
- lib/simple_filter/module_helper.rb
|
103
|
+
- lib/simple_filter/version.rb
|
104
|
+
- simple_filter.gemspec
|
105
|
+
homepage: https://github.com/matiasleidemer/simple_filter
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.4.5
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: A simple DSL to create filter classes for ActiveRecord scopes
|
129
|
+
test_files: []
|