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

Keep duration when ignore_non_working_days is changed

Christophe Bliard5cd18fac44a5
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 41cb8bad17a..11cde74743e 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -68,36 +68,49 @@ 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.
+ # Returns a field derivable by the presence of the two others, or +nil+
+ # if none was found.
#
- # A field is derivable if it has not been set explicitly while the other two
- # fields are set. Matching is done in the order :duration, :due_date,
- # :start_date. The first one to match is returned.
+ # 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.
def derivable_attribute_by_others_presence
- if attribute_not_set_in_params?(:duration) && all_present?(:start_date, :due_date)
- :duration
- elsif attribute_not_set_in_params?(:due_date) && all_present?(:start_date, :duration)
- :due_date
- elsif attribute_not_set_in_params?(:start_date) && all_present?(:due_date, :duration)
- :start_date
- end
+ fields = %i[duration due_date start_date]
+ fields = fields.without(:duration) if work_package.ignore_non_working_days_changed?
+ fields.find { |field| derivable_by_others_presence?(field) }
end
- # Returns a field derivable by the absence of one of the two others.
+ # Returns true if given +field+ is derivable from the presence of the two
+ # others.
#
# A field is derivable if it has not been set explicitly while the other two
- # fields have one set and one nil. Matching is done in the order :duration, :due_date,
- # :start_date. The first one to match is returned.
+ # fields are set.
+ def derivable_by_others_presence?(field)
+ others = %i[start_date due_date duration].without(field)
+ attribute_not_set_in_params?(field) && all_present?(*others)
+ end
+
+ # Returns a field derivable by the absence of one of the two others, or +nil+
+ # if none was found.
#
- # Note: if both other fields are nil, then the field is not derivable
+ # Matching is done in the order :duration, :due_date, :start_date. The first
+ # one to match is returned.
def derivable_attribute_by_others_absence
- if attribute_not_set_in_params?(:duration) && only_one_present?(:start_date, :due_date)
- :duration
- elsif attribute_not_set_in_params?(:due_date) && only_one_present?(:start_date, :duration)
- :due_date
- elsif attribute_not_set_in_params?(:start_date) && only_one_present?(:due_date, :duration)
- :start_date
- end
+ %i[duration due_date start_date].find { |field| derivable_by_others_absence?(field) }
+ end
+
+ # Returns true if given +field+ is derivable from the absence of one of the
+ # two others.
+ #
+ # A field is derivable if it has not been set explicitly while the other two
+ # fields have one set and one nil.
+ #
+ # Note: if both other fields are nil, then the field is not derivable
+ def derivable_by_others_absence?(field)
+ others = %i[start_date due_date duration].without(field)
+ attribute_not_set_in_params?(field) && only_one_present?(*others)
end
def attribute_not_set_in_params?(field)
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 6ebb5193977..854468629dd 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1071,6 +1071,90 @@ describe WorkPackages::SetAttributesService,
end
end
end
+
+ context 'when duration and end_date both change' do
+ 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 } }
+
+ it_behaves_like 'service call' do
+ it "updates the start date and skips the non-working days" do
+ expect { subject }
+ .to change(work_package, :start_date)
+ .from(monday)
+ .to(monday.next_occurring(:thursday))
+ end
+ end
+ end
+
+ context 'when "ignore non-working days" is switched to true' do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: monday, due_date: next_monday, ignore_non_working_days: false)
+ end
+ let(:call_attributes) { { ignore_non_working_days: true } }
+
+ it_behaves_like 'service call' do
+ it "updates the due date from start date and duration to include 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: 6)
+ .to(start_date: monday, due_date: next_monday - 2.days, duration: 6)
+ end
+ end
+ end
+
+ context 'when "ignore non-working days" is switched to false' 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) { { ignore_non_working_days: false } }
+
+ it_behaves_like 'service call' do
+ it "updates the due date from start 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)
+ .to(start_date: monday, due_date: next_monday + 2.days, duration: 8)
+ end
+ end
+ end
+
+ context 'when "ignore non-working days" is changed AND "finish date" is cleared' 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) { { ignore_non_working_days: false, due_date: nil } }
+
+ 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)
+ .to(start_date: monday, due_date: nil, duration: nil)
+ end
+ end
+ end
+
+ context 'when "ignore non-working days" is changed AND "finish date" is set to another date' 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) { { ignore_non_working_days: false, due_date: wednesday } }
+
+ 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)
+ .to(start_date: wednesday - 9.days, due_date: wednesday, duration: 8)
+ end
+ end
+ end
end
end