ransack_mongo 0.0.2 → 1.0.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 +4 -4
- data/README.md +13 -9
- data/lib/ransack_mongo/query.rb +34 -36
- data/lib/ransack_mongo/version.rb +1 -1
- data/spec/ransack_mongo/query_spec.rb +10 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53ef1016a5f5047576966e475de778a6e290576e
|
4
|
+
data.tar.gz: f48e2f0923334b94ae39607289b2122ef757df09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
###
|
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
|
-
|
79
|
+
RansackMongo::Query.parse(name_xpto: 'Pablo')
|
77
80
|
# => {}
|
78
|
-
|
79
|
-
|
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
|
data/lib/ransack_mongo/query.rb
CHANGED
@@ -3,51 +3,49 @@ module RansackMongo
|
|
3
3
|
|
4
4
|
# https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching
|
5
5
|
class Query
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
35
|
-
|
31
|
+
db_adapter.to_query
|
32
|
+
end
|
36
33
|
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
raise MatcherNotFound, "No matchers found. To allow empty queries use .to_query instead"
|
39
|
+
selector
|
42
40
|
end
|
43
41
|
|
44
|
-
|
45
|
-
end
|
42
|
+
private
|
46
43
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
@@ -10,7 +10,7 @@ module RansackMongo
|
|
10
10
|
@query = {}
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
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 '#
|
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.
|
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 '#
|
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.
|
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.
|
44
|
+
expect(described_class.parse!(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
describe '#
|
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.
|
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.
|
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.
|
65
|
+
expect(described_class.parse(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }], 'country' => 'Brazil')
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|