mongoid-filterable 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1afed03de888d2a4efd38e70952e7dc9c5021969
4
- data.tar.gz: 9e5ad498c4928cf82e67dade0b302388f8c590f7
3
+ metadata.gz: 0701464e3122f49b16a47beadfc0e01139bbb6ac
4
+ data.tar.gz: e86e64747217c219cb5309b5731291484f7d5d30
5
5
  SHA512:
6
- metadata.gz: 4e995be00a0ae857130777daeab41b23dcf42cb3e8a5046a45536bed6c049546455780872f6ecf534c5acea7b30be2b0c92d193146e45d8a5c07ab0803b022d8
7
- data.tar.gz: 7826c6b4649920c30685716627cc3368ac11b623d748cb394fb35cc3c73d247e6cb6bc99dce3d3ae4066ffcda449afa3f695c883d29203def0c8cd8eef6f4ace
6
+ metadata.gz: 4eda7b941e36e6d2524bffb4ee3e227fe1aff4f9d8c432c0b967e5359ff9450b6fda9336beead0f78ed87a0276af6e2a0fcc6b2bb52c902ca6dd258a272f1a0c
7
+ data.tar.gz: 7deb45a00b151e153be0cb456b0f6f9319edd4e4d9780fe54f6141838149571d77b9ae834fc2a79cca03fff987c29b19aaa2d4bda28a2bafe4a365e25586924b
data/README.md CHANGED
@@ -34,6 +34,10 @@ class City
34
34
 
35
35
  filter_by(:name)
36
36
  filter_by(:people, ->(value) { where(:people.gt => value) })
37
+ filter_by(:people_range, (lambda do |range_start, range_end|
38
+ where(:people.lte => range_end,
39
+ :people.gte => range_start)
40
+ end))
37
41
  end
38
42
 
39
43
  City.create(name: 'city1', people: 100)
@@ -55,6 +59,14 @@ City.filter({name: 'city1', people: 1000}, '$and').count # => 0
55
59
  City.filter({name: 'city1', people: 1000}, '$or').count # => 1
56
60
  ```
57
61
 
62
+ #### Range
63
+
64
+ Searches with more than one param is also available:
65
+
66
+ ```ruby
67
+ City.filter(people_range: [500, 1000]).count # => 1
68
+ ```
69
+
58
70
  #### Rails controller
59
71
 
60
72
  ```ruby
@@ -15,7 +15,11 @@ module Mongoid
15
15
 
16
16
  filtering_params.each do |key, value|
17
17
  if value.present? && respond_to?("filter_with_#{key}")
18
- selectors.push criteria.public_send("filter_with_#{key}", value).selector
18
+ if value.is_a?(Array) && scopes["filter_with_#{key}".to_sym][:scope].arity > 1
19
+ selectors.push criteria.public_send("filter_with_#{key}", *value).selector
20
+ else
21
+ selectors.push criteria.public_send("filter_with_#{key}", value).selector
22
+ end
19
23
  end
20
24
  end
21
25
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Filterable
3
- VERSION = '0.2.1'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -16,6 +16,11 @@ describe Mongoid::Filterable do
16
16
  filter_by(:name)
17
17
  filter_by(:code)
18
18
  filter_by(:people, ->(value) { where(:people.gt => value) })
19
+ filter_by(:people_in, ->(value) { where(:people.in => value) })
20
+ filter_by(:people_range, (lambda do |range_start, range_end|
21
+ where(:people.lte => range_end,
22
+ :people.gte => range_start)
23
+ end))
19
24
  filter_by_normalized(:country)
20
25
  end
21
26
 
@@ -99,4 +104,22 @@ describe Mongoid::Filterable do
99
104
  expect(City.filter(nil).count).to eq(2)
100
105
  end
101
106
  end
107
+
108
+ context 'when value is an array' do
109
+ it 'filter should receive all the values' do
110
+ City.create(people: 100)
111
+ City.create(people: 500)
112
+ City.create(people: 1000)
113
+ City.create(people: 1000)
114
+ expect(City.filter(people_range: [500, 1000]).count).to eq(3)
115
+ end
116
+
117
+ it 'does not break compatibility with filters receiving only one param as array' do
118
+ City.create(people: 100)
119
+ City.create(people: 500)
120
+ City.create(people: 1000)
121
+ City.create(people: 1000)
122
+ expect(City.filter(people_in: [500, 100]).count).to eq(2)
123
+ end
124
+ end
102
125
  end
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.2.1
4
+ version: 0.3.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: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler