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

Make delete action call the UpdateService as well

Christophe Bliard345ead12c6d4
diff --git a/app/controllers/work_package_children_controller.rb b/app/controllers/work_package_children_controller.rb
index 6bb5c3b70e2..d4eac1a084f 100644
--- a/app/controllers/work_package_children_controller.rb
+++ b/app/controllers/work_package_children_controller.rb
@@ -36,9 +36,6 @@ class WorkPackageChildrenController < ApplicationController
before_action :authorize # Short-circuit early if not authorized
- before_action :set_child, except: %i[new create]
- before_action :set_relations, except: %i[new create]
-
def new
component = WorkPackageRelationsTab::AddWorkPackageChildDialogComponent
.new(work_package: @work_package)
@@ -46,40 +43,33 @@ class WorkPackageChildrenController < ApplicationController
end
def create
- target_work_package_id = params[:work_package][:id]
- target_child_work_package = WorkPackage.find(target_work_package_id)
-
- service_result = WorkPackages::UpdateService.new(user: current_user, model: target_child_work_package)
- .call(parent: @work_package)
- if service_result.success?
- @children = @work_package.children.visible
- @relations = @work_package.relations.visible
+ child = WorkPackage.find(params[:work_package][:id])
+ service_result = set_relation(child:, parent: @work_package)
- component = WorkPackageRelationsTab::IndexComponent.new(
- work_package: @work_package,
- relations: @relations,
- children: @children
- )
- replace_via_turbo_stream(component:)
- update_flash_message_via_turbo_stream(
- message: I18n.t(:notice_successful_update), scheme: :success
- )
- respond_with_turbo_streams
- else
- respond_with_turbo_streams(status: :unprocessable_entity)
- end
+ respond_with_relations_tab_update(service_result)
end
def destroy
- @child.parent = nil
+ child = WorkPackage.find(params[:id])
+ service_result = set_relation(child:, parent: nil)
- if @child.save
+ respond_with_relations_tab_update(service_result)
+ end
+
+ private
+
+ def set_relation(child:, parent:)
+ WorkPackages::UpdateService.new(user: current_user, model: child)
+ .call(parent:)
+ end
+
+ def respond_with_relations_tab_update(service_result)
+ if service_result.success?
@work_package.reload
- @children = @work_package.children.visible
component = WorkPackageRelationsTab::IndexComponent.new(
work_package: @work_package,
- relations: @relations,
- children: @children
+ relations: @work_package.relations.visible,
+ children: @work_package.children.visible
)
replace_via_turbo_stream(component:)
update_flash_message_via_turbo_stream(
@@ -87,21 +77,13 @@ class WorkPackageChildrenController < ApplicationController
)
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])
@project = @work_package.project
end
-
- def set_child
- @child = WorkPackage.find(params[:id])
- end
-
- def set_relations
- @relations = @work_package.relations.visible
- end
end
diff --git a/spec/controllers/work_package_children_controller_spec.rb b/spec/controllers/work_package_children_controller_spec.rb
index d5cdcdceafe..0f71b58d3b5 100644
--- a/spec/controllers/work_package_children_controller_spec.rb
+++ b/spec/controllers/work_package_children_controller_spec.rb
@@ -79,24 +79,42 @@ RSpec.describe WorkPackageChildrenController do
end
describe "DELETE /work_packages/:work_package_id/children/:id" do
- before do
- allow(WorkPackageRelationsTab::IndexComponent).to receive(:new).and_call_original
- allow(controller).to receive(:replace_via_turbo_stream).and_call_original
- end
-
- it "deletes the child relationship" do
+ def send_delete_request
delete("destroy",
params: { work_package_id: work_package.id,
id: child_work_package.id },
as: :turbo_stream)
+ end
- expect(response).to be_successful
+ it "deletes the child relationship" do
+ send_delete_request
+
+ expect(response).to have_http_status(:ok)
+ expect(child_work_package.reload.parent).to be_nil
+ end
+
+ it "renders the relations tab index component" do
+ allow(WorkPackageRelationsTab::IndexComponent).to receive(:new).and_call_original
+ allow(controller).to receive(:replace_via_turbo_stream).and_call_original
+
+ send_delete_request
expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)
.with(work_package:, relations: [], children: [])
expect(controller).to have_received(:replace_via_turbo_stream)
.with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))
- expect(child_work_package.reload.parent).to be_nil
+ end
+
+ it "updates dependent work packages" do
+ allow(WorkPackages::UpdateAncestorsService).to receive(:new).and_call_original
+ allow(WorkPackages::SetScheduleService).to receive(:new).and_call_original
+
+ send_delete_request
+
+ expect(WorkPackages::UpdateAncestorsService).to have_received(:new)
+ .with(user: user, work_package: child_work_package)
+ expect(WorkPackages::SetScheduleService).to have_received(:new)
+ .with(a_hash_including(work_package: [child_work_package]))
end
end
end