filterism 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -24
- data/.irbrc +2 -0
- data/.rspec +1 -2
- data/Gemfile +2 -2
- data/LICENSE.txt +22 -0
- data/README.md +41 -4
- data/Rakefile +1 -17
- data/exec/autospec +16 -0
- data/exec/htmldiff +16 -0
- data/exec/ldiff +16 -0
- data/exec/rake +16 -0
- data/exec/rspec +16 -0
- data/filterism.gemspec +25 -22
- data/lib/filterism.rb +3 -79
- data/lib/filterism/adapters/active_record_adapter.rb +45 -0
- data/lib/filterism/parsers/params_parser.rb +51 -0
- data/lib/filterism/version.rb +1 -1
- data/spec/adapters/active_record_adapter_spec.rb +84 -0
- data/spec/db/schema.rb +7 -0
- data/spec/factories.rb +8 -0
- data/spec/parsers/params_parser_spec.rb +52 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/active_record.rb +24 -0
- metadata +150 -148
- data/spec/filterism_spec.rb +0 -91
- data/spec/finders/activerecord_test_connector.rb +0 -104
- data/spec/fixtures/post.rb +0 -3
- data/spec/fixtures/posts.yml +0 -16
- data/spec/fixtures/schema.rb +0 -17
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -30
- data/spec/helper.rb +0 -6
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5702e936b3509a35a0988602855c6ec4a8e0e758
|
4
|
+
data.tar.gz: 91adb5ae786eee091d36df6938391660b7b68d36
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9205254ef687c7a6dfbd695ff9833b5feef1b8efaf7554ad16e03f3d4b67b4b6dfcdd71e86ffd9c2cbda08634fce39fb4492d1c345bbafa58b3610d42137c67e
|
7
|
+
data.tar.gz: 32035e5f2ac954fc310c82ca6f2a68a5a44de173f00d52c325f8331e112d3a827aed00151317ec5e97c019e997a07113fdec3f694ddf3aab0fb2ead7298d6b3b
|
data/.gitignore
CHANGED
@@ -1,41 +1,22 @@
|
|
1
|
-
!.gitignore
|
2
1
|
*.gem
|
3
2
|
*.rbc
|
4
|
-
*.sw[a-p]
|
5
|
-
*.tmproj
|
6
|
-
*.tmproject
|
7
|
-
*.un~
|
8
|
-
*~
|
9
|
-
.DS_Store
|
10
|
-
.Spotlight-V100
|
11
|
-
.Trashes
|
12
|
-
._*
|
13
3
|
.bundle
|
14
4
|
.config
|
15
|
-
.directory
|
16
|
-
.elc
|
17
|
-
.redcar
|
18
5
|
.yardoc
|
19
|
-
/.emacs.desktop
|
20
|
-
/.emacs.desktop.lock
|
21
|
-
Desktop.ini
|
22
6
|
Gemfile.lock
|
23
|
-
Icon?
|
24
7
|
InstalledFiles
|
25
|
-
Session.vim
|
26
|
-
Thumbs.db
|
27
|
-
\#*\#
|
28
8
|
_yardoc
|
29
|
-
auto-save-list
|
30
9
|
coverage
|
31
10
|
doc/
|
32
11
|
lib/bundler/man
|
33
12
|
pkg
|
34
|
-
pkg/*
|
35
13
|
rdoc
|
36
14
|
spec/reports
|
37
15
|
test/tmp
|
38
16
|
test/version_tmp
|
39
17
|
tmp
|
40
|
-
|
41
|
-
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.irbrc
ADDED
data/.rspec
CHANGED
data/Gemfile
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Phil Spitler
|
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
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# Filterism
|
2
2
|
|
3
|
-
A DSL for filtering
|
4
|
-
term _is_ in them.
|
3
|
+
A DSL for filtering items (usually in some form of record store).
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
@@ -12,6 +11,25 @@ gem 'filterism'
|
|
12
11
|
gem install filterism
|
13
12
|
|
14
13
|
## Usage
|
14
|
+
All examples use the ActiveRecordAdapter which is included with the gem. Other adapters may be created to meet your needs. These adapters generate the syntax to speak to whatever record store you need.
|
15
|
+
|
16
|
+
### Globally
|
17
|
+
Adds .filter, .filterable_fields and .unfilterable_fields methods to all of you models
|
18
|
+
Somewhere in your apps bootstrap/startup code:
|
19
|
+
|
20
|
+
``` ruby
|
21
|
+
ActiveRecord::Base.include(Filterism)
|
22
|
+
```
|
23
|
+
|
24
|
+
### Locally (in a model)
|
25
|
+
Adds .filter, .filterable_fields and .unfilterable_fields methods to only the User model
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
include Filterism
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
15
33
|
Filterism was developed to fill the need of being able to filter Active Record results by aguments passed in the URL, however, it basically takes in a hash. Usually that hash would be "params" passed from the controller. The syntax of the keys in the hash control how the data is filtered.
|
16
34
|
|
17
35
|
It also adds a ".filterable_fields" method for you to limit by which fields
|
@@ -28,13 +46,24 @@ In the first argument, name is the [field] and [in] is the comparator.
|
|
28
46
|
In the second argument, salary is the [field] and [greater_than] is the comparator.
|
29
47
|
|
30
48
|
### Model
|
49
|
+
#### Filterable Fields
|
31
50
|
In the model you can limit which fields are filterable. This is optional. If this isn't in your model, all fields are filterable.
|
32
51
|
|
33
52
|
``` ruby
|
34
53
|
class User < ActiveRecord::Base
|
35
|
-
filterable_fields :name
|
54
|
+
filterable_fields :name
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
#### Unfilterable Fields
|
59
|
+
|
60
|
+
``` ruby
|
61
|
+
class User < ActiveRecord::Base
|
62
|
+
unfilterable_fields :salary
|
36
63
|
end
|
37
64
|
```
|
65
|
+
The examples above are equivalent. If you have a long list of fields, you can use either a whitelist or blacklist depending on your needs
|
66
|
+
|
38
67
|
|
39
68
|
### Controller
|
40
69
|
This gem adds the ".filter" class method to any active record class. You can pass in any hash, it doesn't have to operate from params. It will only look at keys that have the "\_is\_" syntax.
|
@@ -71,4 +100,12 @@ COMPARATORS = {
|
|
71
100
|
|
72
101
|
You can see that you can do things like [field]_is_gt=4000 or [field]_is_in=4,66,19
|
73
102
|
|
74
|
-
Please see https://github.com/philspitler/filterism/blob/master/spec/filterism_spec.rb for more examples of using ".filter"
|
103
|
+
Please see https://github.com/philspitler/filterism/blob/master/spec/filterism_spec.rb for more examples of using ".filter"
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
1. Fork it ( https://github.com/[my-github-username]/filterism/fork )
|
108
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
109
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
110
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
111
|
+
5. Create a new Pull Request
|
data/Rakefile
CHANGED
@@ -1,18 +1,2 @@
|
|
1
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
2
|
|
3
|
-
require 'bundler'
|
4
|
-
Bundler::GemHelper.install_tasks
|
5
|
-
|
6
|
-
require 'rspec/core/rake_task'
|
7
|
-
RSpec::Core::RakeTask.new(:spec)
|
8
|
-
|
9
|
-
task :default => :spec
|
10
|
-
task :test => :spec
|
11
|
-
|
12
|
-
require 'yard'
|
13
|
-
namespace :doc do
|
14
|
-
YARD::Rake::YardocTask.new do |task|
|
15
|
-
task.files = ['LICENSE.md', 'lib/**/*.rb']
|
16
|
-
task.options = ['--markup', 'markdown']
|
17
|
-
end
|
18
|
-
end
|
data/exec/autospec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'autospec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'autospec')
|
data/exec/htmldiff
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'htmldiff' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('diff-lcs', 'htmldiff')
|
data/exec/ldiff
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'ldiff' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('diff-lcs', 'ldiff')
|
data/exec/rake
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rake' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rake', 'rake')
|
data/exec/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/filterism.gemspec
CHANGED
@@ -1,26 +1,29 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'filterism/version'
|
3
5
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "filterism"
|
8
|
+
spec.version = Filterism::VERSION
|
9
|
+
spec.authors = ["Phil Spitler"]
|
10
|
+
spec.email = ["pspitler@gmail.com"]
|
11
|
+
spec.summary = "DSL for communicating filter intent via HTTP parameters in the URL"
|
12
|
+
spec.description = ""
|
13
|
+
spec.homepage = "http://sproutkey.com"
|
14
|
+
spec.license = "MIT"
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "bond"
|
25
|
+
spec.add_development_dependency "simplecov"
|
26
|
+
spec.add_development_dependency "activerecord"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_development_dependency "factory_girl", "~> 4.0"
|
26
29
|
end
|
data/lib/filterism.rb
CHANGED
@@ -1,79 +1,3 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
attr_accessor :filterable_fields
|
5
|
-
|
6
|
-
COMPARATORS = {
|
7
|
-
'equal_to' => '=',
|
8
|
-
'not_equal_to' => '!=',
|
9
|
-
|
10
|
-
'greater_than' => '>',
|
11
|
-
'gt' => '>',
|
12
|
-
'after' => '>',
|
13
|
-
'greater_than_or_equal_to' => '>=',
|
14
|
-
'gtet' => '>=',
|
15
|
-
|
16
|
-
'less_than' => '<',
|
17
|
-
'lt' => '<',
|
18
|
-
'before' => '<',
|
19
|
-
'less_than_or_equal_to' => '<=',
|
20
|
-
'ltet' => '<=',
|
21
|
-
|
22
|
-
'like' => 'LIKE',
|
23
|
-
|
24
|
-
'in' => 'IN'
|
25
|
-
}
|
26
|
-
|
27
|
-
def filter(params)
|
28
|
-
|
29
|
-
#remove non filter params
|
30
|
-
params.delete_if { |key, value| key.to_s.index('_is_').nil? }
|
31
|
-
|
32
|
-
where_clause = ""
|
33
|
-
values = []
|
34
|
-
|
35
|
-
params.each do |key, value|
|
36
|
-
keysplit = key.to_s.split('_is_')
|
37
|
-
unless @filterable_fields and not @filterable_fields.include?(keysplit[0].to_sym)
|
38
|
-
|
39
|
-
if COMPARATORS.keys.include?(keysplit[1])
|
40
|
-
column = keysplit[0]
|
41
|
-
comparator = COMPARATORS[keysplit[1]]
|
42
|
-
|
43
|
-
where_mark = '?'
|
44
|
-
|
45
|
-
if comparator == 'LIKE'
|
46
|
-
values << "%#{value}%"
|
47
|
-
elsif comparator == 'IN'
|
48
|
-
where_mark = '(?)'
|
49
|
-
values << value.split(',')
|
50
|
-
else
|
51
|
-
values << convert_if_string_is_boolean(value)
|
52
|
-
end
|
53
|
-
|
54
|
-
where_clause << " AND" unless where_clause.empty?
|
55
|
-
|
56
|
-
where_clause << " #{column} #{comparator} #{where_mark}"
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
where(where_clause, *values)
|
63
|
-
end
|
64
|
-
|
65
|
-
def filterable_fields(*fields)
|
66
|
-
@filterable_fields = fields
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
def convert_if_string_is_boolean(value)
|
71
|
-
if value == "true" || value == "false"
|
72
|
-
value = "true" ? true : false
|
73
|
-
end
|
74
|
-
return value
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
ActiveRecord::Base.extend Filterism
|
1
|
+
require "filterism/version"
|
2
|
+
require "filterism/parsers/params_parser"
|
3
|
+
require "filterism/adapters/active_record_adapter"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module Filterism
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def filterable_fields(*fields)
|
8
|
+
@filterable_fields = fields.to_s
|
9
|
+
end
|
10
|
+
def unfilterable_fields(*fields)
|
11
|
+
@unfilterable_fields = fields.to_s
|
12
|
+
end
|
13
|
+
def filter(conditions)
|
14
|
+
where_clause = ""
|
15
|
+
values = []
|
16
|
+
where_mark = '?'
|
17
|
+
conditions.each do |condition|
|
18
|
+
unless (@filterable_fields and not @filterable_fields.include?(condition[:key])) or (@unfilterable_fields and @unfilterable_fields.include?(condition[:key]))
|
19
|
+
if condition[:comparator] == 'LIKE'
|
20
|
+
values << "%#{condition[:value]}%"
|
21
|
+
elsif condition[:comparator] == 'IN'
|
22
|
+
where_mark = '(?)'
|
23
|
+
values << condition[:value].split(',')
|
24
|
+
else
|
25
|
+
values << convert_if_string_is_boolean(condition[:value])
|
26
|
+
end
|
27
|
+
|
28
|
+
where_clause << " AND" unless where_clause.empty?
|
29
|
+
|
30
|
+
where_clause << " #{condition[:key]} #{condition[:comparator]} #{where_mark}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
where(where_clause, *values)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def convert_if_string_is_boolean(value)
|
39
|
+
if value == "true" || value == "false"
|
40
|
+
value = (value == "true" ? "t" : "f")
|
41
|
+
end
|
42
|
+
return value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|