From d10c45772e7a05e052c8b8747480ee79dfc260af Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Thu, 8 May 2025 10:20:57 +0200 Subject: [PATCH] Add a clear command to clear filters --- app/models/command/clear_filters.rb | 20 ++++++++++++++++++++ app/models/command/parser.rb | 2 ++ app/views/commands/_help.html.erb | 2 ++ test/models/command/clear_filters_test.rb | 15 +++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 app/models/command/clear_filters.rb create mode 100644 test/models/command/clear_filters_test.rb diff --git a/app/models/command/clear_filters.rb b/app/models/command/clear_filters.rb new file mode 100644 index 000000000..90685d00e --- /dev/null +++ b/app/models/command/clear_filters.rb @@ -0,0 +1,20 @@ +class Command::ClearFilters < Command + store_accessor :data, :params + + def title + "Clear filters" + end + + def execute + redirect_to cards_path(**params.to_h.with_indifferent_access.without(*filters_to_clear)) + end + + private + FILTERS_TO_KEEP = [ :collection_ids, :indexed_by ] + + def filters_to_clear + Filter::Params::PERMITTED_PARAMS + .flat_map { |param| param.is_a?(Hash) ? param.keys : param } + .without(*FILTERS_TO_KEEP) + end +end diff --git a/app/models/command/parser.rb b/app/models/command/parser.rb index ea330420a..c2c98fcb7 100644 --- a/app/models/command/parser.rb +++ b/app/models/command/parser.rb @@ -25,6 +25,8 @@ class Command::Parser Command::GoToUser.new(user_id: assignee_from(command_name)&.id) when "/assign", "/assignto" Command::Assign.new(assignee_ids: assignees_from(command_arguments).collect(&:id), card_ids: cards.ids) + when "/clear" + Command::ClearFilters.new(params: filter.as_params) when "/close" Command::Close.new(card_ids: cards.ids, reason: command_arguments.join(" ")) when "/tag" diff --git a/app/views/commands/_help.html.erb b/app/views/commands/_help.html.erb index 356083278..8c18abcf3 100644 --- a/app/views/commands/_help.html.erb +++ b/app/views/commands/_help.html.erb @@ -14,6 +14,8 @@
Filter multiple cards by their IDs
search_term
Search for cards containing the term
+
/clear
+
Clear all the filters

Cards

/assign [username 1] [username 2]...
diff --git a/test/models/command/clear_filters_test.rb b/test/models/command/clear_filters_test.rb new file mode 100644 index 000000000..aa5fe1a5f --- /dev/null +++ b/test/models/command/clear_filters_test.rb @@ -0,0 +1,15 @@ +require "test_helper" + +class Command::FilterTest < ActionDispatch::IntegrationTest + include CommandTestHelper + + setup do + Current.session = sessions(:david) + end + + test "clear the filters keeping the selected collections" do + result = execute_command "/clear", context_url: "?card_ids%5B%5D=1&card_ids%5B%5D=2&collection_ids%5B%5D=#{collections(:writebook).id}&indexed_by=newest&terms%5B%5D=jorge" + + assert_equal cards_path(indexed_by: "newest", collection_ids: [ collections(:writebook).id ]), result.url + end +end