simple_filter 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +101 -0
- data/Rakefile +8 -0
- data/lib/generators/simple_filter/USAGE +3 -0
- data/lib/generators/simple_filter/simple_filter_generator.rb +16 -0
- data/lib/generators/simple_filter/templates/filter.rb +17 -0
- data/lib/simple_filter.rb +9 -0
- data/lib/simple_filter/base.rb +88 -0
- data/lib/simple_filter/configuration.rb +14 -0
- data/lib/simple_filter/railtie.rb +6 -0
- data/lib/simple_filter/version.rb +3 -0
- data/simple_filter.gemspec +17 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 ryancheung
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# SimpleFilter
|
2
|
+
|
3
|
+
Simple filter for replacing the awkward search method or scopes of models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_filter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_filter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
**SimpleFilter** was designed to replace the awkward search method or scopes in your models when
|
22
|
+
doing filtering. It encapsulate all of the filter logic to filter class, which will make your
|
23
|
+
model more cleaner. And it defaults to provider sort and pagination filter, relieving you from
|
24
|
+
keep writing `sort` or `pagination` filter for each models.
|
25
|
+
|
26
|
+
To start using **SimpleFilter** you just need to generate a filter class for your model:
|
27
|
+
|
28
|
+
rails generate simple_filter your_model_name
|
29
|
+
|
30
|
+
Then in your `app/filters/` directory you'll find filter file like this:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class UserFilter < SimpleFilter::Base
|
34
|
+
# Rewrite default filter options in case you need.
|
35
|
+
# config per_page: 20, order_by: :name
|
36
|
+
|
37
|
+
# There're default filters including :sort, :pagination. You can use them along with
|
38
|
+
# your custom filters like this:
|
39
|
+
#
|
40
|
+
filter :sort
|
41
|
+
#
|
42
|
+
# private
|
43
|
+
#
|
44
|
+
# def by_name
|
45
|
+
# if params[:name].present?
|
46
|
+
# @scope = scope.where(:name => params[:name])
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Here you can customize your filters with the `filter` DSL. For example:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class UserFilter < SimpleFilter::Base
|
56
|
+
filter :by_name, :sort, :pagination
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def by_name
|
61
|
+
if params[:name].present?
|
62
|
+
@scope = scope.where(:name => params[:name])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
In case your filter class name was not consist with the ActiveModel name, you could
|
69
|
+
use `scope` DSL to config this manually:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class PersonFilter < SimpleFilter::Base
|
73
|
+
scope :user
|
74
|
+
|
75
|
+
filter :sort
|
76
|
+
|
77
|
+
# other filters
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
In your controller you can use it like this:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class Admin::UsersController < Admin::BaseController
|
85
|
+
def index
|
86
|
+
@users = UserFilter.all(params)
|
87
|
+
end
|
88
|
+
|
89
|
+
# other actions...
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
As you see it, it's very simple, enjoy it!
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
1. Fork it
|
98
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
99
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
100
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
101
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class SimpleFilterGenerator < Rails::Generators::Base
|
2
|
+
desc "Create a filter for model specified"
|
3
|
+
argument :model_name, :type => :string, :default => ""
|
4
|
+
|
5
|
+
def self.source_root
|
6
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_filter
|
10
|
+
if model_name.blank?
|
11
|
+
puts "No model name provided, aborted!"
|
12
|
+
else
|
13
|
+
template 'filter.rb', File.join('app/filters', "#{model_name}_filter.rb")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= model_name.camelize %>Filter < SimpleFilter::Base
|
2
|
+
# Rewrite default filter options in case you need.
|
3
|
+
# config per_page: 20, order_by: :name
|
4
|
+
|
5
|
+
# There're default filters including :sort, :pagination. You can use them along with
|
6
|
+
# your custom filters like this:
|
7
|
+
#
|
8
|
+
# filter :sort, :by_name
|
9
|
+
#
|
10
|
+
# private
|
11
|
+
#
|
12
|
+
# def by_name
|
13
|
+
# if params[:name].present?
|
14
|
+
# @scope = scope.where(:name => params[:name])
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module SimpleFilter
|
2
|
+
class Base
|
3
|
+
class_attribute :filters, :configuration
|
4
|
+
|
5
|
+
# The params are the parameters you might have entered in your form.
|
6
|
+
attr_reader :params
|
7
|
+
|
8
|
+
self.filters = []
|
9
|
+
self.configuration = Configuration.new
|
10
|
+
|
11
|
+
# Call filter to define which filters are available.
|
12
|
+
# These will all be run in the order you specified
|
13
|
+
def self.filter(*filters)
|
14
|
+
self.filters = self.filters + filters
|
15
|
+
end
|
16
|
+
|
17
|
+
# possible options:
|
18
|
+
# per_page 10
|
19
|
+
# order_by :id
|
20
|
+
def self.config(options = {})
|
21
|
+
configuration.per_page = options[:per_page] unless options[:per_page].blank?
|
22
|
+
configuration.order_by = options[:order_by] unless options[:order_by].blank?
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.all(*args)
|
26
|
+
new(*args).all
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(params)
|
30
|
+
@params = params
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get all filtered results. This is the public facing method that
|
34
|
+
# you'd want to call when getting the results of the filter.
|
35
|
+
def all
|
36
|
+
apply_filters
|
37
|
+
scope
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# As part of the contract, set the default scope by overriding this method.
|
43
|
+
def scope
|
44
|
+
raise NotImplementedError
|
45
|
+
end
|
46
|
+
|
47
|
+
# Run all the filters, specified in subclasses.
|
48
|
+
def apply_filters
|
49
|
+
filters.each { |filter| send(filter) }
|
50
|
+
end
|
51
|
+
|
52
|
+
# Probably every filter should be able to paginate the results.
|
53
|
+
# remember to call pagination last, because will_paginate won't return
|
54
|
+
# a real ActiveRecord::Relation object.
|
55
|
+
def pagination
|
56
|
+
@scope = scope.paginate(:page => page, :per_page => per_page)
|
57
|
+
end
|
58
|
+
|
59
|
+
def page
|
60
|
+
params[:page] || 1
|
61
|
+
end
|
62
|
+
|
63
|
+
def per_page
|
64
|
+
params[:per_page] || self.class.configuration.per_page
|
65
|
+
end
|
66
|
+
|
67
|
+
# Similar to pagination, sorting is something common to all filters,
|
68
|
+
# The default order is :id, because that will be available on every
|
69
|
+
# model. You can override it easily however.
|
70
|
+
def sort(default = self.class.configuration.order_by)
|
71
|
+
@scope = scope.order(params[:order] || default)
|
72
|
+
@scope = scope.reverse_order if params[:direction] == 'desc'
|
73
|
+
end
|
74
|
+
|
75
|
+
class << self
|
76
|
+
def scope(model_name)
|
77
|
+
define_method(:scope) do
|
78
|
+
@scope ||= model_name.to_s.camelize.constantize.scoped
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def inherited(klass)
|
83
|
+
klass.send(:scope, klass.to_s.sub('Filter', '').underscore)
|
84
|
+
klass.configuration = self.configuration.dup
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/simple_filter/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["ryancheung"]
|
6
|
+
gem.email = ["ryancheung.go@gmail.com"]
|
7
|
+
gem.description = %q{Filter for ActiveModel}
|
8
|
+
gem.summary = %q{Simple filter for replacing the awkward search method or scopes of models.}
|
9
|
+
gem.homepage = "https://github.com/ryancheung/simple_filter"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "simple_filter"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = SimpleFilter::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ryancheung
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Filter for ActiveModel
|
15
|
+
email:
|
16
|
+
- ryancheung.go@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/generators/simple_filter/USAGE
|
27
|
+
- lib/generators/simple_filter/simple_filter_generator.rb
|
28
|
+
- lib/generators/simple_filter/templates/filter.rb
|
29
|
+
- lib/simple_filter.rb
|
30
|
+
- lib/simple_filter/base.rb
|
31
|
+
- lib/simple_filter/configuration.rb
|
32
|
+
- lib/simple_filter/railtie.rb
|
33
|
+
- lib/simple_filter/version.rb
|
34
|
+
- simple_filter.gemspec
|
35
|
+
homepage: https://github.com/ryancheung/simple_filter
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.24
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Simple filter for replacing the awkward search method or scopes of models.
|
59
|
+
test_files: []
|