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

Add ability to delete relations from within the tab

Aaron Contrerasf698bb93736a
diff --git a/app/components/work_package_relations_tab/index_component.html.erb b/app/components/work_package_relations_tab/index_component.html.erb
index bebeff3d9c1..1765c8201d1 100644
--- a/app/components/work_package_relations_tab/index_component.html.erb
+++ b/app/components/work_package_relations_tab/index_component.html.erb
@@ -36,8 +36,8 @@
title: t("#{base_key}_plural").capitalize,
items: relations_of_type
) do |relation|
- related_work_package = relation.from == work_package ? relation.to : relation.from
- render(WorkPackageRelationsTab::RelationComponent.new(work_package: related_work_package))
+ render(WorkPackageRelationsTab::RelationComponent.new(work_package:,
+ relation:))
end
end
end
@@ -51,7 +51,9 @@
title: t("#{base_key}_plural").capitalize,
items: children
) do |child|
- render(WorkPackageRelationsTab::RelationComponent.new(work_package: child))
+ render(WorkPackageRelationsTab::RelationComponent.new(work_package:,
+ relation: nil,
+ child:))
end
end
end
diff --git a/app/components/work_package_relations_tab/index_component.rb b/app/components/work_package_relations_tab/index_component.rb
index 740f907e5b2..981631e9e9a 100644
--- a/app/components/work_package_relations_tab/index_component.rb
+++ b/app/components/work_package_relations_tab/index_component.rb
@@ -48,10 +48,14 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
end
items.each do |item|
- border_box.with_row(py: 2) do
+ border_box.with_row(py: 2, test_selector: row_test_selector(item)) do
yield(item)
end
end
end
end
+
+ def row_test_selector(item)
+ "op-relation-row-#{item.id}"
+ end
end
diff --git a/app/components/work_package_relations_tab/relation_component.html.erb b/app/components/work_package_relations_tab/relation_component.html.erb
index 96d92cb6712..9030f8108fe 100644
--- a/app/components/work_package_relations_tab/relation_component.html.erb
+++ b/app/components/work_package_relations_tab/relation_component.html.erb
@@ -4,33 +4,32 @@
align_items: :center,
style: "gap: 0.5rem;") do |meta_information_row|
meta_information_row.with_column do
- render(WorkPackages::HighlightedTypeComponent.new(work_package: @work_package, font_size: :small))
+ render(WorkPackages::HighlightedTypeComponent.new(work_package: related_work_package, font_size: :small))
end
meta_information_row.with_column do
- render(Primer::Beta::Text.new(font_size: :small,color: :muted)) { "##{work_package.id}" }
+ render(Primer::Beta::Text.new(font_size: :small,color: :muted)) { "##{related_work_package.id}" }
end
meta_information_row.with_column do
- render WorkPackages::StatusBadgeComponent.new(status: work_package.status)
+ render WorkPackages::StatusBadgeComponent.new(status: related_work_package.status)
end
end
flex.with_row(flex_layout: true, justify_content: :space_between, align_items: :center) do |subject_line_row|
subject_line_row.with_column do
- render(Primer::Beta::Link.new(href: work_package_path(work_package),
+ render(Primer::Beta::Link.new(href: work_package_path(related_work_package),
color: :default,
underline: false,
font_size: :normal,
font_weight: :bold,
- target: "_blank")) { work_package.subject }
+ target: "_blank")) { related_work_package.subject }
end
subject_line_row.with_column do
- render(Primer::Alpha::ActionMenu.new) do |menu|
+ render(Primer::Alpha::ActionMenu.new(test_selector: action_menu_test_selector)) do |menu|
menu.with_show_button(icon: "kebab-horizontal",
'aria-label': I18n.t(:label_agenda_item_actions),
scheme: :invisible,
- ml: 2,
- test_selector: 'op-meeting-agenda-actions')
+ ml: 2)
menu.with_item(label: "Edit relation",
href: "#",
content_arguments: {
@@ -39,23 +38,28 @@
item.with_leading_visual_icon(icon: :pencil)
end
menu.with_item(label: "Delete relation",
- scheme: :danger,
- href: "#",
- form_arguments: {
- method: :delete, data: { confirm: t("text_are_you_sure"), "turbo-stream": true }
- }) do |item|
+ scheme: :danger,
+ href: destroy_path,
+ form_arguments: {
+ method: :delete,
+ data: {
+ confirm: t("text_are_you_sure"),
+ turbo_stream: true
+ }
+ },
+ test_selector: delete_button_test_selector) do |item|
item.with_leading_visual_icon(icon: :trash)
end
end
end
end
- if work_package.description.present?
+ if related_work_package.description.present?
flex.with_row(flex_layout: true, justify_content: :flex_start) do |description_row|
description_row.with_column do
render(Primer::Beta::Text.new(font_size: :small,
color: :muted)) do
- format_text(work_package, :description)
+ format_text(related_work_package, :description)
end
end
end
diff --git a/app/components/work_package_relations_tab/relation_component.rb b/app/components/work_package_relations_tab/relation_component.rb
index 0a71843dab4..433cf17d4ae 100644
--- a/app/components/work_package_relations_tab/relation_component.rb
+++ b/app/components/work_package_relations_tab/relation_component.rb
@@ -2,11 +2,51 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
include ApplicationHelper
include OpPrimer::ComponentHelpers
- attr_reader :work_package
+ attr_reader :work_package, :relation
- def initialize(work_package:)
+ def initialize(work_package:,
+ relation:,
+ child: nil)
super()
@work_package = work_package
+ @relation = relation
+ @child = child
+ end
+
+ def related_work_package
+ @related_work_package ||= if parent_child_relationship?
+ @child
+ else
+ relation.from == work_package ? relation.to : relation.from
+ end
+ end
+
+ private
+
+ def parent_child_relationship? = @child.present?
+
+ def underlying_resource_id
+ @underlying_resource_id ||= if parent_child_relationship?
+ @child.id
+ else
+ @relation.id
+ end
+ end
+
+ def destroy_path
+ if parent_child_relationship?
+ work_package_child_path(@child)
+ else
+ work_package_relation_path(@work_package, @relation)
+ end
+ end
+
+ def action_menu_test_selector
+ "op-relation-row-#{underlying_resource_id}-action-menu"
+ end
+
+ def delete_button_test_selector
+ "op-relation-row-#{underlying_resource_id}-delete-button"
end
end
diff --git a/app/controllers/work_package_children_controller.rb b/app/controllers/work_package_children_controller.rb
new file mode 100644
index 00000000000..f5e80be75a1
--- /dev/null
+++ b/app/controllers/work_package_children_controller.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+class WorkPackageChildrenController < ApplicationController
+ include OpTurbo::ComponentStream
+
+ before_action :set_child
+ before_action :set_work_package
+
+ before_action :authorize # Short-circuit early if not authorized
+
+ before_action :set_relations
+
+ def destroy
+ @child.parent = nil
+
+ if @child.save
+ @children = @work_package.children.visible
+ component = WorkPackageRelationsTab::IndexComponent.new(
+ work_package: @work_package,
+ relations: @relations,
+ children: @children
+ )
+ replace_via_turbo_stream(component:)
+
+ respond_with_turbo_streams
+ end
+ end
+
+ private
+
+ def set_child
+ @child = WorkPackage.find(params[:id])
+ end
+
+ def set_work_package
+ @work_package = @child.parent
+ end
+
+ def set_relations
+ @relations = @work_package.relations.visible
+ end
+end
diff --git a/app/controllers/work_package_relations_controller.rb b/app/controllers/work_package_relations_controller.rb
new file mode 100644
index 00000000000..1e49d6d9a6e
--- /dev/null
+++ b/app/controllers/work_package_relations_controller.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+class WorkPackageRelationsController < ApplicationController
+ include OpTurbo::ComponentStream
+
+ before_action :set_work_package
+ before_action :set_relation
+ before_action :authorize
+
+ def destroy
+ service_result = Relations::DeleteService.new(user: current_user, model: @relation).call
+
+ if service_result.success?
+ @children = WorkPackage.where(parent_id: @work_package.id)
+ @relations = @work_package
+ .relations
+ .includes(:to, :from)
+
+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package,
+ relations: @relations,
+ children: @children)
+ replace_via_turbo_stream(component:)
+ respond_with_turbo_streams
+ else
+ respond_with_turbo_streams(status: :unprocessable_entity)
+ end
+ end
+
+ private
+
+ def set_work_package
+ @work_package = WorkPackage.find(params[:work_package_id])
+ end
+
+ def set_relation
+ @relation = @work_package.relations.find(params[:id])
+ end
+end
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 9305f8f0b75..f2aea7ca0a4 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -323,7 +323,9 @@ Rails.application.reloader.to_prepare do
dependencies: :view_work_packages
wpt.permission :manage_subtasks,
- {},
+ {
+ work_package_children: %i[destroy]
+ },
permissible_on: :project,
dependencies: :view_work_packages
# Queries
diff --git a/config/routes.rb b/config/routes.rb
index 5a5862103b4..a64e2842ef7 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -603,6 +603,7 @@ Rails.application.routes.draw do
as: :work_package_progress
end
resources :relations_tab, only: %i[index], controller: "work_package_relations_tab"
+ resources :relations, only: %i[destroy], controller: "work_package_relations"
get "/export_dialog" => "work_packages#export_dialog", on: :collection, as: "export_dialog"
get :show_conflict_flash_message, on: :collection # we don't need a specific work package for this
@@ -619,6 +620,8 @@ Rails.application.routes.draw do
get "/edit" => "work_packages#show", on: :member, as: "edit"
end
+ resources :work_package_children, only: %i[destroy]
+
resources :versions, only: %i[show edit update destroy] do
member do
get :status_by
diff --git a/spec/features/work_packages/details/relations/primerized_relations_spec.rb b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
index 3798ae260c7..c9f7b1f91d7 100644
--- a/spec/features/work_packages/details/relations/primerized_relations_spec.rb
+++ b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
@@ -29,8 +29,7 @@
require "spec_helper"
RSpec.describe "Primerized work package relations tab",
- :cuprite,
- :js do
+ :js, :with_cuprite do
let(:user) { create(:admin) }
let(:project) { create(:project) }
let(:work_package) { create(:work_package, project:) }
@@ -70,6 +69,12 @@ RSpec.describe "Primerized work package relations tab",
to: from1,
relation_type: Relation::TYPE_FOLLOWS)
end
+ let!(:child_wp) do
+ create(:work_package,
+ parent: work_package,
+ type: type1,
+ project: project)
+ end
current_user { user }
@@ -117,4 +122,47 @@ RSpec.describe "Primerized work package relations tab",
expect(page).to have_text(label_for_relation_type(target_relation_type))
end
end
+
+ describe "deletion" do
+ it "can delete relations" do
+ scroll_to_element find(".detail-panel--relations")
+
+ # Find the first relation and delete it
+ relation_row = page.find("[data-test-selector='op-relation-row-#{relation1.id}']")
+
+ within(relation_row) do
+ page.find("[data-test-selector='op-relation-row-#{relation1.id}-action-menu']").click
+ page.find("[data-test-selector='op-relation-row-#{relation1.id}-delete-button']").click
+ end
+
+ wait_for_reload
+
+ # Expect the relation to be gone
+ within "##{WorkPackageRelationsTab::IndexComponent::FRAME_ID}" do
+ expect(page).to have_no_text(relation1.to.subject)
+ end
+
+ expect { relation1.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "can delete children" do
+ scroll_to_element find(".detail-panel--relations")
+
+ # Find the first relation and delete it
+ child_row = page.find("[data-test-selector='op-relation-row-#{child_wp.id}']")
+
+ within(child_row) do
+ page.find("[data-test-selector='op-relation-row-#{child_wp.id}-action-menu']").click
+ page.find("[data-test-selector='op-relation-row-#{child_wp.id}-delete-button']").click
+ end
+
+ wait_for_reload
+
+ within "##{WorkPackageRelationsTab::IndexComponent::FRAME_ID}" do
+ expect(page).to have_no_text(child_wp.subject)
+ end
+
+ expect(child_wp.reload.parent).to be_nil
+ end
+ end
end