added more predication methods.
This commit is contained in:
parent
59920feb59
commit
b6574263f6
|
@ -1,3 +1,5 @@
|
|||
require 'ransack/adapters/mongoid/attributes/predications'
|
||||
|
||||
module Ransack
|
||||
module Adapters
|
||||
module Mongoid
|
||||
|
@ -9,44 +11,14 @@ module Ransack
|
|||
# include Arel::OrderPredications
|
||||
# include Arel::Math
|
||||
|
||||
include ::Ransack::Adapters::Mongoid::Attributes::Predications
|
||||
|
||||
###
|
||||
# Create a node for lowering this attribute
|
||||
def lower
|
||||
relation.lower self
|
||||
end
|
||||
|
||||
def eq(other)
|
||||
{ name => other }.to_inquiry
|
||||
end
|
||||
|
||||
def not_eq(other)
|
||||
{ name.to_sym.ne => other }.to_inquiry
|
||||
end
|
||||
|
||||
def matches(other)
|
||||
{ name => /#{Regexp.escape(other)}/i }.to_inquiry
|
||||
end
|
||||
|
||||
def does_not_match(other)
|
||||
{ "$not" => { name => /#{Regexp.escape(other)}/i } }.to_inquiry
|
||||
end
|
||||
|
||||
def not_eq_all(other)
|
||||
q = []
|
||||
other.each do |value|
|
||||
q << { name.to_sym.ne => value }
|
||||
end
|
||||
{ "$and" => q }.to_inquiry
|
||||
end
|
||||
|
||||
def eq_any(other)
|
||||
q = []
|
||||
other.each do |value|
|
||||
q << { name => value }
|
||||
end
|
||||
{ "$or" => q }.to_inquiry
|
||||
end
|
||||
|
||||
def asc
|
||||
{ name => :asc }
|
||||
end
|
||||
|
|
|
@ -0,0 +1,102 @@
|
|||
module Ransack
|
||||
module Adapters
|
||||
module Mongoid
|
||||
module Attributes
|
||||
module Predications
|
||||
|
||||
def eq(other)
|
||||
{ name => other }.to_inquiry
|
||||
end
|
||||
|
||||
def not_eq(other)
|
||||
{ name => { '$ne' => other } }.to_inquiry
|
||||
end
|
||||
|
||||
def matches(other)
|
||||
{ name => /#{Regexp.escape(other)}/i }.to_inquiry
|
||||
end
|
||||
|
||||
def does_not_match(other)
|
||||
{ "$not" => { name => /#{Regexp.escape(other)}/i } }.to_inquiry
|
||||
end
|
||||
|
||||
def eq_all(others)
|
||||
grouping_all :eq, others
|
||||
end
|
||||
|
||||
def not_eq_all(others)
|
||||
grouping_all :not_eq, others
|
||||
end
|
||||
|
||||
def eq_any(others)
|
||||
grouping_any :eq, others
|
||||
end
|
||||
|
||||
def not_eq_any(others)
|
||||
grouping_any :not_eq, others
|
||||
end
|
||||
|
||||
def gteq right
|
||||
{ name => { '$gte' => right } }.to_inquiry
|
||||
end
|
||||
|
||||
def gteq_any others
|
||||
grouping_any :gteq, others
|
||||
end
|
||||
|
||||
def gteq_all others
|
||||
grouping_all :gteq, others
|
||||
end
|
||||
|
||||
def gt right
|
||||
{ name => { '$gt' => right } }
|
||||
end
|
||||
|
||||
def gt_any others
|
||||
grouping_any :gt, others
|
||||
end
|
||||
|
||||
def gt_all others
|
||||
grouping_all :gt, others
|
||||
end
|
||||
|
||||
def lt right
|
||||
{ name => { '$lt' => right } }
|
||||
end
|
||||
|
||||
def lt_any others
|
||||
grouping_any :lt, others
|
||||
end
|
||||
|
||||
def lt_all others
|
||||
grouping_all :lt, others
|
||||
end
|
||||
|
||||
def lteq right
|
||||
{ name => { '$lte' => right } }.to_inquiry
|
||||
end
|
||||
|
||||
def lteq_any others
|
||||
grouping_any :lteq, others
|
||||
end
|
||||
|
||||
def lteq_all others
|
||||
grouping_all :lteq, others
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def grouping_any method_id, others
|
||||
nodes = others.map { |e| send(method_id, e) }
|
||||
{ "$or" => nodes }.to_inquiry
|
||||
end
|
||||
|
||||
def grouping_all method_id, others
|
||||
nodes = others.map { |e| send(method_id, e) }
|
||||
{ "$and" => nodes }.to_inquiry
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -37,7 +37,6 @@ module Ransack
|
|||
end
|
||||
|
||||
describe 'cont' do
|
||||
|
||||
it_has_behavior 'wildcard escaping', :name_cont, { 'name' => /%\._\\/i } do
|
||||
subject { @s }
|
||||
end
|
||||
|
@ -106,5 +105,37 @@ module Ransack
|
|||
expect(@s.result.selector).to eq({ '$and' => [ { 'name' => { '$ne' => nil}}, { 'name' => { '$ne' => '' }} ] })
|
||||
end
|
||||
end
|
||||
|
||||
describe 'gt' do
|
||||
it 'generates an greater than for time' do
|
||||
time = Time.now
|
||||
@s.created_at_gt = time
|
||||
expect(@s.result.selector).to eq({ "created_at" => { '$gt' => time } })
|
||||
end
|
||||
end
|
||||
|
||||
describe 'lt' do
|
||||
it 'generates an greater than for time' do
|
||||
time = Time.now
|
||||
@s.created_at_lt = time
|
||||
expect(@s.result.selector).to eq({ "created_at" => { '$lt' => time } })
|
||||
end
|
||||
end
|
||||
|
||||
describe 'gteq' do
|
||||
it 'generates an greater than for time' do
|
||||
time = Time.now
|
||||
@s.created_at_gteq = time
|
||||
expect(@s.result.selector).to eq({ "created_at" => { '$gte' => time } })
|
||||
end
|
||||
end
|
||||
|
||||
describe 'lteq' do
|
||||
it 'generates an greater than for time' do
|
||||
time = Time.now
|
||||
@s.created_at_lteq = time
|
||||
expect(@s.result.selector).to eq({ "created_at" => { '$lte' => time } })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue