Class: BrickLayer::Search

Inherits:
Object
  • Object
show all
Defined in:
app/models/brick_layer/search.rb

Constant Summary collapse

MAPPINGS =
{
  :use_array    => ["all","in","nin","near"],
  :use_boolean  => ["exists"],
  :use_integer  => ["gt","gte","lt","lte","size"]
}

Class Method Summary collapse

Class Method Details

.params_to_query(params_hash) ⇒ Object

Convert the passed in params to a mongoid compatible search query



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/brick_layer/search.rb', line 32

def params_to_query(params_hash)
  [:controller, :action, :format, :model].each { |a| params_hash.delete(a) }
  query_params = {}
  params_hash.each do |k,v|
    break_up_params = k.to_s.split("_")
    operator  = break_up_params.delete_at(-1)
    attribute = break_up_params.join("_")
    query_params[attribute.to_sym.send(operator)] = value_wrapper( operator, (v.respond_to?("gsub") ? v.gsub('"','') : v) )
  end
  
  return query_params
end

.perform_search(params, options = {}) ⇒ Object

Actually perform the search with a set of params and options on an object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/brick_layer/search.rb', line 10

def perform_search(params,options={})
  class_to_use = params[:model].blank? ? BrickLayer::DataSet : params[:model].gsub('"','').singularize.camelize.constantize
  
  if params[:q]
    fulltext_options = options[:ft_options].blank? ? {} : options[:ft_options]
    results = class_to_use.fulltext_search(params[:q],fulltext_options)
  elsif params[:queries]
    params[:queries].each do |param_set|
      query = params_to_query({param_set[0] => param_set[1]})
      query_set = results || class_to_use
      
      results = query_set.where(query)
    end
  else
    query = params_to_query(params)
    results = query.blank? ? [] : class_to_use.where(query)
  end
  
  return results
end