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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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::SetAttributesdef update_durationreturn 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)+ endenddef set_version_to_nildiff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rbindex b76dab88e13..20a3feecb69 100644--- a/spec/factories/work_package_factory.rb+++ b/spec/factories/work_package_factory.rb@@ -42,6 +42,10 @@ FactoryBot.define doupdated_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.typediff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex 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 } dolet(:user) { build_stubbed(:user) }let(:project) dop = build_stubbed(:project)@@ -47,6 +49,7 @@ describe WorkPackages::SetAttributesService, type: :model doWorkPackage.newendlet(: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.todayend- it "sets the duration to 1" do+ it "sets the duration to nil" dosubjectexpect(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+ endendendend@@ -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" dosubjectexpect(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+ endendendend@@ -724,7 +753,10 @@ describe WorkPackages::SetAttributesService, type: :model doendcontext '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)+ endlet(: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" dosubjectexpect(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+ endendendendcontext '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)+ endlet(:call_attributes) { { due_date: nil } }let(:attributes) { {} }@@ -772,11 +818,22 @@ describe WorkPackages::SetAttributesService, type: :model do.to be_nilend- it "sets the duration to 1" do+ it "sets the duration to nil" dosubjectexpect(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+ endendendend