ransack_mongo 0.0.2 → 1.0.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: 653de3c263786edc9d472e925e4c2802b9d8723e
4
- data.tar.gz: 6bc873650e072552a2582dca3fdc4f3a6606c5d0
3
+ metadata.gz: 53ef1016a5f5047576966e475de778a6e290576e
4
+ data.tar.gz: f48e2f0923334b94ae39607289b2122ef757df09
5
5
  SHA512:
6
- metadata.gz: add84c5b3298b51116656d07c7d541fa6733a1b0dc568caf6a15297921edc2342042a32408dfb797b526ddb8a80c9b2e246106a5ae9f028109e6d5a404303edf
7
- data.tar.gz: 23e2806cc27103853ccf12a53d224ac93cc319c47f09e675556493e140947f4f3e91914fabf864c4f49eb4d553dd34328aa8904b759553fa1799088796f4661c
6
+ metadata.gz: 08cc605b686091b44734ef6f5e1ed2d6501edc387db67dfd8ccc8985691feb7de64af042b5cf9ea7b23fbe1814f3e0d667359837641b9e22996a41e1c542be74
7
+ data.tar.gz: 184cc91865c21b2e339a8d508645282f0a4de53e62bfa2d9e5979277207cce252972d2538fa93714ef67038293e5ea5dc4e442ef09b89bb804d7e125d2368be6
data/README.md CHANGED
@@ -28,15 +28,16 @@ Or install it yourself as:
28
28
 
29
29
  ```ruby
30
30
  # GET /customers?q[name_eq]=Pablo&q[middle_name_or_last_name_cont]=Cantero
31
+
31
32
  # params[:q]
32
33
  # => { name_eq: 'Pablo', middle_name_or_last_name_cont: 'Cantero' }
33
- # query.to_query(params[:q])
34
+
35
+ # RansackMongo::Query.parse(params[:q])
34
36
  # => { name: 'Pablo', '$or' => { middle_name: /Cantero/i, last_name: /Cantero/i } }
35
37
 
36
38
  # GET /customers
37
39
  def index
38
- query = RansackMongo::Query.new
39
- selector = query.to_query(params[:q])
40
+ selector = RansackMongo::Query.parse(params[:q])
40
41
 
41
42
  # Mongo Ruby Driver
42
43
  @customers = db.customers.find(selector)
@@ -63,20 +64,23 @@ You can also combine predicates for OR queries.
63
64
 
64
65
  ```ruby
65
66
  query_param = { name_eq: 'Pablo', middle_name_or_last_name_cont: 'Cantero' }
66
- query.to_query(params[:q])
67
+
68
+ RansackMongo::Query.parse(params[:q])
67
69
  # => { name: 'Pablo', '$or' => { middle_name: /Cantero/i, last_name: /Cantero/i } }
68
70
  ```
69
71
 
70
- ### to_query!
72
+ ### parse!
73
+
74
+ You can use `parse!` for stricter validations. This method will raise an exception if a query cannot be produced.
71
75
 
72
- You can use to_query! for stricter validations. This method will raise an exception if a query cannot be produced.
73
76
  ```ruby
74
77
  # xpto isn't a valid predicate
75
78
 
76
- query.to_query(name_xpto: 'Pablo')
79
+ RansackMongo::Query.parse(name_xpto: 'Pablo')
77
80
  # => {}
78
- query.to_query!(name_xpto: 'Pablo')
79
- # => RansackMongo::MatcherNotFound: No matchers found. To allow empty queries use .to_query instead
81
+
82
+ RansackMongo::Query.parse!(name_xpto: 'Pablo')
83
+ # => RansackMongo::MatcherNotFound: No matchers found. To allow empty queries use .parse instead
80
84
  ```
81
85
 
82
86
  ## Contributing
@@ -3,51 +3,49 @@ module RansackMongo
3
3
 
4
4
  # https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching
5
5
  class Query
6
- def initialize(db_adapter_class = MongoAdapter)
7
- @db_adapter_class = db_adapter_class
8
- end
9
-
10
- def to_query(params)
11
- parsed_predicates = Predicate.new(@db_adapter_class.predicates).parse(params)
12
-
13
- db_adapter = @db_adapter_class.new
14
-
15
- parsed_predicates.keys.each do |p|
16
- parsed_predicates[p].each do |parsed_predicate|
17
- attr = parsed_predicate['attr']
18
- value = parsed_predicate['value']
19
-
20
- begin
21
- if attr.include? '_or_'
22
- # attr => name_or_lastname
23
- or_query(db_adapter, attr, value, p)
24
- else
25
- # attr => name
26
- db_adapter.send("#{p}_matcher", attr, value)
6
+ class << self
7
+ def parse(params, db_adapter_class = MongoAdapter)
8
+ parsed_predicates = Predicate.new(db_adapter_class.predicates).parse(params)
9
+
10
+ db_adapter = db_adapter_class.new
11
+
12
+ parsed_predicates.keys.each do |p|
13
+ parsed_predicates[p].each do |parsed_predicate|
14
+ attr = parsed_predicate['attr']
15
+ value = parsed_predicate['value']
16
+
17
+ begin
18
+ if attr.include? '_or_'
19
+ # attr => name_or_lastname
20
+ or_query(db_adapter, attr, value, p)
21
+ else
22
+ # attr => name
23
+ db_adapter.send("#{p}_matcher", attr, value)
24
+ end
25
+ rescue NoMethodError => e
26
+ raise MatcherNotFound, "The matcher #{p} `#{p}_matcher` was not found in the #{db_adapter_class.name}. Check `#{db_adapter_class}.predicates`"
27
27
  end
