Commit 2212ee4a by Hugo Maia Vieira

parse must deal with nil param

parent d9227a89
...@@ -5,7 +5,7 @@ module RansackMongo ...@@ -5,7 +5,7 @@ module RansackMongo
end end
def parse(params) def parse(params)
params.keys.inject({}) do |query, query_param| (params || {}).keys.inject({}) do |query, query_param|
attr = query_param.to_s attr = query_param.to_s
p, attr = detect_and_strip_from_string(attr) p, attr = detect_and_strip_from_string(attr)
......
...@@ -19,7 +19,7 @@ module RansackMongo ...@@ -19,7 +19,7 @@ module RansackMongo
end end
end end
describe '#parse' do describe '.parse' do
context 'when not implement matcher' do context 'when not implement matcher' do
it 'raises proper exception' do it 'raises proper exception' do
params = { 'name_foo' => 'Pablo' } params = { 'name_foo' => 'Pablo' }
...@@ -32,7 +32,7 @@ module RansackMongo ...@@ -32,7 +32,7 @@ module RansackMongo
end end
context 'when MongoAdapter' do context 'when MongoAdapter' do
describe '#parse!' do describe '.parse!' do
it 'raises exception when query evaluates to an empty hash' do it 'raises exception when query evaluates to an empty hash' do
params = { 'name' => 'Pablo' } params = { 'name' => 'Pablo' }
expect { described_class.parse!(params) }.to raise_error(MatcherNotFound) expect { described_class.parse!(params) }.to raise_error(MatcherNotFound)
...@@ -45,13 +45,19 @@ module RansackMongo ...@@ -45,13 +45,19 @@ module RansackMongo
end end
end end
describe '#parse' do describe '.parse' do
it 'returns the query' do it 'returns the query' do
params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' } params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
expect(described_class.parse(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i) expect(described_class.parse(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
end end
context 'when nil query' do
it 'return an empty query' do
expect(described_class.parse(nil)).to eq({})
end
end
context 'when or' do context 'when or' do
it 'returns the query' do it 'returns the query' do
params = { 'name_or_fullname_eq' => 'Pablo' } params = { 'name_or_fullname_eq' => 'Pablo' }
......
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