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

[#41821] Set duration when from date or to date is nil

Christophe Bliard905dd195b149
diff --git a/app/services/work_packages/shared/all_days.rb b/app/services/work_packages/shared/all_days.rb
index fcdaeb1c1eb..4c71f7ae6fa 100644
--- a/app/services/work_packages/shared/all_days.rb
+++ b/app/services/work_packages/shared/all_days.rb
@@ -31,8 +31,16 @@ module WorkPackages
class AllDays
# Returns number of days between two dates.
def duration(from_date, to_date)
+ return no_duration if from_date.nil? || to_date.nil?
+
(from_date..to_date).count
end
+
+ private
+
+ def no_duration
+ OpenProject::FeatureDecisions.work_packages_duration_field_active? ? nil : 1
+ end
end
end
end
diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rb
index 6c772f55092..39d6518152e 100644
--- a/app/services/work_packages/shared/working_days.rb
+++ b/app/services/work_packages/shared/working_days.rb
@@ -32,11 +32,17 @@ module WorkPackages
# Returns number of working days between two dates, excluding weekend days
# and non working days.
def duration(from_date, to_date)
+ return no_duration if from_date.nil? || to_date.nil?
+
(from_date..to_date).count { working?(_1) }
end
private
+ def no_duration
+ OpenProject::FeatureDecisions.work_packages_duration_field_active? ? nil : 1
+ end
+
def working?(date)
working_week_day?(date) && working_specific_date?(date)
end
diff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rb
index a9c60ddee2e..04c7202629c 100644
--- a/spec/services/work_packages/shared/all_days_spec.rb
+++ b/spec/services/work_packages/shared/all_days_spec.rb
@@ -72,6 +72,30 @@ RSpec.describe WorkPackages::Shared::AllDays do
include_examples 'it returns duration', 365, Date.new(2022, 1, 1), Date.new(2022, 12, 31)
include_examples 'it returns duration', 365 * 2, Date.new(2022, 1, 1), Date.new(2023, 12, 31)
end
+
+ context 'without from date', with_flag: { work_packages_duration_field_active: true } do
+ it 'returns nil' do
+ expect(subject.duration(nil, sunday_2022_07_31)).to be_nil
+ end
+
+ context 'when work packages duration field is inactive', with_flag: { work_packages_duration_field_active: false } do
+ it 'returns 1' do
+ expect(subject.duration(nil, sunday_2022_07_31)).to eq(1)
+ end
+ end
+ end
+
+ context 'without to date', with_flag: { work_packages_duration_field_active: true } do
+ it 'returns nil' do
+ expect(subject.duration(sunday_2022_07_31, nil)).to be_nil
+ end
+
+ context 'when work packages duration field is inactive', with_flag: { work_packages_duration_field_active: false } do
+ it 'returns 1' do
+ expect(subject.duration(sunday_2022_07_31, nil)).to eq(1)
+ end
+ end
+ end
end
end
# rubocop:enable Naming/VariableNumber
diff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rb
index c294a2fb28f..32819726ef9 100644
--- a/spec/services/work_packages/shared/working_days_spec.rb
+++ b/spec/services/work_packages/shared/working_days_spec.rb
@@ -90,6 +90,30 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
include_examples 'it returns duration', 1, Date.new(2022, 12, 24), Date.new(2022, 12, 25)
include_examples 'it returns duration', 8, Date.new(2022, 12, 24), Date.new(2023, 1, 2)
end
+
+ context 'without from date', with_flag: { work_packages_duration_field_active: true } do
+ it 'returns nil' do
+ expect(subject.duration(nil, sunday_2022_07_31)).to be_nil
+ end
+
+ context 'when work packages duration field is inactive', with_flag: { work_packages_duration_field_active: false } do
+ it 'returns 1' do
+ expect(subject.duration(nil, sunday_2022_07_31)).to eq(1)
+ end
+ end
+ end
+
+ context 'without to date', with_flag: { work_packages_duration_field_active: true } do
+ it 'returns nil' do
+ expect(subject.duration(sunday_2022_07_31, nil)).to be_nil
+ end
+
+ context 'when work packages duration field is inactive', with_flag: { work_packages_duration_field_active: false } do
+ it 'returns 1' do
+ expect(subject.duration(sunday_2022_07_31, nil)).to eq(1)
+ end
+ end
+ end
end
end
# rubocop:enable Naming/VariableNumber