From 50da28c0bed8b9bc3549ca8b1d3ff1e56909e5f2 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Wed, 7 May 2025 16:07:21 +0200 Subject: [PATCH] Add command to filter by tag --- app/models/command/filter_by_tag.rb | 18 ++++++++++++++++++ app/models/command/parser.rb | 7 +++++++ test/models/command/filter_by_tag_test.rb | 15 +++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 app/models/command/filter_by_tag.rb create mode 100644 test/models/command/filter_by_tag_test.rb diff --git a/app/models/command/filter_by_tag.rb b/app/models/command/filter_by_tag.rb new file mode 100644 index 000000000..8c6a49e74 --- /dev/null +++ b/app/models/command/filter_by_tag.rb @@ -0,0 +1,18 @@ +class Command::FilterByTag < Command + store_accessor :data, :tag_id, :params + + validates_presence_of :tag_id + + def title + "Filter by tag ##{tag&.title || tag_id} " + end + + def execute + redirect_to cards_path(**params.merge(tag_ids: [ tag_id ])) + end + + private + def tag + Tag.find_by_id(tag_id) + end +end diff --git a/app/models/command/parser.rb b/app/models/command/parser.rb index 830b6c93d..8fbd5ccc3 100644 --- a/app/models/command/parser.rb +++ b/app/models/command/parser.rb @@ -23,6 +23,8 @@ class Command::Parser Command::Assign.new(assignee_ids: assignees_from(command_arguments).collect(&:id), card_ids: cards.ids) when "/close" Command::Close.new(card_ids: cards.ids, reason: command_arguments.join(" ")) + when /^#/ + Command::FilterByTag.new(tag_id: tag_from(string).id, params: filter.as_params) when /^@/ Command::GoToUser.new(user_id: assignee_from(command_name)&.id) else @@ -44,6 +46,11 @@ class Command::Parser User.all.find { |user| user.mentionable_handles.include?(string_without_at) } end + def tag_from(string) + title = string.gsub(/^#/, "") + Tag.find_or_create_by!(title: title) + end + def parse_free_string(string) if cards = multiple_cards_from(string) Command::FilterCards.new(card_ids: cards.ids, params: filter.as_params) diff --git a/test/models/command/filter_by_tag_test.rb b/test/models/command/filter_by_tag_test.rb new file mode 100644 index 000000000..ef335a1d3 --- /dev/null +++ b/test/models/command/filter_by_tag_test.rb @@ -0,0 +1,15 @@ +require "test_helper" + +class Command::FilterByCardTest < ActionDispatch::IntegrationTest + include CommandTestHelper + + setup do + @tag = tags(:web) + end + + test "redirects to the cards index filtering by cards" do + result = execute_command "##{@tag.title}" + + assert_equal cards_path(indexed_by: "newest", tag_ids: [ @tag.id ]), result.url + end +end