Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autocomplete to search index #3604

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/controllers/finders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def show_page_variables
@sort_presenter = sort_presenter
@pagination = pagination_presenter
@spelling_suggestion_presenter = spelling_suggestion_presenter
@search_component = use_autocomplete? ? "search_with_autocomplete" : "search"
end

private
Expand Down Expand Up @@ -387,8 +386,4 @@ def filter_query_array(arr)
arr.reject { |v| v == "all" }.compact.presence
end
end

def use_autocomplete?
true unless ENV["GOVUK_DISABLE_SEARCH_AUTOCOMPLETE"]
end
end
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ module ApplicationHelper
def absolute_url_for(path)
URI.join(Plek.new.website_root, path)
end

def search_component
use_autocomplete = true unless ENV["GOVUK_DISABLE_SEARCH_AUTOCOMPLETE"]

use_autocomplete ? "search_with_autocomplete" : "search"
end
end
2 changes: 1 addition & 1 deletion app/views/finders/show_all_content_finder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<div id="keywords" role="search" aria-label="Sitewide">
<%= render "govuk_publishing_components/components/#{@search_component}", {
<%= render "govuk_publishing_components/components/#{search_component}", {
id: "finder-keyword-search",
name: "keywords",
type: 'search',
Expand Down
4 changes: 3 additions & 1 deletion app/views/search/_search_field.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
} %>
<% end %>

<%= render "govuk_publishing_components/components/search", {
<%= render "govuk_publishing_components/components/#{search_component}", {
inline_label: false,
label_text: label_text,
size: "large",
id: "search-main",
disable_corrections: true,
source_url: api_search_autocomplete_url(format: :json),
source_key: "suggestions",
} %>

<%= hidden_field_tag("filter_manual[]", params[:filter_manual]) if params[:filter_manual] %>
Expand Down
61 changes: 61 additions & 0 deletions spec/controllers/search_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "spec_helper"
require "gds_api/test_helpers/content_store"

describe SearchController, type: :controller do
include GdsApi::TestHelpers::ContentStore

before do
stub_content_store_has_item("/search")
end

describe "GET index" do
render_views

it "renders the search page" do
get :index
expect(response.status).to eq(200)
expect(response).to render_template("search/no_search_term")
end

context "when given search parameters" do
it "redirects to the all content finder" do
get :index, params: { q: "my search" }

destination = finder_path("search/all",
params: { keywords: "my search", order: "relevance" })
expect(response).to redirect_to(destination)
end
end

context "when JSON is requested" do
it "redirects to the all content finder" do
get :index, format: :json

destination = finder_path("search/all",
params: { order: "relevance", format: "json" })
expect(response).to redirect_to(destination)
end
end

context "when GOVUK_DISABLE_SEARCH_AUTOCOMPLETE is not set" do
it "renders the search autocomplete component" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: nil do
get :index

expect(response.body).to include("gem-c-search-with-autocomplete")
end
end
end

context "when GOVUK_DISABLE_SEARCH_AUTOCOMPLETE is set" do
it "renders the search component instead of the autocomplete component" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: "1" do
get :index

expect(response.body).not_to include("gem-c-search-with-autocomplete")
expect(response.body).to include("gem-c-search")
end
end
end
end
end
21 changes: 21 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "spec_helper"

RSpec.describe ApplicationHelper, type: :helper do
describe "#search_component" do
context "when ENV['GOVUK_DISABLE_SEARCH_AUTOCOMPLETE'] is not set" do
it "returns 'search_with_autocomplete'" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: nil do
expect(helper.search_component).to eq("search_with_autocomplete")
end
end
end

context "when ENV['GOVUK_DISABLE_SEARCH_AUTOCOMPLETE'] is set" do
it "returns 'search'" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: "1" do
expect(helper.search_component).to eq("search")
end
end
end
end
end
Loading