Commit 45aeeecd by Pablo Cantero

Simple interface

parent ac410e78
......@@ -28,15 +28,16 @@ Or install it yourself as:
```ruby
# GET /customers?q[name_eq]=Pablo&q[middle_name_or_last_name_cont]=Cantero
# params[:q]
# => { name_eq: 'Pablo', middle_name_or_last_name_cont: 'Cantero' }
# query.to_query(params[:q])
# RansackMongo::Query.parse(params[:q])
# => { name: 'Pablo', '$or' => { middle_name: /Cantero/i, last_name: /Cantero/i } }
# GET /customers
def index
query = RansackMongo::Query.new
selector = query.to_query(params[:q])
selector = RansackMongo::Query.parse(params[:q])
# Mongo Ruby Driver
@customers = db.customers.find(selector)
......@@ -63,20 +64,23 @@ You can also combine predicates for OR queries.
```ruby
query_param = { name_eq: 'Pablo', middle_name_or_last_name_cont: 'Cantero' }
query.to_query(params[:q])
RansackMongo::Query.parse(params[:q])
# => { name: 'Pablo', '$or' => { middle_name: /Cantero/i, last_name: /Cantero/i } }
```
### to_query!
### parse!
You can use `parse!` for stricter validations. This method will raise an exception if a query cannot be produced.
You can use to_query! for stricter validations. This method will raise an exception if a query cannot be produced.
```ruby
# xpto isn't a valid predicate
query.to_query(name_xpto: 'Pablo')
RansackMongo::Query.parse(name_xpto: 'Pablo')
# => {}
query.to_query!(name_xpto: 'Pablo')
# => RansackMongo::MatcherNotFound: No matchers found. To allow empty queries use .to_query instead
RansackMongo::Query.parse!(name_xpto: 'Pablo')
# => RansackMongo::MatcherNotFound: No matchers found. To allow empty queries use .parse instead
```
## Contributing
......
......@@ -3,51 +3,49 @@ module RansackMongo
# https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching
class Query
def initialize(db_adapter_class = MongoAdapter)
@db_adapter_class = db_adapter_class
end
def to_query(params)
parsed_predicates = Predicate.new(@db_adapter_class.predicates).parse(params)
db_adapter = @db_adapter_class.new
parsed_predicates.keys.each do |p|
parsed_predicates[p].each do |parsed_predicate|
attr = parsed_predicate['attr']
value = parsed_predicate['value']
begin
if attr.include? '_or_'
# attr => name_or_lastname
or_query(db_adapter, attr, value, p)
else
# attr => name
db_adapter.send("#{p}_matcher", attr, value)
class << self
def parse(params, db_adapter_class = MongoAdapter)
parsed_predicates = Predicate.new(db_adapter_class.predicates).parse(params)
db_adapter = db_adapter_class.new
parsed_predicates.keys.each do |p|
parsed_predicates[p].each do |parsed_predicate|
attr = parsed_predicate['attr']
value = parsed_predicate['value']
begin
if attr.include? '_or_'
# attr => name_or_lastname
or_query(db_adapter, attr, value, p)
else
# attr => name
db_adapter.send("#{p}_matcher", attr, value)
end
rescue NoMethodError => e
raise MatcherNotFound, "The matcher #{p} `#{p}_matcher` was not found in the #{db_adapter_class.name}. Check `#{db_adapter_class}.predicates`"
end
rescue NoMethodError => e
raise MatcherNotFound, "The matcher #{p} `#{p}_matcher` was not found in the #{@db_adapter_class.name}. Check `#{@db_adapter_class}.predicates`"
end
end
end
db_adapter.to_query
end
db_adapter.to_query
end
def to_query!(params)
selector = to_query params
def parse!(params, db_adapter_class = MongoAdapter)
if (selector = parse(params)).empty?
raise MatcherNotFound, 'No matchers found. To allow empty queries use .parse instead'
end
if selector.empty?
raise MatcherNotFound, "No matchers found. To allow empty queries use .to_query instead"
selector
end
selector
end
private
def or_query(db_adapter, attr, value, p)
db_adapter.or_op do
attr.split('_or_').each do |or_attr|
db_adapter.send("#{p}_matcher", or_attr, value)
def or_query(db_adapter, attr, value, p)
db_adapter.or_op do
attr.split('_or_').each do |or_attr|
db_adapter.send("#{p}_matcher", or_attr, value)
end
end
end
end
......
......@@ -10,7 +10,7 @@ module RansackMongo
@query = {}
end
def to_query
def parse
@query
end
......@@ -19,12 +19,12 @@ module RansackMongo
end
end
describe '#to_query' do
describe '#parse' do
context 'when not implement matcher' do
it 'raises proper exception' do
params = { 'name_foo' => 'Pablo' }
expect {
described_class.new(FooAdapter).to_query(params)
described_class.parse(params, FooAdapter)
}.to raise_error(MatcherNotFound, 'The matcher foo `foo_matcher` was not found in the RansackMongo::FooAdapter. Check `RansackMongo::FooAdapter.predicates`')
end
end
......@@ -32,37 +32,37 @@ module RansackMongo
end
context 'when MongoAdapter' do
describe '#to_query!' do
describe '#parse!' do
it 'raises exception when query evaluates to an empty hash' do
params = { 'name' => 'Pablo' }
expect { described_class.new.to_query!(params) }.to raise_error(MatcherNotFound)
expect { described_class.parse!(params) }.to raise_error(MatcherNotFound)
end
it 'returns the query' do
params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
expect(described_class.new.to_query!(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
expect(described_class.parse!(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
end
end
describe '#to_query' do
describe '#parse' do
it 'returns the query' do
params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
expect(described_class.new.to_query(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
expect(described_class.parse(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
end
context 'when or' do
it 'returns the query' do
params = { 'name_or_fullname_eq' => 'Pablo' }
expect(described_class.new.to_query(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }])
expect(described_class.parse(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }])
end
it 'preserves other criterias' do
params = { 'name_or_fullname_eq' => 'Pablo', 'country_eq' => 'Brazil' }
expect(described_class.new.to_query(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }], 'country' => 'Brazil')
expect(described_class.parse(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }], 'country' => 'Brazil')
end
end
end
......
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