28
- rescue NoMethodError => e
29
- raise MatcherNotFound, "The matcher #{p} `#{p}_matcher` was not found in the #{@db_adapter_class.name}. Check `#{@db_adapter_class}.predicates`"
30
28
  end
31
29
  end
32
- end
33
30
 
34
- db_adapter.to_query
35
- end
31
+ db_adapter.to_query
32
+ end
36
33
 
37
- def to_query!(params)
38
- selector = to_query params
34
+ def parse!(params, db_adapter_class = MongoAdapter)
35
+ if (selector = parse(params)).empty?
36
+ raise MatcherNotFound, 'No matchers found. To allow empty queries use .parse instead'
37
+ end
39
38
 
40
- if selector.empty?
41
- raise MatcherNotFound, "No matchers found. To allow empty queries use .to_query instead"
39
+ selector
42
40
  end
43
41
 
44
- selector
45
- end
42
+ private
46
43
 
47
- def or_query(db_adapter, attr, value, p)
48
- db_adapter.or_op do
49
- attr.split('_or_').each do |or_attr|
50
- db_adapter.send("#{p}_matcher", or_attr, value)
44
+ def or_query(db_adapter, attr, value, p)
45
+ db_adapter.or_op do
46
+ attr.split('_or_').each do |or_attr|
47
+ db_adapter.send("#{p}_matcher", or_attr, value)
48
+ end
51
49
  end
52
50
  end
53
51
  end
@@ -1,3 +1,3 @@
1
1
  module RansackMongo
2
- VERSION = '0.0.2'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -10,7 +10,7 @@ module RansackMongo
10
10
  @query = {}
11
11
  end
12
12
 
13
- def to_query
13
+ def parse
14
14
  @query
15
15
  end
16
16
 
@@ -19,12 +19,12 @@ module RansackMongo
19
19
  end
20
20
  end
21
21
 
22
- describe '#to_query' 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' }
26
26
  expect {
27
- described_class.new(FooAdapter).to_query(params)
27
+ described_class.parse(params, FooAdapter)
28
28
  }.to raise_error(MatcherNotFound, 'The matcher foo `foo_matcher` was not found in the RansackMongo::FooAdapter. Check `RansackMongo::FooAdapter.predicates`')
29
29
  end
30
30
  end
@@ -32,37 +32,37 @@ module RansackMongo
32
32
  end
33
33
 
34
34
  context 'when MongoAdapter' do
35
- describe '#to_query!' do
35
+ describe '#parse!' do
36
36
  it 'raises exception when query evaluates to an empty hash' do
37
37
  params = { 'name' => 'Pablo' }
38
- expect { described_class.new.to_query!(params) }.to raise_error(MatcherNotFound)
38
+ expect { described_class.parse!(params) }.to raise_error(MatcherNotFound)
39
39
  end
40
40
 
41
41
  it 'returns the query' do
42
42
  params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
43
43
 
44
- expect(described_class.new.to_query!(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
44
+ expect(described_class.parse!(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
45
45
  end
46
46
  end
47
47
 
48
- describe '#to_query' do
48
+ describe '#parse' do
49
49
  it 'returns the query' do
50
50
  params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
51
51
 
52
- expect(described_class.new.to_query(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
52
+ expect(described_class.parse(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
53
53
  end
54
54
 
55
55
  context 'when or' do
56
56
  it 'returns the query' do
57
57
  params = { 'name_or_fullname_eq' => 'Pablo' }
58
58
 
59
- expect(described_class.new.to_query(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }])
59
+ expect(described_class.parse(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }])
60
60
  end
61
61
 
62
62
  it 'preserves other criterias' do
63
63
  params = { 'name_or_fullname_eq' => 'Pablo', 'country_eq' => 'Brazil' }
64
64
 
65
- expect(described_class.new.to_query(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }], 'country' => 'Brazil')
65
+ expect(described_class.parse(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }], 'country' => 'Brazil')
66
66
  end
67
67
  end
68
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack_mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Cantero