| Class | SearchController |
| In: |
app/controllers/search_controller.rb
|
| Parent: | ApplicationController |
Provides the actions for searching the database and formatting the results.
At the moment this just calls to_s on whatever is returned as a search result but ideally this should be checked to match against the search query so that whatever is being typed matches closely with the description of the item.
e.g. typing "dugong" currently autocompletes to "Dinungkwulangwa" which isn‘t right.
# File app/controllers/search_controller.rb, line 16
16: def auto_complete_for_search_query
17: @results = System.multi_search(params[:search][:query] + '*', @@SEARCH_MODELS)
18: render :inline => "<%= auto_complete_result(@results, 'to_s') %>"
19: end
# File app/controllers/search_controller.rb, line 6 6: def index 7: search 8: render :action => 'search' 9: end
Uses paginating_find plugin to search ferret index and match the search query. Expects query to be found in params[:search][:query], optionally the models to be searched may be specified in params[:search][:models], default is to search all models specified in @@SEARCH_MODELS.
# File app/controllers/search_controller.rb, line 28
28: def results
29: @query = params[:search][:query] rescue ""
30: params[:page] ||= 1 # set default page
31:
32: # Check if models have been specified
33: if (params[:search][:models] rescue false)
34: # When search is from another controller the search models are specified as strings
35: # but we want model classes
36: models = params[:search][:models].map { |model|
37: model.is_a?( String ) ? model.constantize : model }
38: else
39: # use all searchable models
40: models = @@SEARCH_MODELS
41: end
42:
43: # Perform search with ferret
44: @results = System.paginating_ferret_multi_search(
45: { :query => @query,
46: :models => models,
47: :page_size => 20,
48: :current => params[:page]
49: } )
50:
51: # Count results for printing in view
52: unless @results.empty?
53: @counts = []
54: models.each do |model|
55: count = model.total_hits(@query)
56: # format each count to fit into a sentence
57: @counts << count.to_s + " " + model.to_s.humanize.downcase.pluralize(count.to_i) unless count == 0
58: end
59: else
60: @counts = ["Nothing"]
61: end
62: end