mongoid-filterable 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/mongoid-filterable/filterable.rb +13 -15
- data/lib/mongoid-filterable/version.rb +1 -1
- data/lib/mongoid-filterable.rb +2 -2
- data/spec/filterable_spec.rb +19 -11
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8569c578ae3ca2f65454cd555e1e782990d1683d
|
4
|
+
data.tar.gz: 241bf4537c8bbd14941633fb90d3596a3c4e8cbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 850126a6da8200e6eb107f1540e9a027c7c1d44db15baeaf3c058500027c18ee0709e0c48474e55f9be43ff62d21792ecc211b55e6a242d571a10d1b46f3acdc
|
7
|
+
data.tar.gz: c286fa0accc54edd85318462a1940e8cc17a40a3dcede73bf88226fa8713f1b7010843bfa93976359b6717f1f60969f53b0c1a932fca00438a2b0b4039e33e7d
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ class City
|
|
29
29
|
field :people
|
30
30
|
|
31
31
|
filter_by(:name)
|
32
|
-
filter_by(:people,
|
32
|
+
filter_by(:people, ->(value) { where(:people.gt => value) })
|
33
33
|
end
|
34
34
|
|
35
35
|
City.create(name: 'city1', people: 100)
|
@@ -41,7 +41,7 @@ City.filter({people: 500}) # => 1
|
|
41
41
|
|
42
42
|
#### Operator
|
43
43
|
|
44
|
-
You can specify selector operator:
|
44
|
+
You can specify selector operator:
|
45
45
|
|
46
46
|
* $and (default operator)
|
47
47
|
* $or
|
@@ -58,7 +58,7 @@ class CitiesController
|
|
58
58
|
def index
|
59
59
|
respond_with City.filter(filter_params)
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
private
|
63
63
|
|
64
64
|
def filter_params
|
@@ -7,33 +7,31 @@ module Mongoid
|
|
7
7
|
##
|
8
8
|
# Applies params scopes to current scope
|
9
9
|
#
|
10
|
-
def filter(filtering_params, operator='$and')
|
10
|
+
def filter(filtering_params, operator = '$and')
|
11
11
|
return self unless filtering_params
|
12
|
-
results =
|
12
|
+
results = all
|
13
13
|
selectors = []
|
14
|
+
criteria = Mongoid::Criteria.new(self)
|
14
15
|
|
15
16
|
filtering_params.each do |key, value|
|
16
|
-
if value.present? &&
|
17
|
-
selectors.push
|
17
|
+
if value.present? && respond_to?("filter_with_#{key}")
|
18
|
+
selectors.push criteria.public_send("filter_with_#{key}", value).selector
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
-
results
|
22
|
+
selectors.empty? ? results : results.where(operator => selectors)
|
23
23
|
end
|
24
24
|
|
25
25
|
##
|
26
26
|
# Adds attr scope
|
27
27
|
#
|
28
|
-
def filter_by(attr, filter=nil)
|
28
|
+
def filter_by(attr, filter = nil)
|
29
29
|
if filter
|
30
|
-
|
30
|
+
scope "filter_with_#{attr}", filter
|
31
|
+
elsif fields[attr.to_s].type == String
|
32
|
+
scope "filter_with_#{attr}", ->(value) { where(attr => Regexp.new(value)) }
|
31
33
|
else
|
32
|
-
|
33
|
-
self.scope "filter_with_#{attr}", lambda{ |value| where(attr => Regexp.new(value)) }
|
34
|
-
else
|
35
|
-
self.scope "filter_with_#{attr}", lambda{ |value| where(attr => value) }
|
36
|
-
end
|
34
|
+
scope "filter_with_#{attr}", ->(value) { where(attr => value) }
|
37
35
|
end
|
38
36
|
end
|
39
37
|
|
@@ -43,8 +41,8 @@ module Mongoid
|
|
43
41
|
#
|
44
42
|
def filter_by_normalized(attr)
|
45
43
|
normalized_name = (attr.to_s + '_normalized').to_sym
|
46
|
-
|
47
|
-
where(
|
44
|
+
scope "filter_with_#{attr}", lambda { |value|
|
45
|
+
where(normalized_name => Regexp.new(I18n.transliterate(value), 'i'))
|
48
46
|
}
|
49
47
|
end
|
50
48
|
end
|
data/lib/mongoid-filterable.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'mongoid-filterable/version'
|
2
|
+
require 'mongoid-filterable/filterable'
|
data/spec/filterable_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe Mongoid::Filterable do
|
|
12
12
|
|
13
13
|
filter_by(:name)
|
14
14
|
filter_by(:code)
|
15
|
-
filter_by(:people,
|
15
|
+
filter_by(:people, ->(value) { where(:people.gt => value) })
|
16
16
|
filter_by_normalized(:country)
|
17
17
|
end
|
18
18
|
|
@@ -24,30 +24,38 @@ describe Mongoid::Filterable do
|
|
24
24
|
it 'should filter by default filter' do
|
25
25
|
City.create(code: :code1)
|
26
26
|
City.create(code: :code2)
|
27
|
-
expect(City.filter(
|
28
|
-
expect(City.filter(
|
27
|
+
expect(City.filter(code: :code1).count).to eq(1)
|
28
|
+
expect(City.filter(code: :code1).first.code).to eq(:code1)
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'should filter by match filter' do
|
32
32
|
City.create(name: 'city1')
|
33
33
|
City.create(name: 'city2')
|
34
|
-
expect(City.filter(
|
35
|
-
expect(City.filter(
|
36
|
-
expect(City.filter(
|
34
|
+
expect(City.filter(name: 'city').count).to eq(2)
|
35
|
+
expect(City.filter(name: 'city1').count).to eq(1)
|
36
|
+
expect(City.filter(name: 'city1').first.name).to eq('city1')
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should filter by custom filter' do
|
40
40
|
City.create(people: 100)
|
41
41
|
City.create(people: 1000)
|
42
|
-
expect(City.filter(
|
43
|
-
expect(City.filter(
|
42
|
+
expect(City.filter(people: 500).count).to eq(1)
|
43
|
+
expect(City.filter(people: 500).first.people).to eq(1000)
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'should filter by normalized filter' do
|
47
47
|
City.create(country_normalized: 'spain')
|
48
48
|
City.create(country_normalized: 'france')
|
49
|
-
expect(City.filter(
|
50
|
-
expect(City.filter(
|
49
|
+
expect(City.filter(country: 'spain').count).to eq(1)
|
50
|
+
expect(City.filter(country: 'spain').first.country_normalized).to eq('spain')
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should respect previous selector' do
|
54
|
+
City.create(name: 'city1', people: 100)
|
55
|
+
City.create(name: 'city2', people: 1000)
|
56
|
+
City.create(name: 'city3', people: 2000)
|
57
|
+
expect(City.where(name: 'city2').filter(people: '500').count).to eq(1)
|
58
|
+
expect(City.where(name: 'city2').filter(people: '500').first.name).to eq('city2')
|
51
59
|
end
|
52
60
|
end
|
53
61
|
|
@@ -71,7 +79,7 @@ describe Mongoid::Filterable do
|
|
71
79
|
it 'should ignore filter' do
|
72
80
|
City.create(name: 'city1')
|
73
81
|
City.create(name: 'city2')
|
74
|
-
expect(City.filter(
|
82
|
+
expect(City.filter(invalid: 'val').count).to eq(2)
|
75
83
|
end
|
76
84
|
end
|
77
85
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-filterable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Jurado
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -113,3 +113,4 @@ test_files:
|
|
113
113
|
- spec/filterable_spec.rb
|
114
114
|
- spec/spec_helper.rb
|
115
115
|
- spec/support/mongoid.yml
|
116
|
+
has_rdoc:
|