Module: Alchemy::Search::SearchPage

Defined in:
app/services/alchemy/search/search_page.rb

Class Method Summary collapse

Class Method Details

.paginate_perObject



18
19
20
# File 'app/services/alchemy/search/search_page.rb', line 18

def self.paginate_per
  Alchemy::PgSearch.config[:paginate_per]
end

.perform_search(params, ability: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'app/services/alchemy/search/search_page.rb', line 6

def self.perform_search(params, ability: nil)
  search_results = Alchemy.search_class.search(params[:query], ability:)
  search_results = search_results&.page(params[:page])&.per(paginate_per) if paginate_per.present?

  # order the documents by searchable_created_at and use the ranking order as second order argument
  if params[:sort] == "date"
    search_results.order_values.unshift("pg_search_documents.searchable_created_at DESC")
  end

  search_results
end

.search_result_pageObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/alchemy/search/search_page.rb', line 22

def self.search_result_page
  @search_result_page ||= begin
    page_layouts = PageLayout.all.select do |page_layout|
      page_layout.key?(:searchresults) && page_layout[:searchresults].to_s.casecmp(true.to_s).zero?
    end

    if page_layouts.nil?
      raise "No searchresults page layout found. Please add page layout with `searchresults: true` into your `page_layouts.yml` file."
    end

    page = Page.published.find_by(
      page_layout: page_layouts.first["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