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

Switch parent to manual scheduling mode when it loses its last child

Christophe Bliard661229c18ccb
diff --git a/app/services/work_packages/update_service.rb b/app/services/work_packages/update_service.rb
index 68aabe92cd6..3d8cb6926dd 100644
--- a/app/services/work_packages/update_service.rb
+++ b/app/services/work_packages/update_service.rb
@@ -129,42 +129,24 @@ class WorkPackages::UpdateService < BaseServices::Update
end
def reschedule_related(work_package)
- moved_work_packages = [work_package]
+ work_packages_to_reschedule = [work_package]
- # if parent changed, we find a child or a predecessor of the former parent to
- # give it to the SetScheduleService so that the former parent is rescheduled.
+ # if parent changed, the former parent needs to be rescheduled too.
if parent_just_changed?(work_package)
- former_parent_id = work_package.parent_id_before_last_save
- some_child_or_predecessor = find_some_child_or_predecessor(former_parent_id)
- if some_child_or_predecessor
- moved_work_packages << some_child_or_predecessor
- else # rubocop:disable Style/EmptyElse
- # aha! switch former parent to manual mode?
- end
+ former_parent = WorkPackage.find_by(id: work_package.parent_id_before_last_save)
+ work_packages_to_reschedule << former_parent if former_parent
end
- reschedule(work_package, moved_work_packages).dependent_results
+ WorkPackages::SetScheduleService
+ .new(user:, work_package: work_packages_to_reschedule, initiated_by: cause_of_rescheduling)
+ .call(work_package.saved_changes.keys.map(&:to_sym))
+ .dependent_results
end
def parent_just_changed?(work_package)
work_package.saved_change_to_parent_id? && work_package.parent_id_before_last_save
end
- def find_some_child_or_predecessor(former_parent_id)
- former_parent = WorkPackage.find(former_parent_id)
- if a_child = former_parent.children.first
- a_child
- elsif a_relation = Relation.follows.of_successor(former_parent).first
- a_relation.predecessor
- end
- end
-
- def reschedule(work_package, work_packages)
- WorkPackages::SetScheduleService
- .new(user:, work_package: work_packages, initiated_by: cause_of_rescheduling)
- .call(work_package.saved_changes.keys.map(&:to_sym))
- end
-
# When multiple services change a work package, we still only want one update to the database due to:
# * performance
# * having only one journal entry
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index cf1f2229f36..27408311ec1 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -1082,6 +1082,33 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
end
end
+ context "when updating child dates" do
+ context "with a hierarchy of ancestors" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode
+ | grandparent | XXX | automatic
+ | parent | XXX | automatic
+ | child | XXX | automatic
+ | work_package | XXX | manual
+ TABLE
+
+ let(:attributes) { { start_date: _table.tuesday, due_date: _table.friday } }
+
+ it "updates the dates of the whole ancestors hierarchy" do
+ expect(subject).to be_success
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package", "child", "parent", "grandparent")
+
+ expect_work_packages_after_reload([work_package, child, parent, grandparent], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | grandparent | XXXX | automatic
+ | parent | XXXX | automatic
+ | child | XXXX | automatic
+ | work_package | XXXX | manual
+ TABLE
+ end
+ end
+ end
+
context "when switching scheduling mode to automatic" do
let(:attributes) { { schedule_manually: false } }
@@ -1100,6 +1127,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "sets the dates to the child dates, despite the predecessor" do
expect(subject).to be_success
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package")
expect_work_packages_after_reload([work_package, predecessor, child], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1122,6 +1150,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "sets the dates to start after the predecessor" do
expect(subject).to be_success
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package", "child")
expect_work_packages_after_reload([predecessor, child_predecessor, work_package, child], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1145,6 +1174,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "reschedule the automatic child to start after the predecessor and parent dates span over both children dates" do
expect(subject).to be_success
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package", "child2")
expect_work_packages_after_reload([predecessor, work_package, child1, child2], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1167,7 +1197,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "sets the parent start and due dates to the children earliest and latest dates" do
expect(subject).to be_success
- expect(subject.all_results).to contain_exactly(work_package)
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package")
expect_work_packages_after_reload([work_package, child1, child2, child3], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1189,7 +1219,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "sets the parent start and due dates to the children earliest and latest start dates" do
expect(subject).to be_success
- expect(subject.all_results).to contain_exactly(work_package)
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package")
expect_work_packages_after_reload([work_package, child1, child2], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1210,7 +1240,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "sets the parent start and due dates to the children earliest and latest due dates" do
expect(subject).to be_success
- expect(subject.all_results).to contain_exactly(work_package)
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package")
expect_work_packages_after_reload([work_package, child1, child2], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1230,7 +1260,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "clears the parent dates" do
expect(subject).to be_success
- expect(subject.all_results).to contain_exactly(work_package)
+ expect(subject.all_results.pluck(:subject)).to contain_exactly("work_package")
expect_work_packages_after_reload([work_package, child], <<~TABLE)
| subject | MTWTFSS | scheduling mode
@@ -1327,6 +1357,28 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
TABLE
end
end
+
+ describe "when the parent has no predecessors" do
+ let_work_packages(<<~TABLE)
+ hierarchy | MTWTFSS | scheduling mode
+ # the child will be removed
+ parent | XXX | automatic
+ work_package | XXX | manual
+ TABLE
+
+ it "keeps former parent dates and switch to manual scheduling mode" do
+ expect(subject).to be_success
+ expect(subject.all_results.pluck(:subject))
+ .to contain_exactly("work_package", "parent")
+ expect(work_package.reload.parent).to be_nil
+
+ expect_work_packages(subject.all_results, <<~TABLE)
+ subject | MTWTFSS | scheduling mode
+ parent | XXX | manual
+ work_package | XXX | manual
+ TABLE
+ end
+ end
end
describe "replacing the attachments" do
diff --git a/spec/support/table_helpers/let_work_packages.rb b/spec/support/table_helpers/let_work_packages.rb
index fcb00e8f7f6..6a56d1bcc05 100644
--- a/spec/support/table_helpers/let_work_packages.rb
+++ b/spec/support/table_helpers/let_work_packages.rb
@@ -63,9 +63,9 @@ module TableHelpers
# end
def let_work_packages(table_representation)
let!(:_table) { create_table(table_representation) }
+ let(:table_work_packages) { _table.work_packages }
table_data = TableData.for(table_representation)
- let(:table_work_packages) { _table.work_packages }
table_data.work_package_identifiers.each do |identifier|
let(identifier) { _table.work_package(identifier) }
end