From e0548c59bbcc01f4b935bbe09b0d2af71d130034 Mon Sep 17 00:00:00 2001 From: Christian Sutter Date: Fri, 4 Oct 2024 12:29:47 +0000 Subject: [PATCH] Implement `FiltersPresenter#reset_url` This returns the URL used for the "Clear all" link on both the filter summary and filter panel in the new "all content" finder UI. - Add real implementation for `FiltersPresenter#reset_url`, merging the query parameters keys from every applied facet and then removing them from the current URL - Add `UrlBuilder#url_except_keys` to remove all given keys from the current finder URL's query parameters - Refactor duplication into a `#applied_filters` private method --- app/lib/url_builder.rb | 4 ++ app/presenters/filters_presenter.rb | 18 ++++++-- spec/lib/url_builder_spec.rb | 18 ++++++++ spec/presenters/filters_presenter_spec.rb | 52 ++++++++++++++++++++--- 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/app/lib/url_builder.rb b/app/lib/url_builder.rb index 64bda8e5a..aa75b170a 100644 --- a/app/lib/url_builder.rb +++ b/app/lib/url_builder.rb @@ -16,6 +16,10 @@ def url_except(excepted_param) build_url(query_params.deep_except(excepted_param)) end + def url_except_keys(keys) + build_url(query_params.except(*keys)) + end + private attr_reader :path, :query_params diff --git a/app/presenters/filters_presenter.rb b/app/presenters/filters_presenter.rb index 6be7fdd22..b74953ae4 100644 --- a/app/presenters/filters_presenter.rb +++ b/app/presenters/filters_presenter.rb @@ -5,11 +5,11 @@ def initialize(facets, finder_url_builder) end def any_filters? - facets.any?(&:has_filters?) + applied_filters.any? end def summary_items - facets.flat_map(&:applied_filters).map do |filter| + applied_filters.map do |filter| { label: filter[:name], value: filter[:label], @@ -20,10 +20,22 @@ def summary_items end def reset_url - "#" + return unless any_filters? + + finder_url_builder.url_except_keys(all_filter_keys) end private attr_reader :facets, :finder_url_builder + + def applied_filters + @applied_filters ||= facets.flat_map(&:applied_filters) + end + + def all_filter_keys + applied_filters + .flat_map { |filter| filter[:query_params].keys } + .uniq + end end diff --git a/spec/lib/url_builder_spec.rb b/spec/lib/url_builder_spec.rb index 7bfbe62b9..b3d751908 100644 --- a/spec/lib/url_builder_spec.rb +++ b/spec/lib/url_builder_spec.rb @@ -59,4 +59,22 @@ expect(url).to eq("/search/all?array%5B%5D=two&hash%5Ba%5D=1&hash%5Bc%5D=2&keywords=dumbledore") end end + + describe "#url_except_keys" do + subject(:url) { builder.url_except_keys(excepted_keys) } + + let(:query_params) do + { + keywords: "dumbledore", + hash: { a: 1, b: 2, c: 2 }, + array: %w[one two], + single_value: "value", + } + end + let(:excepted_keys) { %i[hash array single_value] } + + it "builds a URL without the excluded keys" do + expect(url).to eq("/search/all?keywords=dumbledore") + end + end end diff --git a/spec/presenters/filters_presenter_spec.rb b/spec/presenters/filters_presenter_spec.rb index f8385443e..004b41188 100644 --- a/spec/presenters/filters_presenter_spec.rb +++ b/spec/presenters/filters_presenter_spec.rb @@ -7,9 +7,27 @@ let(:finder_url_builder) { instance_double(UrlBuilder) } let(:facet_without_applied_filters) { double("Facet", has_filters?: false, applied_filters: []) } - let(:facet_with_applied_filters) { double("Facet", has_filters?: true, applied_filters:) } - - let(:applied_filters) { [{ name: "name", label: "label", query_params: { key: %w[value] } }] } + let(:facet_with_applied_filters) do + double( + "Facet", + has_filters?: true, + applied_filters: [{ name: "name", label: "label", query_params: { key: %w[value] } }], + ) + end + let(:another_facet_with_applied_filters) do + double( + "Facet", + has_filters?: true, + applied_filters: [ + { name: "name1", label: "label1", query_params: { key1: %w[value1] } }, + { name: "name2", + label: "label2", + query_params: { + key1: %w[anothervalue1], key2: { value2: "subvalue" } + } }, + ], + ) + end describe "#any_filters" do context "when there are no facets" do @@ -32,8 +50,32 @@ end describe "#reset_url" do - it "returns a static anchor link" do - expect(subject.reset_url).to eq("#") + subject(:reset_url) { filters_presenter.reset_url } + + context "when there are no facets" do + let(:facets) { [] } + + it { is_expected.to be_nil } + end + + context "when there are only facets without applied filters" do + let(:facets) { [facet_without_applied_filters, facet_without_applied_filters] } + + it { is_expected.to be_nil } + end + + context "when there are facets with applied filters" do + let(:facets) { [facet_with_applied_filters, another_facet_with_applied_filters] } + + before do + allow(finder_url_builder).to receive(:url_except_keys) + .with(containing_exactly(:key, :key1, :key2)) + .and_return("/search/foo") + end + + it "returns the expected reset URL" do + expect(reset_url).to eq("/search/foo") + end end end