Skip to content

Commit

Permalink
Reduce duplication in FindersController & facet_collection
Browse files Browse the repository at this point in the history
The facet_collection partial only ever uses the filterable facets, so we
can avoid some duplication by selecting the filterable facets in
FindersController#facets. However, there are a few callsites within the
controller which use the full list of facets, so I've introduced a new
`#all_facets` method to handle that without making it available as a
view helper method.
  • Loading branch information
floehopper authored and andysellick committed Nov 29, 2023
1 parent 13fd220 commit 108e3ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 10 additions & 4 deletions app/controllers/finders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def render_component(partial, locals)
def result_set_presenter
@result_set_presenter ||= ResultSetPresenter.new(
content_item,
facets,
all_facets,
results,
filter_params,
sort_presenter,
Expand All @@ -106,12 +106,18 @@ def results
@results ||= ResultSetParser.parse(search_results)
end

def all_facets
return @facets if defined?(@facets)

FacetsBuilder.new(content_item:, search_results:, value_hash: filter_params).facets
end

def facets
@facets ||= FacetsBuilder.new(content_item:, search_results:, value_hash: filter_params).facets
all_facets.select(&:filterable?)
end

def signup_links
@signup_links ||= SignupLinksPresenter.new(content_item, facets, keywords).signup_links
@signup_links ||= SignupLinksPresenter.new(content_item, all_facets, keywords).signup_links
end

def initialize_search_query(is_for_feed: false)
Expand Down Expand Up @@ -174,7 +180,7 @@ def keywords

def facet_tags
@facet_tags ||= FacetTagsPresenter.new(
facets.select(&:filterable?),
facets,
sort_presenter,
i_am_a_topic_page_finder:,
)
Expand Down
6 changes: 3 additions & 3 deletions app/views/finders/_facet_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<% if content_item.all_content_finder? %>
<%= render partial: 'filter_button'%>
<% elsif !content_item.all_content_finder? %>
<% if facets.select(&:filterable?).any? %>
<% if facets.any? %>
<%= render partial: 'filter_button'%>
<% end %>
<% end %>

<% if facets.select(&:filterable?).any? %>
<% if facets.any? %>
<div data-module="gem-track-click" data-track-category="filterClicked"
data-track-action="skip-Link" data-track-label="">
<%= render "govuk_publishing_components/components/skip_link", {
Expand All @@ -46,7 +46,7 @@
</div>
</div>
<div class="facets__content" data-module="ga4-event-tracker" data-ga4-filter-container>
<% facets.select(&:filterable?).each.with_index do |facet, index| %>
<% facets.each.with_index do |facet, index| %>
<%= render partial: facet, object: facet, locals: { index: } %>
<% end %>
<div class="facets__tags-block js-mobile-facet-tag-block" data-module="remove-filter">
Expand Down

0 comments on commit 108e3ef

Please sign in to comment.