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
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