mongoid-filterable 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fde9d83b88b2d886f664e4b8f6259b211bcc8de3
4
- data.tar.gz: e210953988bc2bf8d6312088f0d5c5242edf9f03
3
+ metadata.gz: 8569c578ae3ca2f65454cd555e1e782990d1683d
4
+ data.tar.gz: 241bf4537c8bbd14941633fb90d3596a3c4e8cbd
5
5
  SHA512:
6
- metadata.gz: d785c82ca2ab77429e5c205d542e93bad551e98a5b65ef0d2bc3729a3250e245f166f4764ef6ea374a355cfd57ef94e21654734fba0f6fea0e13e34de18003f4
7
- data.tar.gz: 5ecd66e9db4e1fdb733c36d75ddf9bb027718e4512b617f23bfb5bda63e2d0fee61df364d76dff1fc64e7a16218dbe090c615d7f9262f814239773415f11c743
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, lambda{|value| where(:people.gt => value)})
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 = self.all
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? && self.respond_to?("filter_with_#{key}")
17
- selectors.push self.public_send("filter_with_#{key}", value).selector
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
- results.selector = {operator => selectors} if selectors.size > 0
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
- self.scope "filter_with_#{attr}", filter
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
- if self.fields[attr.to_s].type == String
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
- self.scope "filter_with_#{attr}", lambda{ |value|
47
- where( normalized_name => Regexp.new(I18n.transliterate(value), 'i') )
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
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Filterable
3
- VERSION = "0.1.1"
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -1,2 +1,2 @@
1
- require "mongoid-filterable/version"
2
- require "mongoid-filterable/filterable"
1
+ require 'mongoid-filterable/version'
2
+ require 'mongoid-filterable/filterable'
@@ -12,7 +12,7 @@ describe Mongoid::Filterable do
12
12
 
13
13
  filter_by(:name)
14
14
  filter_by(:code)
15
- filter_by(:people, lambda{|value| where(:people.gt => value)})
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({code: :code1}).count).to eq(1)
28
- expect(City.filter({code: :code1}).first.code).to eq(:code1)
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({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')
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({people: 500}).count).to eq(1)
43
- expect(City.filter({people: 500}).first.people).to eq(1000)
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({country: 'spain'}).count).to eq(1)
50
- expect(City.filter({country: 'spain'}).first.country_normalized).to eq('spain')
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({invalid: 'val'}).count).to eq(2)
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.1.1
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: 2016-05-09 00:00:00.000000000 Z
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: