Class: RailsConnector::DefaultSearchController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/rails_connector/default_search_controller.rb

Overview

This class provides a default controller implementation for searching. It should be customized by subclassing.

Direct Known Subclasses

SearchController

Instance Method Summary collapse

Instance Method Details

#searchObject

Fetches search hits and paginates them. In case of an error, flashes appropriate error messages.

For use in views, hits are stored in the @hits variable. Pagination is done using the limit option (defaults to 10). You can change that limit by subclassing DefaultSearchController and then overwriting to CustomSearchController.options = {:limit => X}.

To customize the pagination, you should subclass DefaultSearchController:

class SearchController < RailsConnector::DefaultSearchController
  def search
    # What this method should do:
    #  * Initialize a SearchRequest obj
    #  * Paginate the results
    #  * Fill the @hits variable for your views
    #  * Flash on errors
  end
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/rails_connector/default_search_controller.rb', line 32

def search
  unless (@query = params[:q]).blank?
    @hits = WillPaginate::Collection.create(current_page, options[:limit]) do |pager|
      result = SearchRequest.new(@query, options.merge(:offset => pager.offset)).fetch_hits
      pager.replace(result)
      pager.total_entries = result.total_hits
    end
  else
    flash.now[:errors] = I18n.t(:"rails_connector.controllers.search.specify_query")
  end
rescue SES::SearchError => e
  logger.error(e)
  flash.now[:errors] = I18n.t(:"rails_connector.controllers.search.try_another_key")
rescue Errno::ECONNREFUSED, Errno::EAFNOSUPPORT
  flash.now[:errors] = I18n.t(:"rails_connector.controllers.search.search_disabled")
end