filtron 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +84 -0
- data/Rakefile +13 -0
- data/filtron.gemspec +17 -0
- data/lib/filtron.rb +70 -0
- data/test/filtron_test.rb +43 -0
- data/test/test_helper.rb +4 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4da48a4bd725abada21ee792367ac234463e213b
|
4
|
+
data.tar.gz: 4fbd214421276dd779381371def7e4f926cd90eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c2108831492429deb0c1bd0dd64ff292a6b89b0ec5abdcf8878949ad57b8c373a0011deca52fac86b0110860aa166c3b1d685741b34b93265e70bbc9be5678c3
|
7
|
+
data.tar.gz: 49173cd2de3913c79a5315794b45fc444affb886aad8e6297036e59e257de0e5b17c0a8d293feac1b938ba73c10d8ed06e6988cbbb02eb1b2c1c9c55b12d14e9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Lee Jarvis
|
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,84 @@
|
|
1
|
+
# Filtron
|
2
|
+
|
3
|
+
Filtron was built to work with [Ransack](https://github.com/ernie/ransack).
|
4
|
+
Ransack uses search predicates to simplify searching and querying your model
|
5
|
+
data. Filtron aims to alias these predicates so your URLs are a little
|
6
|
+
less ugly when filtering your data via GET requests. Take this URL for example:
|
7
|
+
|
8
|
+
http://example.com/users?user[first_name_eq]=Lee&user[email_cont]=@gmail
|
9
|
+
|
10
|
+
# Controller:
|
11
|
+
def index
|
12
|
+
@users = User.search(params[:user]).result
|
13
|
+
end
|
14
|
+
|
15
|
+
Wouldn't this be nicer?
|
16
|
+
|
17
|
+
http://example.com/users?name=Lee&email=@gmail
|
18
|
+
|
19
|
+
# Controller:
|
20
|
+
def index
|
21
|
+
@users = User.filter(params)
|
22
|
+
end
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
gem 'filtron'
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
You can easily add Filtron to your existing models
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
filter_with :fname, :first_name_eq
|
37
|
+
filter_with :lname, :last_name_eq
|
38
|
+
filter_with :country, :address_country_name_eq do |code|
|
39
|
+
Country.find_by_code(code).name
|
40
|
+
end
|
41
|
+
|
42
|
+
queries :first_name, :email
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Now in your controller:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
def index
|
50
|
+
@users = User.filter(params)
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
`params` might look something like this:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
{
|
58
|
+
fname: "Lee",
|
59
|
+
country: "gb",
|
60
|
+
q: "@gmail.com",
|
61
|
+
something: "else"
|
62
|
+
}
|
63
|
+
```
|
64
|
+
|
65
|
+
This will translate into the following Ransack predicates:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
{
|
69
|
+
first_name_eq: "Lee",
|
70
|
+
address_country_name_eq: "Great Britain",
|
71
|
+
first_name_or_email_cont: "@gmail.com"
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
Filtron will then use these predicates to search for and return a result.
|
76
|
+
|
77
|
+
By default, `queries` will use `params[:q]` for a general query. You can
|
78
|
+
change this in the call to `queries`:
|
79
|
+
|
80
|
+
```
|
81
|
+
queries :first_name, :last_name, :email, with: :search
|
82
|
+
```
|
83
|
+
|
84
|
+
Now, `/users?search=foo` will use these query predicates.
|
data/Rakefile
ADDED
data/filtron.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "filtron"
|
3
|
+
spec.version = "0.0.1"
|
4
|
+
spec.authors = ["Lee Jarvis"]
|
5
|
+
spec.email = ["ljjarvis@gmail.com"]
|
6
|
+
spec.description = "Pretty aliases for filtering your data"
|
7
|
+
spec.summary = "Simple filtering"
|
8
|
+
spec.homepage = "https://github.com/leejarvis/filtron"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
spec.files = `git ls-files`.split($/)
|
12
|
+
spec.test_files = spec.files.grep(/^test/)
|
13
|
+
spec.require_paths = ["lib"]
|
14
|
+
|
15
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
16
|
+
spec.add_development_dependency "rake"
|
17
|
+
end
|
data/lib/filtron.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Filtron
|
2
|
+
VERSION = "0.0.1"
|
3
|
+
|
4
|
+
def filtron_filters
|
5
|
+
@filtron_filters ||= {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def filtron_query_predicate
|
9
|
+
@filtron_query_predicate
|
10
|
+
end
|
11
|
+
|
12
|
+
def filtron_query_param
|
13
|
+
@filtron_query_param ||= :q
|
14
|
+
end
|
15
|
+
|
16
|
+
def filter_with(param, predicate, options = {}, &block)
|
17
|
+
filtron_filters[param] = [predicate, options, block]
|
18
|
+
end
|
19
|
+
|
20
|
+
def queries(*columns)
|
21
|
+
options = columns[-1].is_a?(Hash) ? columns.pop : {}
|
22
|
+
suffix = options.fetch(:suffix, "_cont")
|
23
|
+
suffix = "_#{suffix}" unless suffix.start_with?("_")
|
24
|
+
|
25
|
+
@filtron_query_param = options.fetch(:with, @filtron_query_param)
|
26
|
+
@filtron_query_predicate = columns.join("_or_") + suffix
|
27
|
+
end
|
28
|
+
|
29
|
+
def filter(filters)
|
30
|
+
conditions = filters_to_conditions(filters)
|
31
|
+
|
32
|
+
if filters[:ransack_options]
|
33
|
+
search(conditions).result(filters[:ransack_options])
|
34
|
+
else
|
35
|
+
search(conditions)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def filters_to_conditions(params)
|
42
|
+
predicates = filtron_filters
|
43
|
+
filters = filters_from_predicates(params, predicates)
|
44
|
+
add_query_predicate(filters, params)
|
45
|
+
Hash[filters]
|
46
|
+
end
|
47
|
+
|
48
|
+
def filters_from_predicates(params, predicates)
|
49
|
+
slice(params, *predicates.keys).map do |key, value|
|
50
|
+
filter = predicates[key]
|
51
|
+
value = filter[-1].call(value) if filter[-1].respond_to?(:call)
|
52
|
+
[filter[0], value]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def slice(hash, *keys)
|
57
|
+
keys.each_with_object({}) { |k, h| h[k] = hash[k] if hash.key?(k) }
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_query_predicate(filters, params)
|
61
|
+
key = filtron_query_param
|
62
|
+
if key && params[key]
|
63
|
+
filters << [filtron_query_predicate, params[key]]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if defined? ActiveRecord::Base
|
69
|
+
ActiveRecord::Base.extend Filtron
|
70
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FiltronTest < Minitest::Test
|
4
|
+
|
5
|
+
class Search
|
6
|
+
attr_reader :conditions
|
7
|
+
|
8
|
+
def initialize(conditions)
|
9
|
+
@conditions = conditions
|
10
|
+
end
|
11
|
+
|
12
|
+
def result(options = {})
|
13
|
+
self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class User
|
18
|
+
extend Filtron
|
19
|
+
|
20
|
+
filter_with :name, :name_eq
|
21
|
+
filter_with :country, :country_name_eq do |code|
|
22
|
+
{"gb" => "Great Britain"}[code]
|
23
|
+
end
|
24
|
+
queries :name, :email, with: :s
|
25
|
+
|
26
|
+
def self.search(conditions)
|
27
|
+
Search.new(conditions)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_adding_filters
|
32
|
+
assert_equal [:name_eq, {}, nil], User.filtron_filters[:name]
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_filters_with_blocks
|
36
|
+
assert_equal "Great Britain", User.filter(country: "gb").conditions[:country_name_eq]
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_adding_queries
|
40
|
+
assert_equal({"name_or_email_cont" => "foo"}, User.filter(s: "foo").conditions)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filtron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lee Jarvis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Pretty aliases for filtering your data
|
42
|
+
email:
|
43
|
+
- ljjarvis@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- filtron.gemspec
|
54
|
+
- lib/filtron.rb
|
55
|
+
- test/filtron_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
homepage: https://github.com/leejarvis/filtron
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.0.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Simple filtering
|
81
|
+
test_files:
|
82
|
+
- test/filtron_test.rb
|
83
|
+
- test/test_helper.rb
|