Commit bef5ab56 by Pablo Cantero

Fix lt gt comparison with Date & Time

parent cd210689
......@@ -27,23 +27,19 @@ module RansackMongo
end
def gt_matcher(attr, value)
@query[attr] ||= {}
@query[attr]['$gt'] = value.to_f
append_sizeable_matcher('$gt', attr, value)
end
def lt_matcher(attr, value)
@query[attr] ||= {}
@query[attr]['$lt'] = value.to_f
append_sizeable_matcher('$lt', attr, value)
end
def gteq_matcher(attr, value)
@query[attr] ||= {}
@query[attr]['$gte'] = value.to_f
append_sizeable_matcher('$gte', attr, value)
end
def lteq_matcher(attr, value)
@query[attr] ||= {}
@query[attr]['$lte'] = value.to_f
append_sizeable_matcher('$lte', attr, value)
end
def or_op # or operation
......@@ -63,5 +59,21 @@ module RansackMongo
def self.predicates
PREDICATES
end
private
def append_sizeable_matcher(m, attr, value)
@query[attr] ||= {}
@query[attr][m] = parse_sizeable_value(value)
end
def parse_sizeable_value(value)
case value
when Date, Time
value
else
Float(value) rescue value
end
end
end
end
......@@ -48,35 +48,38 @@ module RansackMongo
end
end
describe '#gt_matcher' do
%w[gt lt gteq lteq].each do |m|
op_name = { 'gteq' => 'gte', 'lteq' => 'lte' }[m] || m
describe "##{m}_matcher" do
it 'returns the matcher' do
subject.gt_matcher('quantity', 1)
subject.send "#{m}_matcher", 'quantity', 1
expect(subject.to_query).to eq('quantity' => { '$gt' => 1 })
end
expect(subject.to_query).to eq('quantity' => { "$#{op_name}" => 1 })
end
describe '#lt_matcher' do
it 'returns the matcher' do
subject.lt_matcher('quantity', 1)
it 'accepts time' do
updated_at = Time.now
subject.send "#{m}_matcher", 'updated_at', updated_at
expect(subject.to_query).to eq('quantity' => { '$lt' => 1 })
end
expect(subject.to_query).to eq('updated_at' => { "$#{op_name}" => updated_at })
end
describe '#gteq_matcher' do
it 'returns the matcher' do
subject.gteq_matcher('quantity', 1)
it 'accepts time as a string' do
updated_at = '2014-10-11 14:48:07 -0300'
expect(subject.to_query).to eq('quantity' => { '$gte' => 1 })
end
subject.send "#{m}_matcher", 'updated_at', updated_at
expect(subject.to_query).to eq('updated_at' => { "$#{op_name}" => updated_at })
end
describe '#lteq_matcher' do
it 'returns the matcher' do
subject.lteq_matcher('quantity', 1)
it 'accepts date' do
updated_at = Date.new
expect(subject.to_query).to eq('quantity' => { '$lte' => 1 })
subject.send "#{m}_matcher", 'updated_at', updated_at
expect(subject.to_query).to eq('updated_at' => { "$#{op_name}" => updated_at })
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