Class: RansackMongo::Predicate

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

Instance Method Summary collapse

Constructor Details

#initialize(predicates) ⇒ Predicate

Returns a new instance of Predicate.



3
4
5
# File 'lib/ransack_mongo/predicate.rb', line 3

def initialize(predicates)
  @predicates = predicates
end

Instance Method Details

#detect_and_strip_from_string(str) ⇒ Object



29
30
31
32
33
# File 'lib/ransack_mongo/predicate.rb', line 29

def detect_and_strip_from_string(str)
  if p = detect_from_string(str)
    [p, str.sub(/_#{p}$/, '')]
  end
end

#detect_from_string(str) ⇒ Object



25
26
27
# File 'lib/ransack_mongo/predicate.rb', line 25

def detect_from_string(str)
  names_by_decreasing_length.detect { |p| str.end_with?("_#{p}") }
end

#names_by_decreasing_lengthObject



21
22
23
# File 'lib/ransack_mongo/predicate.rb', line 21

def names_by_decreasing_length
  @predicates.sort { |a, b| b.length <=> a.length }
end

#parse(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ransack_mongo/predicate.rb', line 7

def parse(params)
  (params || {}).keys.inject({}) do |query, query_param|
    attr = query_param.to_s
    p, attr = detect_and_strip_from_string(attr)

    if p && attr
      query[p] ||= []
      query[p] << { 'attr' => attr, 'value' => params[query_param] }
    end

    query
  end
end