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

Derive duration from start and end dates

Christophe Bliard9f69eed2532d
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 11cde74743e..5cf8171f213 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -68,17 +68,21 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
derivable_attribute_by_others_presence || derivable_attribute_by_others_absence
end
- # Returns a field derivable by the presence of the two others, or +nil+
- # if none was found.
+ # Returns a field derivable by the presence of the two others, or +nil+ if
+ # none was found.
#
# Matching is done in the order :duration, :due_date, :start_date. The first
# one to match is returned.
#
- # If +ignore_non_working_days+ has been changed, then +duration+ must stay the
- # same and only +due_date+ and +start_date+ can be derivable.
+ # If +ignore_non_working_days+ has been changed, try deriving +due_date+ and
+ # +start_date+ before +duration+.
def derivable_attribute_by_others_presence
- fields = %i[duration due_date start_date]
- fields = fields.without(:duration) if work_package.ignore_non_working_days_changed?
+ fields =
+ if work_package.ignore_non_working_days_changed?
+ %i[due_date start_date duration]
+ else
+ %i[duration due_date start_date]
+ end
fields.find { |field| derivable_by_others_presence?(field) }
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 854468629dd..97ad239078c 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1021,8 +1021,11 @@ describe WorkPackages::SetAttributesService,
context 'with non-working days' do
shared_let(:week_days) { create(:week_days) }
let(:monday) { Time.zone.today.beginning_of_week }
+ let(:tuesday) { monday + 1.day }
let(:wednesday) { monday + 2.days }
+ let(:friday) { monday + 4.days }
let(:next_monday) { monday + 7.days }
+ let(:next_tuesday) { monday + 8.days }
context 'when start date changes' do
let(:work_package) do
@@ -1076,7 +1079,7 @@ describe WorkPackages::SetAttributesService,
let(:work_package) do
build_stubbed(:work_package, start_date: monday, due_date: next_monday, ignore_non_working_days: false)
end
- let(:call_attributes) { { due_date: next_monday + 1.day, duration: 4 } }
+ let(:call_attributes) { { due_date: next_tuesday, duration: 4 } }
it_behaves_like 'service call' do
it "updates the start date and skips the non-working days" do
@@ -1130,7 +1133,6 @@ describe WorkPackages::SetAttributesService,
it_behaves_like 'service call' do
it "does not recompute the due date and nilifies the due date and the duration instead" do
- # start_date and duration are checked too to ensure they did not change
expect { subject }
.to change { work_package.slice(:start_date, :due_date, :duration) }
.from(start_date: monday, due_date: next_monday, duration: 8)
@@ -1143,11 +1145,10 @@ describe WorkPackages::SetAttributesService,
let(:work_package) do
build_stubbed(:work_package, start_date: monday, due_date: next_monday, ignore_non_working_days: true)
end
- let(:call_attributes) { { ignore_non_working_days: false, due_date: wednesday } }
+ let(:call_attributes) { { due_date: wednesday, ignore_non_working_days: false } }
it_behaves_like 'service call' do
it "updates the start date from due date and duration to skip the non-working days" do
- # start_date and duration are checked too to ensure they did not change
expect { subject }
.to change { work_package.slice(:start_date, :due_date, :duration) }
.from(start_date: monday, due_date: next_monday, duration: 8)
@@ -1155,6 +1156,22 @@ describe WorkPackages::SetAttributesService,
end
end
end
+
+ context 'when "ignore non-working days" is changed AND "start date" and "finish date" are set to other dates' do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: monday, due_date: next_monday, ignore_non_working_days: true)
+ end
+ let(:call_attributes) { { start_date: friday, due_date: next_tuesday, ignore_non_working_days: false } }
+
+ it_behaves_like 'service call' do
+ it "updates the duration from start date and due date" do
+ expect { subject }
+ .to change { work_package.slice(:start_date, :due_date, :duration) }
+ .from(start_date: monday, due_date: next_monday, duration: 8)
+ .to(start_date: friday, due_date: next_tuesday, duration: 3)
+ end
+ end
+ end
end
end