Skip to content

Commit

Permalink
Merge pull request #3517 from alphagov/dynamic-filter-text
Browse files Browse the repository at this point in the history
All content finder: Dynamic filter summary text
  • Loading branch information
csutter authored Oct 18, 2024
2 parents f182c72 + f555273 commit 5517184
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
26 changes: 26 additions & 0 deletions app/presenters/filters_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def any_filters?
applied_filters.any?
end

def summary_heading_text
"Active #{summary_phrase}"
end

def reset_link_text
"Clear all #{summary_phrase}"
end

def summary_items
applied_filters.map do |filter|
{
Expand Down Expand Up @@ -39,4 +47,22 @@ def all_filter_keys
.flat_map { |filter| filter[:query_params].keys }
.uniq
end

def summary_phrase
if active_sort? && active_non_sort_filters?
"filters and sorting"
elsif active_sort?
"sorting"
else
"filters"
end
end

def active_sort?
facets.any? { |facet| facet.key == SortFacet::KEY && facet.has_filters? }
end

def active_non_sort_filters?
facets.any? { |facet| facet.key != SortFacet::KEY && facet.has_filters? }
end
end
4 changes: 2 additions & 2 deletions app/views/finders/show_all_content_finder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@
<% if filters_presenter.any_filters? %>
<%= render "components/filter_summary", {
reset_link_href: filters_presenter.reset_url,
reset_link_text: "Clear all filters",
reset_link_text: filters_presenter.reset_link_text,
heading_level: 3,
heading_text: "Active filters",
heading_text: filters_presenter.summary_heading_text,
filters: filters_presenter.summary_items,
} %>
<% end %>
Expand Down
53 changes: 52 additions & 1 deletion spec/presenters/filters_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@
let(:facets) { [] }
let(:finder_url_builder) { instance_double(UrlBuilder) }

let(:facet_without_applied_filters) { double("Facet", has_filters?: false, applied_filters: []) }
let(:facet_without_applied_filters) do
double(
"Facet",
key: "facet_without_applied_filters",
has_filters?: false,
applied_filters: [],
)
end
let(:facet_with_applied_filters) do
double(
"Facet",
key: "facet_with_applied_filters",
has_filters?: true,
applied_filters: [{ name: "name", label: "label", query_params: { key: %w[value] } }],
)
end
let(:another_facet_with_applied_filters) do
double(
"Facet",
key: "another_facet_with_applied_filters",
has_filters?: true,
applied_filters: [
{ name: "name1", label: "label1", query_params: { key1: %w[value1] } },
Expand All @@ -28,6 +37,8 @@
],
)
end
let(:active_sort_facet) { double("SortFacet", key: "order", has_filters?: true) }
let(:inactive_sort_facet) { double("SortFacet", key: "order", has_filters?: false) }

describe "#any_filters" do
context "when there are no facets" do
Expand Down Expand Up @@ -113,4 +124,44 @@
end
end
end

describe "#summary_heading_text and #reset_link_text" do
context "when neither filters nor sorting are active" do
let(:facets) { [facet_without_applied_filters, inactive_sort_facet] }

it "returns default text" do
expect(filters_presenter.summary_heading_text).to eq("Active filters")
expect(filters_presenter.reset_link_text).to eq("Clear all filters")
end
end

context "when filters are active but sorting is not" do
let(:facets) { [facet_with_applied_filters, inactive_sort_facet] }

it "returns the expected summary heading text and reset link text" do
expect(filters_presenter.summary_heading_text).to eq("Active filters")
expect(filters_presenter.reset_link_text).to eq("Clear all filters")
end
end

context "when sorting is active but filters are not" do
let(:facets) { [facet_without_applied_filters, active_sort_facet] }

it "returns the expected summary heading text and reset link text" do
expect(filters_presenter.summary_heading_text).to eq("Active sorting")
expect(filters_presenter.reset_link_text).to eq("Clear all sorting")
end
end

context "when both filters and sorting are active" do
let(:facets) do
[facet_with_applied_filters, active_sort_facet]
end

it "returns the expected summary heading text and reset link text" do
expect(filters_presenter.summary_heading_text).to eq("Active filters and sorting")
expect(filters_presenter.reset_link_text).to eq("Clear all filters and sorting")
end
end
end
end

0 comments on commit 5517184

Please sign in to comment.