Skip to content

Commit

Permalink
Allow search autocomplete to be easily disabled
Browse files Browse the repository at this point in the history
Autocomplete uses a denylist to stop certain terms from serving
suggestions. The process to update the denylist is very manual and
developer heavy.

It might be difficult to update the denylist over the holiday period. In
that situation the decision might be taken to temporarily turn off
autocomplete entirely.

An environment variable is used to determine whether or not to render
search with autocomplete. The value of this variable will be applied
globally across all applications that use site search.

This environment variable will either exist, or not. The value of it
doesn't really matter. If it exists, then search with autocomplete
should be disabled. A value of "1" is used in the tests, as that is the
default value being set in the helm charts.

See alphagov/govuk-helm-charts#2832
  • Loading branch information
leenagupte committed Dec 9, 2024
1 parent 6eaed0f commit afb7239
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/finders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ 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 @@ -386,4 +387,8 @@ 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
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_with_autocomplete", {
<%= render "govuk_publishing_components/components/#{@search_component}", {
id: "finder-keyword-search",
name: "keywords",
type: 'search',
Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/finders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@
expect(response.status).to eq(200)
expect(response).to render_template("finders/show_all_content_finder")
end

describe "search autocomplete" do
it "renders the search autocomplete when enabled" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: nil do
get :show, params: { slug: "search/all", keywords: "hello" }

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

it "does not render the autocomplete when disabled" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: "1" do
get :show, params: { slug: "search/all", keywords: "hello" }

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

Expand Down

0 comments on commit afb7239

Please sign in to comment.