Commit bef5ab56 by Pablo Cantero

Fix lt gt comparison with Date & Time

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