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

Set duration to nil when start/due dates are removed

Dombi Attilaecb08e6fc4ff
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 232cbf5065a..8018ff96b77 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -182,7 +182,12 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def update_duration
return unless date_changed_but_not_duration?
- work_package.duration = WorkPackages::Shared::Days.for(work_package).duration(work_package.start_date, work_package.due_date)
+ work_package.duration =
+ if work_package.milestone?
+ 1
+ else
+ WorkPackages::Shared::Days.for(work_package).duration(work_package.start_date, work_package.due_date)
+ end
end
def set_version_to_nil
diff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rb
index b76dab88e13..20a3feecb69 100644
--- a/spec/factories/work_package_factory.rb
+++ b/spec/factories/work_package_factory.rb
@@ -42,6 +42,10 @@ FactoryBot.define do
updated_at { Time.zone.now }
duration { WorkPackages::Shared::Days.for(self).duration(start_date&.to_date, due_date&.to_date) }
+ trait :is_milestone do
+ type factory: :type_milestone
+ end
+
callback(:after_build) do |work_package, evaluator|
work_package.type = work_package.project.types.first unless work_package.type
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 50b17ca0b18..3edd8ca3339 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -28,7 +28,9 @@
require 'spec_helper'
-describe WorkPackages::SetAttributesService, type: :model do
+describe WorkPackages::SetAttributesService,
+ type: :model,
+ with_flag: { work_packages_duration_field_active: true } do
let(:user) { build_stubbed(:user) }
let(:project) do
p = build_stubbed(:project)
@@ -47,6 +49,7 @@ describe WorkPackages::SetAttributesService, type: :model do
WorkPackage.new
end
let(:initial_type) { build_stubbed(:type) }
+ let(:milestone_type) { build_stubbed(:type_milestone) }
let(:statuses) { [] }
let(:contract_class) { WorkPackages::UpdateContract }
let(:mock_contract) do
@@ -575,11 +578,24 @@ describe WorkPackages::SetAttributesService, type: :model do
.to eql Time.zone.today
end
- it "sets the duration to 1" do
+ it "sets the duration to nil" do
subject
expect(work_package.duration)
- .to eq 1
+ .to be_nil
+ end
+
+ context 'when the work package type is milestone' do
+ before do
+ work_package.type = milestone_type
+ end
+
+ it "sets the duration to 1" do
+ subject
+
+ expect(work_package.duration)
+ .to eq 1
+ end
end
end
end
@@ -598,11 +614,24 @@ describe WorkPackages::SetAttributesService, type: :model do
.to eq(Time.zone.today + 1.day)
end
- it "sets the duration to 1" do
+ it "sets the duration to nil" do
subject
expect(work_package.duration)
- .to eq 1
+ .to be_nil
+ end
+
+ context 'when the work package type is milestone' do
+ before do
+ work_package.type = milestone_type
+ end
+
+ it "sets the duration to 1" do
+ subject
+
+ expect(work_package.duration)
+ .to eq 1
+ end
end
end
end
@@ -724,7 +753,10 @@ describe WorkPackages::SetAttributesService, type: :model do
end
context 'with start date nilled' do
- let(:work_package) { build_stubbed(:work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }
+ let(:traits) { [] }
+ let(:work_package) do
+ build_stubbed(:work_package, *traits, start_date: Time.zone.today, due_date: Time.zone.today + 5.days)
+ end
let(:call_attributes) { { start_date: nil } }
let(:attributes) { {} }
@@ -743,17 +775,31 @@ describe WorkPackages::SetAttributesService, type: :model do
.to eq(Time.zone.today + 5.days)
end
- it "sets the duration to 1" do
+ it "sets the duration to nil" do
subject
expect(work_package.duration)
- .to eq 1
+ .to be_nil
+ end
+
+ context 'when the work package type is milestone' do
+ let(:traits) { [:is_milestone] }
+
+ it "sets the duration to 1" do
+ subject
+
+ expect(work_package.duration)
+ .to eq 1
+ end
end
end
end
context 'with due date nilled' do
- let(:work_package) { build_stubbed(:work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }
+ let(:traits) { [] }
+ let(:work_package) do
+ build_stubbed(:work_package, *traits, start_date: Time.zone.today, due_date: Time.zone.today + 5.days)
+ end
let(:call_attributes) { { due_date: nil } }
let(:attributes) { {} }
@@ -772,11 +818,22 @@ describe WorkPackages::SetAttributesService, type: :model do
.to be_nil
end
- it "sets the duration to 1" do
+ it "sets the duration to nil" do
subject
expect(work_package.duration)
- .to eq 1
+ .to be_nil
+ end
+
+ context 'when the work package type is milestone' do
+ let(:traits) { [:is_milestone] }
+
+ it "sets the duration to 1" do
+ subject
+
+ expect(work_package.duration)
+ .to eq 1
+ end
end
end
end