Skip to content

Commit

Permalink
Implement FiltersPresenter#reset_url
Browse files Browse the repository at this point in the history
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
  • Loading branch information
csutter committed Oct 10, 2024
1 parent 9b4ce1c commit e0548c5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/lib/url_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions app/presenters/filters_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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
18 changes: 18 additions & 0 deletions spec/lib/url_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 47 additions & 5 deletions spec/presenters/filters_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit e0548c5

Please sign in to comment.