Class: Searchify::SearchifyController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/searchify/searchify_controller.rb

Instance Method Summary collapse

Instance Method Details

#searchObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/searchify/searchify_controller.rb', line 10

def search
  resource_class  = params[:collection].classify.constantize
  search_term     = params[:term]
  search_keyword  = extract_search_key(resource_class)
  search_strategy = params[:search_strategy] || :search_strategy

  collection = if resource_class.respond_to?(search_strategy)
    resource_class.send(search_strategy, search_term, searchify_scopes)
  else
    columns = Searchify::Config.column_names & resource_class.column_names

    scoped = resource_class.where( columns.map{ |c| "(#{c} #{search_keyword} :term)" }.join(' OR '), :term => "%#{search_term}%")

    searchify_scopes.each do |key, value|
      scoped = scoped.send(key, value) if resource_class.respond_to?(key)
    end

    scoped.limit(Searchify::Config.limit).map do |resource|
      {:label => resource.send(Searchify::Config.label_method), :id => resource.id}
    end
  end

  render :json => collection.to_json
end