Commit 0ec5b7ac by Bruno Buccolo

Adds #start and #mstart matchers

parent c5847b7f
......@@ -56,6 +56,8 @@ end
* not_eq
* cont
* in
* start
* mstart
* gt
* lt
* gteq
......
module RansackMongo
class MongoAdapter
PREDICATES = %w[eq not_eq cont in gt lt gteq lteq]
PREDICATES = %w[eq not_eq cont in start mstart gt lt gteq lteq]
def initialize
@query = {}
......@@ -26,6 +26,20 @@ module RansackMongo
@query[attr] = { '$in' => value }
end
def start_matcher(attr, value)
@query[attr] = { '$in' => [/^#{value}/] }
end
def mstart_matcher(attr, value)
values = value.split(",").map do |current|
if (current = current.strip).length > 0
/^#{current}/
end
end.compact
@query[attr] = { '$in' => values }
end
def gt_matcher(attr, value)
append_sizeable_matcher('$gt', attr, value)
end
......
......@@ -34,6 +34,34 @@ module RansackMongo
end
end
describe '#start' do
it 'returns the matcher' do
subject.start_matcher('object_ref', 'Bruno')
expect(subject.to_query).to eq("object_ref" => { "$in" => [/^Bruno/] })
end
end
describe '#mstart' do
it 'returns the matcher' do
subject.mstart_matcher('name', 'Pablo, Bruno,Dude')
expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/, /^Bruno/, /^Dude/] })
end
it 'cleans up the input' do
subject.mstart_matcher('name', ',, , ,Pablo,,,, ,, , , ,')
expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/] })
end
it 'accepts single values' do
subject.mstart_matcher('name', 'Pablo')
expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/] })
end
end
context 'when combine gt lt gteq and lteq' do
it 'returns all matchers' do
subject.gt_matcher('count', '1')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment