ransack_mongo 1.0.0 → 1.0.1

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: 53ef1016a5f5047576966e475de778a6e290576e
4
- data.tar.gz: f48e2f0923334b94ae39607289b2122ef757df09
3
+ metadata.gz: f59a6372320405705331f828b7b8c102194349bd
4
+ data.tar.gz: d90c418ebbe503e5a72d730a0a8d4b195f7a1578
5
5
  SHA512:
6
- metadata.gz: 08cc605b686091b44734ef6f5e1ed2d6501edc387db67dfd8ccc8985691feb7de64af042b5cf9ea7b23fbe1814f3e0d667359837641b9e22996a41e1c542be74
7
- data.tar.gz: 184cc91865c21b2e339a8d508645282f0a4de53e62bfa2d9e5979277207cce252972d2538fa93714ef67038293e5ea5dc4e442ef09b89bb804d7e125d2368be6
6
+ metadata.gz: 40ab45814d16df9f6e54cf752f7592ed3e011c80400053752e7d43734e13477a8060feb1f6e2dadd7eda856715be8a3274ca8efe94deac0187d7f56a52420271
7
+ data.tar.gz: b15d057641b2ab958e701daeed9b4572776c192e2af34c2462e377f0a0e845daf382ad9b25dce6180726b5219eac2221daf09cbdc45010b5dcfd6c37fda5a7bb
data/.travis.yml CHANGED
@@ -6,7 +6,7 @@ rvm:
6
6
  # - 2.1.0
7
7
  # - ruby-head
8
8
  - jruby-19mode
9
- - jruby-head
9
+ # - jruby-head
10
10
 
11
11
  notifications:
12
12
  email:
data/README.md CHANGED
@@ -40,7 +40,10 @@ def index
40
40
  selector = RansackMongo::Query.parse(params[:q])
41
41
 
42
42
  # Mongo Ruby Driver
43
- @customers = db.customers.find(selector)
43
+ @customers = db['customers'].find(selector)
44
+
45
+ # Moped
46
+ @customers = session[:customers].find(selector)
44
47
 
45
48
  # Mongoid
46
49
  @customers = Customer.where(selector)
@@ -53,6 +56,8 @@ end
53
56
  * not_eq
54
57
  * cont
55
58
  * in
59
+ * start
60
+ * mstart
56
61
  * gt
57
62
  * lt
58
63
  * gteq
@@ -1,6 +1,6 @@
1
1
  module RansackMongo
2
2
  class MongoAdapter
3
- PREDICATES = %w[eq not_eq cont in gt lt gteq lteq]
3
+ PREDICATES = %w[eq not_eq cont in start mstart gt lt gteq lteq]
4
4
 
5
5
  def initialize
6
6
  @query = {}
@@ -26,6 +26,20 @@ module RansackMongo
26
26
  @query[attr] = { '$in' => value }
27
27
  end
28
28
 
29
+ def start_matcher(attr, value)
30
+ @query[attr] = { '$in' => [/^#{value}/] }
31
+ end
32
+
33
+ def mstart_matcher(attr, value)
34
+ values = value.split(",").map do |current|
35
+ if (current = current.strip).length > 0
36
+ /^#{current}/i
37
+ end
38
+ end.compact
39
+
40
+ @query[attr] = { '$in' => values }
41
+ end
42
+
29
43
  def gt_matcher(attr, value)
30
44
  append_sizeable_matcher('$gt', attr, value)
31
45
  end
@@ -5,7 +5,7 @@ module RansackMongo
5
5
  end
6
6
 
7
7
  def parse(params)
8
- params.keys.inject({}) do |query, query_param|
8
+ (params || {}).keys.inject({}) do |query, query_param|
9
9
  attr = query_param.to_s
10
10
  p, attr = detect_and_strip_from_string(attr)
11
11
 
@@ -1,3 +1,3 @@
1
1
  module RansackMongo
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -34,6 +34,34 @@ module RansackMongo
34
34
  end
35
35
  end
36
36
 
37
+ describe '#start' do
38
+ it 'returns the matcher' do
39
+ subject.start_matcher('object_ref', 'Bruno')
40
+
41
+ expect(subject.to_query).to eq("object_ref" => { "$in" => [/^Bruno/] })
42
+ end
43
+ end
44
+
45
+ describe '#mstart' do
46
+ it 'returns the matcher' do
47
+ subject.mstart_matcher('name', 'Pablo, Bruno,Dude')
48
+
49
+ expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/i, /^Bruno/i, /^Dude/i] })
50
+ end
51
+
52
+ it 'cleans up the input' do
53
+ subject.mstart_matcher('name', ',, , ,Pablo,,,, ,, , , ,')
54
+
55
+ expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/i] })
56
+ end
57
+
58
+ it 'accepts single values' do
59
+ subject.mstart_matcher('name', 'Pablo')
60
+
61
+ expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/i] })
62
+ end
63
+ end
64
+
37
65
  context 'when combine gt lt gteq and lteq' do
38
66
  it 'returns all matchers' do
39
67
  subject.gt_matcher('count', '1')
@@ -19,7 +19,7 @@ module RansackMongo
19
19
  end
20
20
  end
21
21
 
22
- describe '#parse' do
22
+ describe '.parse' do
23
23
  context 'when not implement matcher' do
24
24
  it 'raises proper exception' do
25
25
  params = { 'name_foo' => 'Pablo' }
@@ -32,7 +32,7 @@ module RansackMongo
32
32
  end
33
33
 
34
34
  context 'when MongoAdapter' do
35
- describe '#parse!' do
35
+ describe '.parse!' do
36
36
  it 'raises exception when query evaluates to an empty hash' do
37
37
  params = { 'name' => 'Pablo' }
38
38
  expect { described_class.parse!(params) }.to raise_error(MatcherNotFound)
@@ -45,13 +45,19 @@ module RansackMongo
45
45
  end
46
46
  end
47
47
 
48
- describe '#parse' do
48
+ describe '.parse' do
49
49
  it 'returns the query' do
50
50
  params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
51
51
 
52
52
  expect(described_class.parse(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
53
53
  end
54
54
 
55
+ context 'when nil query' do
56
+ it 'returns an empty query' do
57
+ expect(described_class.parse(nil)).to eq({})
58
+ end
59
+ end
60
+
55
61
  context 'when or' do
56
62
  it 'returns the query' do
57
63
  params = { 'name_or_fullname_eq' => 'Pablo' }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack_mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Cantero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-11 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler