-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from sascha-karnatz/search-page-module
Add search page module and remove controller extension
- Loading branch information
Showing
9 changed files
with
116 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
module Search | ||
module SearchPage | ||
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? | ||
search_results | ||
end | ||
|
||
def self.paginate_per | ||
Alchemy::PgSearch.config[:paginate_per] | ||
end | ||
|
||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
<div class="search_results"> | ||
<% if @search_results.blank? %> | ||
<% if search_results.blank? %> | ||
<h2 class="no_search_results"> | ||
<%= raw Alchemy.t('search_result_page.no_results', query: h(params[:query])) %> | ||
</h2> | ||
<% else %> | ||
<h2 class="search_results_heading"> | ||
<%= raw Alchemy.t("search_result_page.result_heading", query: h(params[:query])) %> | ||
<%= Alchemy.t("search_result_page.result_count", | ||
count: @search_results.try(:total_count) || @search_results.size) %> | ||
count: search_results.try(:total_count) || search_results.size) %> | ||
</h2> | ||
<ul class="search_result_list"> | ||
<%= render(partial: 'alchemy/search/result', collection: @search_results) %> | ||
<%= render(partial: 'alchemy/search/result', collection: search_results) %> | ||
</ul> | ||
<% end %> | ||
</div> | ||
|
||
<% if @search_results.try(:total_pages) %> | ||
<%= paginate @search_results %> | ||
<% if search_results.try(:total_pages) %> | ||
<%= paginate search_results %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
<%= render_search_results %> | ||
<%= element_view_for(searchresults) do |el| -%> | ||
<% search_results = Alchemy::Search::SearchPage.perform_search(params, ability: current_ability) %> | ||
<%= render "alchemy/search/results", search_results: search_results %> | ||
<% end %> |
2 changes: 0 additions & 2 deletions
2
spec/dummy/app/views/alchemy/elements/_searchresults_editor.html.erb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Alchemy::Search::SearchPage do | ||
let(:paginate_per) { 10 } | ||
before do | ||
Alchemy::PgSearch.config = { | ||
paginate_per:, | ||
} | ||
end | ||
|
||
context "#perform_search" do | ||
let(:query) {"foo"} | ||
let(:params) { {query:}} | ||
subject { described_class.perform_search(params, ability: nil) } | ||
|
||
it "calls the Alchemy.search_class" do | ||
expect(Alchemy.search_class).to receive(:search).with(query, ability: nil) | ||
subject | ||
end | ||
|
||
context "pagination" do | ||
let(:query) {"page"} | ||
|
||
before do | ||
12.times do | ||
create(:alchemy_page, :public) | ||
end | ||
Alchemy.search_class.rebuild | ||
end | ||
|
||
it "response with 10 documents" do | ||
expect(subject.length).to eq(10) | ||
end | ||
|
||
context "without pagination" do | ||
let(:paginate_per) { nil } | ||
|
||
it "response with all documents" do | ||
expect(subject.length).to eq(12) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context '#paginate_per' do | ||
subject { described_class.paginate_per } | ||
|
||
it 'should be 10 if no configuration is set' do | ||
expect(subject).to eq(10) | ||
end | ||
|
||
context "with configuration" do | ||
let(:paginate_per) { 50 } | ||
|
||
it 'should be 50 if no configuration is set' do | ||
expect(subject).to eq(50) | ||
end | ||
end | ||
end | ||
end |