mongoid-filterable 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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +94 -0
- data/Rakefile +1 -0
- data/lib/mongoid-filterable.rb +2 -0
- data/lib/mongoid-filterable/filterable.rb +52 -0
- data/lib/mongoid-filterable/version.rb +5 -0
- data/mongoid-filterable.gemspec +26 -0
- data/spec/filterable_spec.rb +59 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/mongoid.yml +6 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 112246751dd972a531a49a44a390f5410c5e4381
|
4
|
+
data.tar.gz: 88a7ccd863fe6c8c0caef1622bf961150d3d53c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46e4dfeec6248cf4d023d68c6d555aea1ff3ab40388181b0c3e964ff08cadd69ef5a77c7890c09d11fa141f482424612804f656fe63b059eda18f27dc59eda8c
|
7
|
+
data.tar.gz: 5c9db4c56af8acb8de1f93cb4945c2070973b42e226fcf436e752b971f362406e5bcc62aa584a8c8aeb715f677f9d3ff0ea9589b4fefc2f71c8e6ad3c0b12130
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Rafael Jurado
|
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,94 @@
|
|
1
|
+
# Mongoid::Filterable
|
2
|
+
|
3
|
+
Let you add scopes to mongoid document for filters.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mongoid-filterable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mongoid-filterable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
#### Model
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class City
|
25
|
+
include Mongoid::Document
|
26
|
+
include Mongoid::Filterable
|
27
|
+
|
28
|
+
field :name
|
29
|
+
field :people
|
30
|
+
|
31
|
+
filter_by(:name)
|
32
|
+
filter_by(:people, lambda{|value| where(:people.gt => value)})
|
33
|
+
end
|
34
|
+
|
35
|
+
City.create(name: 'city1', people: 100)
|
36
|
+
City.create(name: 'city2', people: 1000)
|
37
|
+
City.filter({name: 'city1'}).count # => 1
|
38
|
+
City.filter({people: 500}) # => 1
|
39
|
+
```
|
40
|
+
|
41
|
+
#### Operator
|
42
|
+
|
43
|
+
You can specify selector operator:
|
44
|
+
|
45
|
+
* $and (default operator)
|
46
|
+
* $or
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
City.filter({name: 'city1', people: 1000}, '$and').count # => 0
|
50
|
+
City.filter({name: 'city1', people: 1000}, '$or').count # => 1
|
51
|
+
```
|
52
|
+
|
53
|
+
#### Rails controller
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class CitiesController
|
57
|
+
def index
|
58
|
+
respond_with City.filter(filter_params)
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def filter_params
|
64
|
+
params.slice(:name, :people)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
#### With [mongoid-normalize-string](https://github.com/nosolosoftware/mongoid-normalize-strings) gem
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class City
|
73
|
+
include Mongoid::Document
|
74
|
+
include Mongoid::Filterable
|
75
|
+
include Mongoid::NormalizeStrings
|
76
|
+
|
77
|
+
field :name
|
78
|
+
normalize :name
|
79
|
+
|
80
|
+
filter_by_normalize(:name)
|
81
|
+
end
|
82
|
+
|
83
|
+
City.create(name: 'Cíty1')
|
84
|
+
City.create(name: 'Cíty2')
|
85
|
+
City.filter({name: 'city1'}).count # => 1
|
86
|
+
```
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
1. Fork it ( http://github.com/<my-github-username>/mongoid-filterable/fork )
|
91
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
92
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
93
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
94
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,52 @@
|
|
1
|
+
##
|
2
|
+
# Let you add scopes to mongoid document for filters.
|
3
|
+
#
|
4
|
+
module Mongoid
|
5
|
+
module Filterable
|
6
|
+
module ClassMethods
|
7
|
+
##
|
8
|
+
# Applies params scopes to current scope
|
9
|
+
#
|
10
|
+
def filter(filtering_params, operator='$and')
|
11
|
+
results = self.all
|
12
|
+
selectors = []
|
13
|
+
|
14
|
+
filtering_params.each do |key, value|
|
15
|
+
selectors.push self.public_send("filter_with_#{key}", value).selector if value.present?
|
16
|
+
end
|
17
|
+
|
18
|
+
results.selector = {operator => selectors} if selectors.size > 0
|
19
|
+
results
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# Adds attr scope
|
24
|
+
#
|
25
|
+
def filter_by(attr, filter=nil)
|
26
|
+
if filter
|
27
|
+
self.scope "filter_with_#{attr}", filter
|
28
|
+
else
|
29
|
+
self.scope "filter_with_#{attr}", lambda{ |value| where(attr => value)}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Adds attr scope using normalized values
|
35
|
+
# (see gem mongoid-normalize-strings)
|
36
|
+
#
|
37
|
+
def filter_by_normalized(attr)
|
38
|
+
normalized_name = (attr.to_s + '_normalized').to_sym
|
39
|
+
self.scope "filter_with_#{attr}", lambda{ |value|
|
40
|
+
where( normalized_name => Regexp.new(I18n.transliterate(value), 'i') )
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Adds class methods
|
47
|
+
#
|
48
|
+
def self.included(base)
|
49
|
+
base.extend(ClassMethods)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid-filterable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid-filterable"
|
8
|
+
spec.version = Mongoid::Filterable::VERSION
|
9
|
+
spec.authors = ["Rafael Jurado"]
|
10
|
+
spec.email = ["rjurado@nosolosoftware.biz"]
|
11
|
+
spec.summary = %q{Easy way to add scopes to your models.}
|
12
|
+
spec.description = %q{Easy way to add scopes to your models.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
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"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "i18n"
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'mongoid'
|
26
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Filterable do
|
4
|
+
class City
|
5
|
+
include Mongoid::Document
|
6
|
+
include Mongoid::Filterable
|
7
|
+
|
8
|
+
field :name
|
9
|
+
field :people
|
10
|
+
field :country_normalized
|
11
|
+
|
12
|
+
filter_by(:name)
|
13
|
+
filter_by(:people, lambda{|value| where(:people.gt => value)})
|
14
|
+
filter_by_normalized(:country)
|
15
|
+
end
|
16
|
+
|
17
|
+
before do
|
18
|
+
City.destroy_all
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when use default operator' do
|
22
|
+
it 'should filter by default filter' do
|
23
|
+
City.create(name: 'city1')
|
24
|
+
City.create(name: 'city2')
|
25
|
+
expect(City.filter({name: 'city1'}).count).to eq(1)
|
26
|
+
expect(City.filter({name: 'city1'}).first.name).to eq('city1')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should filter by custom filter' do
|
30
|
+
City.create(people: 100)
|
31
|
+
City.create(people: 1000)
|
32
|
+
expect(City.filter({people: 500}).count).to eq(1)
|
33
|
+
expect(City.filter({people: 500}).first.people).to eq(1000)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should filter by normalized filter' do
|
37
|
+
City.create(country_normalized: 'spain')
|
38
|
+
City.create(country_normalized: 'france')
|
39
|
+
expect(City.filter({country: 'spain'}).count).to eq(1)
|
40
|
+
expect(City.filter({country: 'spain'}).first.country_normalized).to eq('spain')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when use $and operator' do
|
45
|
+
it 'should filter using and query' do
|
46
|
+
City.create(name: 'city1', people: 100)
|
47
|
+
City.create(name: 'city2', people: 2000)
|
48
|
+
expect(City.filter({name: 'city1', people: '2000'}, '$and').count).to eq(0)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when use $or operator' do
|
53
|
+
it 'should filter using or query' do
|
54
|
+
City.create(name: 'city1', people: 100)
|
55
|
+
City.create(name: 'city2', people: 2000)
|
56
|
+
expect(City.filter({name: 'city1', people: '2000'}, '$or').count).to eq(1)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-filterable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Jurado
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-25 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: i18n
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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: mongoid
|
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: Easy way to add scopes to your models.
|
84
|
+
email:
|
85
|
+
- rjurado@nosolosoftware.biz
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/mongoid-filterable.rb
|
96
|
+
- lib/mongoid-filterable/filterable.rb
|
97
|
+
- lib/mongoid-filterable/version.rb
|
98
|
+
- mongoid-filterable.gemspec
|
99
|
+
- spec/filterable_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/support/mongoid.yml
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.4.5.1
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Easy way to add scopes to your models.
|
126
|
+
test_files:
|
127
|
+
- spec/filterable_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/support/mongoid.yml
|