class ActiveAdmin::Mongoid::Adaptor::Search
Attributes
base[R]
query[R]
query_hash[R]
search_params[R]
Public Class Methods
new(object, search_params = {}, per_page = 30, page = 1)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 7 def initialize(object, search_params = {}, per_page = 30, page = 1) @base = object @search_params = search_params @query_hash = get_query_hash(search_params) vpage = page.to_i > 0 ? page.to_i : 1 @query = @base.where(@query_hash).limit(per_page).skip(per_page * (vpage - 1)) end
Public Instance Methods
method_missing(method_id, *args, &block)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 19 def method_missing(method_id, *args, &block) if is_query(method_id) @search_params[method_id.to_s] else @query.send(method_id, *args, &block) end end
respond_to?(method_id)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 15 def respond_to?(method_id) @query.send(:respond_to?, method_id) end
Private Instance Methods
get_attribute(k, postfix)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 61 def get_attribute(k, postfix) k.match(/^(.*)#{postfix}$/)[1] end
get_query_hash(search_params)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 33 def get_query_hash(search_params) searches = search_params.map do|k, v| mongoidify_search(k,v) end Hash[searches] end
is_query(method_id)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 29 def is_query(method_id) method_id.to_s =~ /_(contains|eq|in|gt|lt|gte|lte)$/ end
mongoidify_search(k, v)
click to toggle source
# File lib/active_admin/mongoid/adaptor.rb, line 40 def mongoidify_search(k, v) case k when /_contains$/ [get_attribute(k, '_contains'), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)] when /_eq$/ [get_attribute(k, '_eq'), v] when /_in$/ [get_attribute(k, '_in').to_sym.in, v] when /_gt$/ [get_attribute(k, "_gt").to_sym.gt, v] when /_lt$/ [get_attribute(k, "_lt").to_sym.lt, v] when /_gte$/ [get_attribute(k, "_gte").to_sym.gte, v] when /_lte$/ [get_attribute(k, "_lte").to_sym.lte, v] else [k, v] end end