From 97a610797758f3b82f2cbe5620b31a7646e2cccb Mon Sep 17 00:00:00 2001 From: Helen Pickavance Date: Wed, 22 Jan 2025 14:53:04 +0000 Subject: [PATCH] Update assignees list to include the current user at the top of the list with the '(You)' suffix --- app/presenters/filtered_editions_presenter.rb | 23 ++++++++---- .../filtered_editions_presenter_test.rb | 35 ++++++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/app/presenters/filtered_editions_presenter.rb b/app/presenters/filtered_editions_presenter.rb index 5068f3d74..e87b025fc 100644 --- a/app/presenters/filtered_editions_presenter.rb +++ b/app/presenters/filtered_editions_presenter.rb @@ -44,15 +44,13 @@ def edition_states def assignees users = [{ text: "All assignees", value: "" }] + users << create_assignee_list_item(@user) available_users.map do |user| - users << if user.id.to_s == @assigned_to_filter - { text: user.name, value: user.id, selected: "true" } - else - { text: user.name, value: user.id } - end - end + next if user == @user + users << create_assignee_list_item(user) + end users end @@ -62,6 +60,19 @@ def editions private + def create_assignee_list_item(user) + user_name = if user == @user + "#{user.name} (You)" + else + user.name + end + if user.id.to_s == @assigned_to_filter + { text: user_name, value: user.id, selected: "true" } + else + { text: user_name, value: user.id } + end + end + def query_editions result = editions_by_content_type result = apply_states_filter(result) diff --git a/test/unit/presenters/filtered_editions_presenter_test.rb b/test/unit/presenters/filtered_editions_presenter_test.rb index da6cf9c3d..1f2f4da82 100644 --- a/test/unit/presenters/filtered_editions_presenter_test.rb +++ b/test/unit/presenters/filtered_editions_presenter_test.rb @@ -225,6 +225,24 @@ def a_gds_user assert_includes(assignees, { text: "All assignees", value: "" }) end + should "return the current user after the 'All assignees' item" do + current_user = FactoryBot.create(:user, name: "River Tam", id: 123) + assignees = FilteredEditionsPresenter.new(current_user).assignees + + assert_equal("#{current_user.name} (You)", assignees[1][:text]) + assert_equal(current_user.id, assignees[1][:value]) + end + + should "only return the current user once" do + current_user = FactoryBot.create(:user, name: "River Tam", id: 123) + bob = FactoryBot.create(:user, name: "Bob") + assignees = FilteredEditionsPresenter.new(current_user).assignees + + assert_equal(1, assignees.count { |user| user[:text] == "#{current_user.name} (You)" }) + assert_equal(bob.name, assignees[2][:text]) + assert_not_includes assignees.map { |user| user[:text] }, current_user.name + end + should "return users who are not the assignee as unselected" do anna = FactoryBot.create(:user, name: "Anna") assignees = FilteredEditionsPresenter.new(a_gds_user).assignees @@ -232,7 +250,14 @@ def a_gds_user assert_includes(assignees, { text: anna.name, value: anna.id }) end - should "return the assignee as selected" do + should "return the assignee as selected (when the assignee is the current user)" do + a_user = a_gds_user + assignees = FilteredEditionsPresenter.new(a_user, assigned_to_filter: a_user.id.to_s).assignees + + assert_includes(assignees, { text: "#{a_user.name} (You)", value: a_user.id, selected: "true" }) + end + + should "return the assignee as selected (when the assignee is not the current user)" do anna = FactoryBot.create(:user, name: "Anna") assignees = FilteredEditionsPresenter.new(a_gds_user, assigned_to_filter: anna.id.to_s).assignees @@ -246,9 +271,10 @@ def a_gds_user assignees = FilteredEditionsPresenter.new(a_gds_user).assignees - assert_equal(anna.name, assignees[1][:text]) - assert_equal(bob.name, assignees[2][:text]) - assert_equal(charlie.name, assignees[3][:text]) + # First two items are 'All assignees' and the current user + assert_equal(anna.name, assignees[2][:text]) + assert_equal(bob.name, assignees[3][:text]) + assert_equal(charlie.name, assignees[4][:text]) end should "not include disabled users" do @@ -259,7 +285,6 @@ def a_gds_user assert_equal(2, users.count) assert_equal("All assignees", users[0][:text]) - assert_equal(a_user.name, users[1][:text]) assert_not_includes users.map { |user| user[:text] }, disabled_user.name end end