class RansackMongo::Predicate

Public Class Methods

new(predicates) click to toggle source
# File lib/ransack_mongo/predicate.rb, line 3
def initialize(predicates)
  @predicates = predicates
end

Public Instance Methods

detect_and_strip_from_string(str) click to toggle source
# 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) click to toggle source
# 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_length() click to toggle source
# File lib/ransack_mongo/predicate.rb, line 21
def names_by_decreasing_length
  @predicates.sort { |a, b| b.length <=> a.length }
end
parse(params) click to toggle source
# 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