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).
This commit is contained in:
Javi Martín
2022-08-22 19:59:03 +02:00
parent 2af7e32415
commit 92941d3304
8 changed files with 65 additions and 7 deletions

View File

@@ -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") %>

View File

@@ -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

View File

@@ -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" %>
<table>

View File

@@ -1,7 +1,5 @@
class SDGManagement::Relations::IndexComponent < ApplicationComponent
include Header
delegate :valid_filters, :current_filter, to: :helpers
attr_reader :records
def initialize(records)

View File

@@ -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 %>

View File

@@ -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

View File

@@ -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

View File

@@ -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