-
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.
Add search page module and remove controller extension
The controller methods were available on every page in Alchemy and was listening to the query param. This approach is not really extendable, if you need multiple parameter to process the search result. The main logic was moved into a SearchPage module which has nearly the same structure as before, but is now way more flexible, because it is only used in this particular page layout. It also allows the application to extend the search and the view without monkey patching the gem. The only downside is, that it isn't possible anymore to add a search string the searchresults - element to test the search in the admin interface.
- Loading branch information
1 parent
7418988
commit b73f2cd
Showing
12 changed files
with
109 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<% @search_results = Alchemy::Search::SearchPage.perform_search(params, ability: current_ability) %> | ||
<%= render_search_form %> | ||
<%= render_elements %> | ||
<%= render_search_results %> |
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 |
---|---|---|
|
@@ -9,7 +9,3 @@ | |
- name: search | ||
searchresults: true | ||
unique: true | ||
elements: | ||
- searchresults | ||
autogenerate: | ||
- searchresults |
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,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 |