module Alchemy::PgSearch::ControllerMethods
Provides full text search methods in your controller
Public Class Methods
included(controller)
click to toggle source
Adds a before_action
to your controller
# File lib/alchemy/pg_search/controller_methods.rb, line 10 def self.included(controller) controller.send(:before_action, :perform_search, only: :show) controller.send(:helper_method, :search_result_page) controller.send(:helper, Alchemy::PgSearch::SearchHelper) end
Private Instance Methods
paginate_per()
click to toggle source
# File lib/alchemy/pg_search/controller_methods.rb, line 87 def paginate_per Alchemy::PgSearch.config[:paginate_per] end
perform_search()
click to toggle source
Performs a full text search with PgSearch
.
Gets invoked everytime 'query' is given in params.
This method only sets the +@search_results+ instance variable.
You have to redirect to the search result page within a search form.
Alchemy
provides a handy helper for rendering the search form:¶ ↑
render_search_form
Note¶ ↑
If in preview mode a fake search value “lorem” will be set.
@see Alchemy::PagesHelper#render_search_form
# File lib/alchemy/pg_search/controller_methods.rb, line 36 def perform_search if self.class == Alchemy::Admin::PagesController && params[:query].blank? params[:query] = 'lorem' end return if params[:query].blank? @search_results = search_results if paginate_per @search_results = @search_results.page(params[:page]).per(paginate_per) end end
search_result_page()
click to toggle source
A view helper that loads the search result page.
@return [Alchemy::Page]
# File lib/alchemy/pg_search/controller_methods.rb, line 66 def search_result_page @search_result_page ||= begin page = Page.published.find_by( page_layout: search_result_page_layout['name'], language_id: Language.current.id ) if page.nil? logger.warn "\n++++++\nNo published search result page found. Please create one or publish your search result page.\n++++++\n" end page end end
search_result_page_layout()
click to toggle source
# File lib/alchemy/pg_search/controller_methods.rb, line 79 def search_result_page_layout page_layout = PageLayout.get_all_by_attributes(searchresults: true).first if page_layout.nil? raise "No searchresults page layout found. Please add page layout with `searchresults: true` into your `page_layouts.yml` file." end page_layout end
search_results()
click to toggle source
Find Pages that have what is provided in “query” param with PgSearch
@return [Array]
# File lib/alchemy/pg_search/controller_methods.rb, line 51 def search_results pages = Alchemy::PgSearch.config[:page_search_scope].pages # Since CanCan cannot (oh the irony) merge +accessible_by+ scope with pg_search scopes, # we need to fake a page object here if can? :show, Alchemy::Page.new(restricted: true, public_on: Date.current) pages.full_text_search(params[:query]) else pages.not_restricted.full_text_search(params[:query]) end end