Skip to content

Commit

Permalink
Merge pull request #58 from sascha-karnatz/search-page-module
Browse files Browse the repository at this point in the history
Add search page module and remove controller extension
  • Loading branch information
tvdeyen authored Oct 28, 2024
2 parents de265fe + e5dfcdb commit 21049ae
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 144 deletions.
29 changes: 7 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,9 @@ In order to render the search results, you'll need a page layout that represents
page layout as `searchresults: true`. The search form will pick this page as result page.

#### Search Results Page

```yaml
# page_layouts.yml
- name: search
searchresults: true
unique: true
```

Tip: For maximum flexibility you could also add an element that represents the search results. This lets your editors to
place additional elements (maybe a header image or additional text blocks) on the search result page.

Add a search layout to the `page_layout.yml` and mark it with a `searchresults` flag. These flag is used to find the
correct page path for search form.

```yaml
# page_layouts.yml
Expand All @@ -159,24 +152,19 @@ place additional elements (maybe a header image or additional text blocks) on th
# elements.yml
- name: searchresults
unique: true
ingredients:
- role: search_string
hint: The is only for presentational purposes in the Alchemy preview
default: lorem
type: Text
```

and then use the view helpers to render the search form on the page layout partial and the search results on the element
view partial.
and then use the view helpers to render the search form on the page layout partial.

```erb
<!-- app/views/alchemy/page_layouts/_search.html.erb -->
<%= render_elements %>
<!-- app/views/alchemy/elements/_searchresults.html.erb -->
<%= element_view_for(searchresults) do |el| -%>
<%= render_search_results %>
<%- end -%>
<% search_results = Alchemy::Search::SearchPage.perform_search(params, ability: current_ability) %>
<%= render "alchemy/search/results", search_results: search_results %>
<% end %>
```

### View Helpers
Expand All @@ -186,9 +174,6 @@ This gem provides some helper methods that let you render the form and the searc
* Render the search form:
`render_search_form`

* Render the search results:
`render_search_results`

### Customize Views

If you want to override the search form and search result views please use this generator.
Expand Down
96 changes: 0 additions & 96 deletions app/controller/alchemy/pg_search/controller_methods.rb

This file was deleted.

16 changes: 2 additions & 14 deletions app/helpers/alchemy/pg_search/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,8 @@ def render_search_form(options = {})
class: "fulltext_search",
id: "search",
}
render "alchemy/search/form", options: default_options.merge(options), search_result_page: search_result_page
end

# Renders the search results partial within +app/views/alchemy/search/_results.html+
#
# @option options show_result_count [Boolean] (true) Should the count of results be displayed or not?
# @option options show_heading [Boolean] (true) Should the heading be displayed or not?
#
def render_search_results(options = {})
default_options = {
show_result_count: true,
show_heading: true,
}
render "alchemy/search/results", options: default_options.merge(options)
search_result_page = Alchemy::Search::SearchPage.search_result_page
render "alchemy/search/form", options: default_options.merge(options), search_result_page:
end

def highlighted_excerpt(text, phrase, radius = 50)
Expand Down
38 changes: 38 additions & 0 deletions app/services/alchemy/search/search_page.rb
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
10 changes: 5 additions & 5 deletions app/views/alchemy/search/_results.html.erb
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 %>
4 changes: 0 additions & 4 deletions lib/alchemy/pg_search/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class Engine < ::Rails::Engine
require_dependency(c)
end

# We need to have the search methods present in all Alchemy controllers
Alchemy::PagesController.send(:include, Alchemy::PgSearch::ControllerMethods)
Alchemy::Admin::PagesController.send(:include, Alchemy::PgSearch::ControllerMethods)

# In development environment, this runs on every code reload, so avoid multiple reindexing jobs
unless Alchemy.publish_targets.map(&:name).include? 'Alchemy::PgSearch::IndexPageJob'
# reindex the page after it was published
Expand Down
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 %>

This file was deleted.

60 changes: 60 additions & 0 deletions spec/services/alchemy/search/search_page_spec.rb
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

0 comments on commit 21049ae

Please sign in to comment.