From 92941d3304d0dbd01192dd737ce5481fde4a5760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 22 Aug 2022 19:59:03 +0200 Subject: [PATCH] Keep filters in admin search While this bug was already present in the general admin search, the combination of both search and filters was very uncommon. I've only found this combinations in the users section, where you've got the "erased" filter, but in this case searching for erased users doesn't really make sense since their username and email have been deleted and so there's nothing to find. So the hidden content seemed to be the only affected section. However, we're adding the field to every section so we don't have to make sure we add it when we need it (like we did in the SDGManagement section). --- .../admin/search_component.html.erb | 1 + app/components/admin/search_component.rb | 10 ++++++ .../relations/index_component.html.erb | 2 +- .../relations/index_component.rb | 2 -- .../relations/search_component.html.erb | 1 - .../relations/search_component.rb | 5 ++- .../components/admin/search_component_spec.rb | 31 +++++++++++++++++++ .../admin/hidden_content_search_spec.rb | 20 ++++++++++++ 8 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 spec/components/admin/search_component_spec.rb diff --git a/app/components/admin/search_component.html.erb b/app/components/admin/search_component.html.erb index 0286dcb48..4658f2971 100644 --- a/app/components/admin/search_component.html.erb +++ b/app/components/admin/search_component.html.erb @@ -1,4 +1,5 @@ <%= form_tag(url, options) do |f| %> + <%= hidden_current_filter_tag %> <%= text_field_tag :search, search_terms.to_s, placeholder: label, "aria-label": label %> <%= content %> <%= submit_tag t("admin.shared.search.search") %> diff --git a/app/components/admin/search_component.rb b/app/components/admin/search_component.rb index e7c4a3e45..6a75b029a 100644 --- a/app/components/admin/search_component.rb +++ b/app/components/admin/search_component.rb @@ -20,4 +20,14 @@ class Admin::SearchComponent < ApplicationComponent def options { method: :get, role: "search" }.merge(form_options) end + + def hidden_current_filter_tag + hidden_field_tag(:filter, current_filter) if current_filter + end + + def current_filter + if helpers.respond_to?(:current_filter) + helpers.current_filter + end + end end diff --git a/app/components/sdg_management/relations/index_component.html.erb b/app/components/sdg_management/relations/index_component.html.erb index 4dfd0cc5a..57127a51f 100644 --- a/app/components/sdg_management/relations/index_component.html.erb +++ b/app/components/sdg_management/relations/index_component.html.erb @@ -1,6 +1,6 @@ <%= header %> -<%= render SDGManagement::Relations::SearchComponent.new(label: search_label, current_filter: current_filter) %> +<%= render SDGManagement::Relations::SearchComponent.new(label: search_label) %> <%= render "shared/filter_subnav", i18n_namespace: "sdg_management.relations.index" %> diff --git a/app/components/sdg_management/relations/index_component.rb b/app/components/sdg_management/relations/index_component.rb index 0db934e13..55fdde345 100644 --- a/app/components/sdg_management/relations/index_component.rb +++ b/app/components/sdg_management/relations/index_component.rb @@ -1,7 +1,5 @@ class SDGManagement::Relations::IndexComponent < ApplicationComponent include Header - delegate :valid_filters, :current_filter, to: :helpers - attr_reader :records def initialize(records) diff --git a/app/components/sdg_management/relations/search_component.html.erb b/app/components/sdg_management/relations/search_component.html.erb index d77f21c8f..31282e747 100644 --- a/app/components/sdg_management/relations/search_component.html.erb +++ b/app/components/sdg_management/relations/search_component.html.erb @@ -5,5 +5,4 @@ <%= component.select_tag :target_code, target_options, include_blank: target_blank_option, "aria-label": target_label %> - <%= component.hidden_field_tag :filter, current_filter %> <% end %> diff --git a/app/components/sdg_management/relations/search_component.rb b/app/components/sdg_management/relations/search_component.rb index fdbe9c833..c48504424 100644 --- a/app/components/sdg_management/relations/search_component.rb +++ b/app/components/sdg_management/relations/search_component.rb @@ -1,10 +1,9 @@ class SDGManagement::Relations::SearchComponent < ApplicationComponent include SDG::OptionsForSelect - attr_reader :label, :current_filter + attr_reader :label - def initialize(label:, current_filter: nil) + def initialize(label:) @label = label - @current_filter = current_filter end private diff --git a/spec/components/admin/search_component_spec.rb b/spec/components/admin/search_component_spec.rb new file mode 100644 index 000000000..196735d52 --- /dev/null +++ b/spec/components/admin/search_component_spec.rb @@ -0,0 +1,31 @@ +require "rails_helper" + +describe Admin::SearchComponent do + describe "#hidden_current_filter_tag" do + context "controller responds to current_filter", controller: ApplicationController do + it "is present when the controller has a current filter" do + allow(controller).to receive(:current_filter).and_return("all") + + render_inline Admin::SearchComponent.new(label: "Search") + + expect(page).to have_field "filter", type: :hidden, with: "all" + end + + it "is not present when the controller has no current filter" do + render_inline Admin::SearchComponent.new(label: "Search") + + expect(page).not_to have_field "filter", type: :hidden + expect(page).not_to have_field "filter" + end + end + + context "controller does not respond to current_filter", controller: ActionController::Base do + it "is not present" do + render_inline Admin::SearchComponent.new(label: "Search") + + expect(page).not_to have_field "filter", type: :hidden + expect(page).not_to have_field "filter" + end + end + end +end diff --git a/spec/system/admin/hidden_content_search_spec.rb b/spec/system/admin/hidden_content_search_spec.rb index 5dd9e5b01..183f72ac8 100644 --- a/spec/system/admin/hidden_content_search_spec.rb +++ b/spec/system/admin/hidden_content_search_spec.rb @@ -102,4 +102,24 @@ describe "Hidden content search", :admin do expect(page).not_to have_content "alien1" expect(page).not_to have_content "alien2" end + + scenario "keeps filter parameters after searching" do + create(:user, :hidden, :with_confirmed_hide, username: "person1") + create(:user, :hidden, :with_confirmed_hide, username: "alien1") + create(:user, :hidden, username: "person2") + create(:user, :hidden, username: "alien2") + + visit admin_hidden_users_path(filter: "with_confirmed_hide") + + fill_in "search", with: "alien" + click_button "Search" + + expect(page).not_to have_content "person1" + expect(page).to have_content "alien1" + expect(page).not_to have_content "person2" + expect(page).not_to have_content "alien2" + + expect(page).to have_content "Confirmed" + expect(page).not_to have_link "Confirmed" + end end