Class: RansackMongo::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/ransack_mongo/query.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(db_adapter_class = MongoAdapter) ⇒ Query

Returns a new instance of Query.



6
7
8
# File 'lib/ransack_mongo/query.rb', line 6

def initialize(db_adapter_class = MongoAdapter)
  @db_adapter_class = db_adapter_class
end

Instance Method Details

#or_query(db_adapter, attr, value, p) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ransack_mongo/query.rb', line 37

def or_query(db_adapter, attr, value, p)
  db_adapter.or_op do
    attr.split('_or_').each do |or_attr|
      db_adapter.send("#{p}_matcher", or_attr, value)
    end
  end
end

#to_query(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ransack_mongo/query.rb', line 10

def to_query(params)
  parsed_predicates = Predicate.new(@db_adapter_class.predicates).parse(params)

  db_adapter = @db_adapter_class.new

  parsed_predicates.keys.each do |p|
    parsed_predicates[p].each do |parsed_predicate|
      attr  = db_adapter.sanitize(parsed_predicate['attr'])
      value = db_adapter.sanitize(parsed_predicate['value'])

      begin
        if attr.include? '_or_'
          # attr => name_or_lastname
          or_query(db_adapter, attr, value, p)
        else
          # attr => name
          db_adapter.send("#{p}_matcher", attr, value)
        end
      rescue NoMethodError => e
        raise MatcherNotFound, "The matcher #{p} `#{p}_matcher` was not found in the #{@db_adapter_class.name}. Check `#{@db_adapter_class}.predicates`"
      end
    end
  end

  db_adapter.to_query
end