Skip to content

Commit

Permalink
Default the state filters on the publications page
Browse files Browse the repository at this point in the history
When visiting the root page, with no filters set, default the state
 filters so that only editions in the "pre-published" states are shown,
 as these are the editions that users of Mainstream Publisher mostly
 care about.

Uses the presence of the title filter as a proxy for whether any filters
 have been set, since that filter is always present when the
 'update filter' button is clicked (even if no title text has been
 supplied, in which case it will have an empty value).
  • Loading branch information
mtaylorgds committed Sep 6, 2024
1 parent 1ef0a0f commit 71e3faf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ class RootController < ApplicationController
archived
].freeze

DEFAULT_FILTER_STATES = %w[draft amends_needed in_review fact_check fact_check_received ready].freeze

def index
filter_params_hash = filter_params.to_h
states_filter_params = filter_params_hash[:states_filter]
sanitised_states_filter_params = states_filter_params&.select { |fp| PERMITTED_FILTER_STATES.include?(fp) }
if user_has_submitted_filters?

Check failure on line 23 in app/controllers/root_controller.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
sanitised_states_filter_params = states_filter_params&.select { |fp| PERMITTED_FILTER_STATES.include?(fp) }
else
sanitised_states_filter_params = DEFAULT_FILTER_STATES
end
assignee_filter = filter_params_hash[:assignee_filter]
format_filter = filter_params_hash[:format_filter]
title_filter = filter_params_hash[:title_filter]
Expand All @@ -33,6 +39,10 @@ def index

private

def user_has_submitted_filters?
filter_params.to_h[:title_filter]
end

def filter_params
params.permit(:page, :assignee_filter, :format_filter, :title_filter, states_filter: [])
end
Expand Down
22 changes: 21 additions & 1 deletion test/functional/root_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ class RootControllerTest < ActionController::TestCase
assert_template "root/index"
end

should "default the state filters when no filters are specified" do
get :index

# These filters should be 'checked'
assert_select "input.govuk-checkboxes__input[value='draft'][checked]"
assert_select "input.govuk-checkboxes__input[value='amends_needed'][checked]"
assert_select "input.govuk-checkboxes__input[value='in_review'][checked]"
assert_select "input.govuk-checkboxes__input[value='fact_check'][checked]"
assert_select "input.govuk-checkboxes__input[value='fact_check_received'][checked]"
assert_select "input.govuk-checkboxes__input[value='ready'][checked]"

# These filters should NOT be 'checked'
assert_select "input.govuk-checkboxes__input[value='scheduled_for_publishing']"
assert_select "input.govuk-checkboxes__input[value='scheduled_for_publishing'][checked]", false
assert_select "input.govuk-checkboxes__input[value='published']"
assert_select "input.govuk-checkboxes__input[value='published'][checked]", false
assert_select "input.govuk-checkboxes__input[value='archived']"
assert_select "input.govuk-checkboxes__input[value='archived'][checked]", false
end

should "filter publications by state" do
FactoryBot.create(:guide_edition, state: "draft")
FactoryBot.create(:guide_edition, state: "published")
Expand Down Expand Up @@ -59,7 +79,7 @@ class RootControllerTest < ActionController::TestCase
.with(has_entry(:states_filter, %w[draft]))
.returns(stub(editions: Kaminari.paginate_array([]).page(1), available_users: []))

get :index, params: { states_filter: %w[draft not_a_real_state] }
get :index, params: { title_filter: "", states_filter: %w[draft not_a_real_state] }
end

should "show the first page of publications when no page is specified" do
Expand Down

0 comments on commit 71e3faf

Please sign in to comment.