Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[OP#57516] Projects should be removable from list with the help of a menu item

Kabiru Mwenja9d1db1ff0b03
diff --git a/app/components/admin/custom_fields/custom_field_projects/row_component.rb b/app/components/admin/custom_fields/custom_field_projects/row_component.rb
index fe94c8e9f65..ee4cc22f9df 100644
--- a/app/components/admin/custom_fields/custom_field_projects/row_component.rb
+++ b/app/components/admin/custom_fields/custom_field_projects/row_component.rb
@@ -37,8 +37,31 @@ module Admin
end
def more_menu_items
- []
+ @more_menu_items ||= [more_menu_detach_project].compact
end
+
+ private
+
+ def more_menu_detach_project
+ if User.current.admin && project.active?
+ {
+ scheme: :default,
+ icon: nil,
+ label: I18n.t("projects.settings.project_custom_fields.actions.remove_from_project"),
+ href: detach_from_project_url,
+ data: { turbo_method: :delete }
+ }
+ end
+ end
+
+ def detach_from_project_url
+ url_helpers.custom_field_project_path(
+ custom_field_id: @table.params[:custom_field].id,
+ custom_fields_project: { project_id: project.id }
+ )
+ end
+
+ def project = model.first
end
end
end
diff --git a/app/components/settings/project_custom_fields/project_custom_field_mapping/row_component.rb b/app/components/settings/project_custom_fields/project_custom_field_mapping/row_component.rb
index f785bfd2d76..f4645d2a23f 100644
--- a/app/components/settings/project_custom_fields/project_custom_field_mapping/row_component.rb
+++ b/app/components/settings/project_custom_fields/project_custom_field_mapping/row_component.rb
@@ -30,26 +30,13 @@ module Settings
module ProjectCustomFields
module ProjectCustomFieldMapping
class RowComponent < Admin::CustomFields::CustomFieldProjects::RowComponent
- def more_menu_items
- @more_menu_items ||= [more_menu_detach_project].compact
- end
-
private
- def more_menu_detach_project
- project = model.first
- if User.current.admin && project.active?
- {
- scheme: :default,
- icon: nil,
- label: I18n.t("projects.settings.project_custom_fields.actions.remove_from_project"),
- href: unlink_admin_settings_project_custom_field_path(
- id: @table.params[:custom_field].id,
- project_custom_field_project_mapping: { project_id: project.id }
- ),
- data: { turbo_method: :delete }
- }
- end
+ def detach_from_project_url
+ url_helpers.unlink_admin_settings_project_custom_field_path(
+ id: @table.params[:custom_field].id,
+ project_custom_field_project_mapping: { project_id: project.id }
+ )
end
end
end
diff --git a/app/controllers/admin/custom_fields/custom_field_projects_controller.rb b/app/controllers/admin/custom_fields/custom_field_projects_controller.rb
index c314cadd5a4..32ce2877a81 100644
--- a/app/controllers/admin/custom_fields/custom_field_projects_controller.rb
+++ b/app/controllers/admin/custom_fields/custom_field_projects_controller.rb
@@ -31,6 +31,7 @@
class Admin::CustomFields::CustomFieldProjectsController < ApplicationController
include OpTurbo::ComponentStream
include OpTurbo::DialogStreamHelper
+ include FlashMessagesOutputSafetyHelper
layout "admin"
@@ -39,9 +40,10 @@ class Admin::CustomFields::CustomFieldProjectsController < ApplicationController
before_action :require_admin
before_action :find_model_object
- before_action :available_custom_fields_projects_query, only: :index
+ before_action :available_custom_fields_projects_query, only: %i[index destroy]
before_action :initialize_custom_field_project, only: :new
before_action :find_projects_to_activate_for_custom_field, only: :create
+ before_action :find_custom_field_project_to_destroy, only: :destroy
menu_item :custom_fields
@@ -72,6 +74,23 @@ class Admin::CustomFields::CustomFieldProjectsController < ApplicationController
respond_to_with_turbo_streams(status: create_service.success? ? :ok : :unprocessable_entity)
end
+ def destroy
+ delete_service = ::CustomFields::CustomFieldProjects::DeleteService
+ .new(user: current_user, model: @custom_field_project)
+ .call
+
+ delete_service.on_success { render_project_list(url_for_action: :index) }
+
+ delete_service.on_failure do
+ update_flash_message_via_turbo_stream(
+ message: join_flash_messages(delete_service.errors.full_messages),
+ full: true, dismiss_scheme: :hide, scheme: :danger
+ )
+ end
+
+ respond_to_with_turbo_streams(status: delete_service.success? ? :ok : :unprocessable_entity)
+ end
+
def default_breadcrumb; end
def show_local_breadcrumb
@@ -117,6 +136,18 @@ class Admin::CustomFields::CustomFieldProjectsController < ApplicationController
respond_with_turbo_streams
end
+ def find_custom_field_project_to_destroy
+ @project = Project.find(params.to_unsafe_h[:custom_fields_project][:project_id])
+ @custom_field_project = CustomFieldsProject.find_by!(custom_field: @custom_field, project: @project)
+ rescue ActiveRecord::RecordNotFound
+ update_flash_message_via_turbo_stream(
+ message: t(:notice_project_not_found), full: true, dismiss_scheme: :hide, scheme: :danger
+ )
+ update_project_list_via_turbo_stream
+
+ respond_with_turbo_streams
+ end
+
def update_project_list_via_turbo_stream(url_for_action: action_name)
update_via_turbo_stream(
component: Admin::CustomFields::CustomFieldProjects::TableComponent.new(
diff --git a/app/services/custom_fields/custom_field_projects/delete_service.rb b/app/services/custom_fields/custom_field_projects/delete_service.rb
new file mode 100644
index 00000000000..e07e0c1ca5b
--- /dev/null
+++ b/app/services/custom_fields/custom_field_projects/delete_service.rb
@@ -0,0 +1,46 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module CustomFields
+ module CustomFieldProjects
+ class DeleteService < ::BaseServices::Delete
+ def destroy(custom_field_project)
+ CustomFieldsProject.transaction do
+ # `custom_fields_projects` table has no `id` column, hence no primary key. #destroy method would not work
+ # Note: `delete_all` does not trigger callbacks
+ CustomFieldsProject.where(custom_field_id: custom_field_project.custom_field_id,
+ project_id: custom_field_project.project_id).delete_all
+ end
+ end
+
+ # Mappings have custom deletion rules that are similar to the update rules all derived from the base contract
+ # Reuse the update contract to ensure that the deletion rules are consistent with the update rules
+ def default_contract_class = CustomFields::CustomFieldProjects::UpdateContract
+ end
+ end
+end
diff --git a/config/routes.rb b/config/routes.rb
index e4c9dbe7308..4f095390345 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -175,6 +175,9 @@ Rails.application.routes.draw do
resources :projects,
controller: "/admin/custom_fields/custom_field_projects",
only: %i[index new create]
+ resource :project,
+ controller: "/admin/custom_fields/custom_field_projects",
+ only: :destroy
end
end
end
diff --git a/spec/features/admin/custom_fields/custom_fields_project_spec.rb b/spec/features/admin/custom_fields/custom_fields_project_spec.rb
index c587b664a99..e4f6f222412 100644
--- a/spec/features/admin/custom_fields/custom_fields_project_spec.rb
+++ b/spec/features/admin/custom_fields/custom_fields_project_spec.rb
@@ -40,6 +40,8 @@ RSpec.describe "Custom Fields Multi-Project Activation", :js do
create(:custom_fields_project, custom_field:, project: archived_project)
end
+ let(:custom_field_projects_page) { Pages::Admin::CustomFields::CustomFieldsProjects::CustomFieldProjectsIndex.new }
+
context "with insufficient permissions" do
it "is not accessible" do
login_as(non_admin)
@@ -125,6 +127,27 @@ RSpec.describe "Custom Fields Multi-Project Activation", :js do
end
end
+ it "allows unlinking a project from a custom field" do
+ project = create(:project)
+ create(:custom_fields_project, custom_field:, project:)
+
+ custom_field_projects_page.click_menu_item_of("Remove from project", project)
+
+ expect(page).to have_no_text(project.name)
+
+ aggregate_failures "pagination links maintain the correct url after unlinking is done" do
+ within ".op-pagination" do
+ pagination_links = page.all(".op-pagination--item-link")
+ expect(pagination_links.size).to be_positive
+
+ pagination_links.each do |pagination_link|
+ uri = URI.parse(pagination_link["href"])
+ expect(uri.path).to eq(custom_field_projects(custom_field))
+ end
+ end
+ end
+ end
+
context "and the project custom field is for all projects" do
shared_let(:custom_field) { create(:user_custom_field, is_for_all: true) }
diff --git a/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb b/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb
new file mode 100644
index 00000000000..a3e68133bc9
--- /dev/null
+++ b/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb
@@ -0,0 +1,40 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require "services/base_services/behaves_like_delete_service"
+
+RSpec.describe CustomFields::CustomFieldProjects::DeleteService do
+ it_behaves_like "BaseServices delete service" do
+ let(:factory) { :custom_fields_project }
+
+ let(:contract_class) do
+ "#{namespace}::UpdateContract".constantize
+ end
+ end
+end
diff --git a/spec/support/pages/admin/custom_fields/custom_fields_projects/custom_field_projects_index.rb b/spec/support/pages/admin/custom_fields/custom_fields_projects/custom_field_projects_index.rb
new file mode 100644
index 00000000000..a170b17e58e
--- /dev/null
+++ b/spec/support/pages/admin/custom_fields/custom_fields_projects/custom_field_projects_index.rb
@@ -0,0 +1,51 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "support/pages/projects/index"
+
+module Pages
+ module Admin
+ module CustomFields
+ module CustomFieldsProjects
+ class CustomFieldProjectsIndex < ::Pages::Projects::Index
+ def path
+ "/custom_fields/#{custom_field.id}/projects"
+ end
+
+ def within_row(project)
+ row = page.find("#admin-custom-fields-custom-field-projects-row-component-project-#{project.id}")
+ row.hover
+ within row do
+ yield row
+ end
+ end
+ end
+ end
+ end
+ end
+end