scope_filter 0.9.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/CHANGELOG.md +0 -0
- data/LICENSE +20 -0
- data/README.md +88 -0
- data/ROADMAP.md +0 -0
- data/lib/scope_filter/active_record.rb +41 -0
- data/lib/scope_filter/configuration.rb +54 -0
- data/lib/scope_filter.rb +9 -0
- metadata +108 -0
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Scope-Filter
|
2
|
+
|
3
|
+
scope_filter is a filter- and search-library for ActiveRecord models using scopes.
|
4
|
+
|
5
|
+
You can define the filterable and sortable scopes and fields and filter records with the `scope_filter` method.
|
6
|
+
|
7
|
+
This lib is fully compatible with [kaminari](https://github.com/amatsuda/kaminari) or [will_paginate](https://github.com/mislav/will_paginate) as it uses ActiveRecord scopes only.
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
add the following line to your Gemfile
|
12
|
+
|
13
|
+
`gem 'scope_filter'`
|
14
|
+
|
15
|
+
or install it via rubygems
|
16
|
+
|
17
|
+
`gem install scope_filter`
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Include `ScopeFilter::ActiveRecord` in your model and define the filterable and sortable fields with the `configure_scope_filter` method.
|
22
|
+
|
23
|
+
You can configure the filter with the following methods:
|
24
|
+
|
25
|
+
- `add_scope` and `add_scopes`: makes scopes of your model searchable (pass a boolean argument at the end to define if the scopes expect a parameter, defaults to false)
|
26
|
+
- `eq`: equal comparator
|
27
|
+
- `g`, `gt`: greater and greater than comparator
|
28
|
+
- `l`, `lt`: less and less than comparator
|
29
|
+
- `like`, `ilike`: like and ilike than comparator
|
30
|
+
- `null`, `not_null`: is null and is not null than comparator
|
31
|
+
- `sortable`: fields the resultset may be sorted by
|
32
|
+
|
33
|
+
The first argument defines the name of the filter parameter. You can define arbitrary filter fields, i.e.:
|
34
|
+
|
35
|
+
`config.like :firstname_ends_with, :field => 'firstname', :pattern => lambda { |v| "%#{v}" }`
|
36
|
+
|
37
|
+
### Example
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class Person < ActiveRecord::Base
|
41
|
+
include ScopeFilter::ActiveRecord
|
42
|
+
|
43
|
+
scope :dwarf, where('age > ? AND height < ?', 16, 150)
|
44
|
+
scope :bastard, where('lastname IN (?)', %w(Snow Sand Rivers))
|
45
|
+
scope :highborn, where('lastname IN (?)', %w(Lannister Stark Targaryen Martell))
|
46
|
+
scope :older_than, lambda { |age| where('age > ?', age) }
|
47
|
+
scope :dead, where('date_of_death IS NOT NULL')
|
48
|
+
scope :dragon, where('lastname = ?', 'Targaryen')
|
49
|
+
|
50
|
+
configure_scope_filter do |config|
|
51
|
+
config.add_scopes :dwarf, :bastard, :dead, :highborn
|
52
|
+
config.add_scope :older_than, true
|
53
|
+
config.like :firstname
|
54
|
+
config.eq :knight
|
55
|
+
config.like :firstname_ends_with, :field => 'firstname', :pattern => lambda { |v| "%#{v}" }
|
56
|
+
config.null :alive, :field => 'date_of_death'
|
57
|
+
config.sortable :lastname, :firstname
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
use the `scope_filter` method to filter records, i.e.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
Person.scope_filter(:highborn => true, :knight => true, :_sort => 'lastname_desc')
|
67
|
+
```
|
68
|
+
|
69
|
+
which will return all highborn knights ordered descending by lastname.
|
70
|
+
|
71
|
+
Or use the params hash:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Person.scope_filter(params[:filter])
|
75
|
+
```
|
76
|
+
|
77
|
+
## Author
|
78
|
+
|
79
|
+
[Lars Kuhnt](http://www.github.com/larskuhnt)
|
80
|
+
Copyright (c) 2011
|
81
|
+
|
82
|
+
## License
|
83
|
+
|
84
|
+
Published under the MIT License.
|
85
|
+
|
86
|
+
See [LICENSE](LICENSE) for details.
|
87
|
+
|
88
|
+
|
data/ROADMAP.md
ADDED
File without changes
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ScopeFilter
|
3
|
+
module ActiveRecord
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def configure_scope_filter(&block)
|
12
|
+
@@sf_config = Configuration.new
|
13
|
+
|
14
|
+
yield @@sf_config
|
15
|
+
|
16
|
+
def self.scope_filter(values = {})
|
17
|
+
init_scope = self.scoped
|
18
|
+
if sort = values.delete(:_sort)
|
19
|
+
sort_field, sort_dir = sort.to_s.split('_')
|
20
|
+
init_scope = self.order("#{sort_field} #{sort_dir.upcase}") if @@sf_config.sort_fields.include?(sort_field.to_sym)
|
21
|
+
end
|
22
|
+
values.inject(init_scope) do |rel, q|
|
23
|
+
field = q.first.to_sym
|
24
|
+
value = q.last
|
25
|
+
return rel if value.blank?
|
26
|
+
if cond = @@sf_config.fields[field]
|
27
|
+
rel = rel.where(cond[:condition], cond[:pattern] ? cond[:pattern].call(value) : value)
|
28
|
+
elsif cond = @@sf_config.scopes[field]
|
29
|
+
rel = cond[:arg] ? rel.send(field, value) : rel.send(field)
|
30
|
+
else
|
31
|
+
rel
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module ScopeFilter
|
4
|
+
|
5
|
+
class Configuration
|
6
|
+
|
7
|
+
attr_reader :fields, :scopes, :sort_fields
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@fields = {}
|
11
|
+
@scopes = {}
|
12
|
+
@sort_fields = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_field(field, cop, options = {})
|
16
|
+
pattern = options[:pattern]
|
17
|
+
@fields[field.to_sym] = { :condition => "#{options[:field] ? options[:field] : field} #{cop}#{pattern ? ' ?' : ''}", :pattern => pattern }
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_scope(name, arg = false)
|
21
|
+
@scopes[name.to_sym] = { :arg => arg }
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_scopes(*scopes)
|
25
|
+
arg = scopes.delete_at(scopes.size-1) if scopes.last.is_a?(TrueClass) || scopes.last.is_a?(FalseClass)
|
26
|
+
scopes.each { |s| add_scope(s, arg.nil? ? false : arg) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def method_missing(m, *args, &block)
|
30
|
+
field = args.first.to_sym
|
31
|
+
options = args.second || {}
|
32
|
+
idx = %w(eq g gt l lt neq like ilike).index(m.to_s)
|
33
|
+
if idx
|
34
|
+
comp = %w(= > >= < <= <> LIKE ILIKE)[idx]
|
35
|
+
add_field field, comp, add_default_pattern(comp, options)
|
36
|
+
end
|
37
|
+
add_field field, 'IS NULL', options if m == :null
|
38
|
+
add_field field, 'IS NOT NULL', options if m == :not_null
|
39
|
+
end
|
40
|
+
|
41
|
+
def sortable(*fields)
|
42
|
+
@sort_fields += fields.map(&:to_sym)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def add_default_pattern(comp, options)
|
48
|
+
return options if options[:pattern]
|
49
|
+
options.merge(:pattern => lambda { |v| comp =~ /LIKE/ ? "%#{v}%" : v })
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/lib/scope_filter.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scope_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lars Kuhnt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-11 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70198313009400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70198313009400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70198313009020 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70198313009020
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
requirement: &70198313008560 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70198313008560
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: factory_girl
|
49
|
+
requirement: &70198313008140 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70198313008140
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: guard-rspec
|
60
|
+
requirement: &70198313007720 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70198313007720
|
69
|
+
description: scope_filter is a filter- and search-library for ActiveRecord models
|
70
|
+
using scopes
|
71
|
+
email:
|
72
|
+
- lars.kuhnt@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- lib/scope_filter/active_record.rb
|
78
|
+
- lib/scope_filter/configuration.rb
|
79
|
+
- lib/scope_filter.rb
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- CHANGELOG.md
|
83
|
+
- ROADMAP.md
|
84
|
+
homepage: http://github.com/larskuhnt/scope_filter
|
85
|
+
licenses: []
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.3.6
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.10
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: ActiveRecord filter- and search-library using scopes
|
108
|
+
test_files: []
|