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|
op_name = { 'gteq' => 'gte', 'lteq' => 'lte' }[m] || m
describe "##{m}_matcher" do
it 'returns the 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 }) expect(subject.to_query).to eq('quantity' => { "$#{op_name}" => 1 })
end
end end
describe '#lt_matcher' do it 'accepts time' do
it 'returns the matcher' do updated_at = Time.now
subject.lt_matcher('quantity', 1) subject.send "#{m}_matcher", 'updated_at', updated_at
expect(subject.to_query).to eq('quantity' => { '$lt' => 1 }) expect(subject.to_query).to eq('updated_at' => { "$#{op_name}" => updated_at })
end
end end
describe '#gteq_matcher' do it 'accepts time as a string' do
it 'returns the matcher' do updated_at = '2014-10-11 14:48:07 -0300'
subject.gteq_matcher('quantity', 1)
expect(subject.to_query).to eq('quantity' => { '$gte' => 1 }) subject.send "#{m}_matcher", 'updated_at', updated_at
end
expect(subject.to_query).to eq('updated_at' => { "$#{op_name}" => updated_at })
end end
describe '#lteq_matcher' do it 'accepts date' do
it 'returns the matcher' do updated_at = Date.new
subject.lteq_matcher('quantity', 1)
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