From 52b31ab19327a4c27e98f99ef5b89732a7784ba3 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Wed, 27 Aug 2025 15:45:09 +0900 Subject: [PATCH 2/2] Show the 'Delete' item in the context menu only when opened from Issues#index, Gantts#show, or Calendars#show. --- app/controllers/context_menus_controller.rb | 13 ++++++++++ app/views/context_menus/issues.html.erb | 6 +++-- .../context_menus_controller_test.rb | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/app/controllers/context_menus_controller.rb b/app/controllers/context_menus_controller.rb index 1e37f623b..bb9e5d82f 100644 --- a/app/controllers/context_menus_controller.rb +++ b/app/controllers/context_menus_controller.rb @@ -46,6 +46,19 @@ class ContextMenusController < ApplicationController @priorities = IssuePriority.active.reverse @back = back_url + begin + # Recognize the controller and action from the back_url to determine + # which view triggered the context menu. + route = Rails.application.routes.recognize_path(@back) + @include_delete = + [ + {controller: 'issues', action: 'index'}, + {controller: 'gantts', action: 'show'}, + {controller: 'calendars', action: 'show'} + ].any?(route.slice(:controller, :action)) + rescue ActionController::RoutingError + @include_delete = false + end @columns = params[:c] diff --git a/app/views/context_menus/issues.html.erb b/app/views/context_menus/issues.html.erb index ee7eaa18c..23b7718f1 100644 --- a/app/views/context_menus/issues.html.erb +++ b/app/views/context_menus/issues.html.erb @@ -173,8 +173,10 @@
  • <%= context_menu_link sprite_icon('copy', l(:button_copy)), bulk_edit_issues_path(:ids => @issue_ids, :copy => '1'), :class => 'icon icon-copy', :disabled => !@can[:copy] %>
  • <% end %> -
  • <%= context_menu_link sprite_icon('del', l(:button_delete_object, object_name: (@issue_ids.size > 1 ? l(:label_issue_plural) : l(:label_issue))).capitalize), issues_path(:ids => @issue_ids, :back_url => @back), - :method => :delete, :data => {:confirm => issues_destroy_confirmation_message(@issues)}, :class => 'icon icon-del', :disabled => !@can[:delete] %>
  • + <% if @include_delete %> +
  • <%= context_menu_link sprite_icon('del', l(:button_delete_object, object_name: (@issue_ids.size > 1 ? l(:label_issue_plural) : l(:label_issue))).capitalize), issues_path(:ids => @issue_ids, :back_url => @back), + :method => :delete, :data => {:confirm => issues_destroy_confirmation_message(@issues)}, :class => 'icon icon-del', :disabled => !@can[:delete] %>
  • + <% end %> <%= call_hook(:view_issues_context_menu_end, {:issues => @issues, :can => @can, :back => @back }) %> diff --git a/test/functional/context_menus_controller_test.rb b/test/functional/context_menus_controller_test.rb index 376f6f62b..77a5649f4 100644 --- a/test/functional/context_menus_controller_test.rb +++ b/test/functional/context_menus_controller_test.rb @@ -459,4 +459,30 @@ class ContextMenusControllerTest < Redmine::ControllerTest assert_select 'a.disabled', :text => 'Bulk edit' end + + def test_context_menu_should_include_delete_for_allowed_back_urls + @request.session[:user_id] = 2 + %w[ + /issues + /projects/ecookbook/issues/gantt + /projects/ecookbook/issues/calendar + ].each do |back_url| + get :issues, :params => { :ids => [1], :back_url => back_url } + assert_response :success + assert_select 'a.icon-del', :text => /Delete/ + end + end + + def test_context_menu_should_not_include_delete_for_disallowed_back_urls + @request.session[:user_id] = 2 + %w[ + /issues/1 + /projects/ecookbook/roadmap + /not/a/real/path + ].each do |back_url| + get :issues, :params => { :ids => [1], :back_url => back_url } + assert_response :success + assert_select 'a.icon-del', :count => 0 + end + end end -- 2.50.1