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
Keep progress attributes consistent when updated
Christophe Bliarde52825db6bed
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex 422e2c1081c..a4edd1cc54e 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -62,9 +62,8 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributesendshift_dates_to_soonest_working_daysupdate_duration- update_done_ratio- update_remaining_hoursupdate_derivable+ update_progress_attributesupdate_project_dependent_attributesreassign_invalid_status_if_type_changedset_templated_description@@ -284,6 +283,32 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributeswork_package.duration = 1 if work_package.milestone?end+ def update_progress_attributes+ if WorkPackage.use_status_for_done_ratio?+ update_done_ratio+ update_remaining_hours+ elsif only_percent_complete_initially_set?+ update_remaining_hours_from_percent_complete+ else+ update_remaining_hours+ update_done_ratio+ end+ end++ def only_percent_complete_initially_set?+ return false if work_package.done_ratio.nil?+ return false if work_package.remaining_hours.present?++ work_package.estimated_hours_changed? && work_package.estimated_hours.present?+ end++ def only_remaining_work_initially_set?+ return false if work_package.remaining_hours.nil?+ return false if work_package.done_ratio.present?++ work_package.estimated_hours_was.nil?+ end+# Compute and update +done_ratio+ if its dependent attributes are being modified.# The dependent attributes for +done_ratio+ are# - +remaining_hours+@@ -293,9 +318,9 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes# considered nil.def update_done_ratioif WorkPackage.use_status_for_done_ratio?- return unless model.status_id_changed?+ return unless work_package.status_id_changed?- model.done_ratio = model.status.default_done_ratio+ work_package.done_ratio = work_package.status.default_done_ratioelsereturn unless work_package.remaining_hours_changed? || work_package.estimated_hours_changed?@@ -307,6 +332,10 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributesendend+ def update_remaining_hours_from_percent_complete+ work_package.remaining_hours = remaining_hours_from_done_ratio_and_estimated_hours+ end+def done_ratio_dependent_attribute_unset?work_package.remaining_hours.nil? || work_package.estimated_hours.nil?end@@ -322,24 +351,30 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes# on the computation of it derived from the status's default done ratio# and the estimated hours. If the estimated hours are unset, then also# unset the remaining hours.+ # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexitydef update_remaining_hours- if WorkPackage.use_status_for_done_ratio? &&- model.status &&- model.status.default_done_ratio- model.remaining_hours = if model.estimated_hours- remaining_hours_from_done_ratio_and_estimated_hours- end+ if WorkPackage.use_status_for_done_ratio?+ work_package.remaining_hours = remaining_hours_from_done_ratio_and_estimated_hourselsif WorkPackage.use_field_for_done_ratio? &&- model.estimated_hours_changed? &&- model.estimated_hours.nil?- model.remaining_hours = nil+ work_package.estimated_hours_changed?+ return if work_package.remaining_hours_changed?+ return if only_remaining_work_initially_set?++ if work_package.estimated_hours.nil? || work_package.remaining_hours.nil?+ work_package.remaining_hours = work_package.estimated_hours+ else+ delta = work_package.estimated_hours - work_package.estimated_hours_was+ work_package.remaining_hours = (work_package.remaining_hours + delta).clamp(0.0, work_package.estimated_hours)+ endendend+ # rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexitydef remaining_hours_from_done_ratio_and_estimated_hours- ((((model.done_ratio.to_f / 100) * model.estimated_hours) \- - model.estimated_hours) \- * -1).abs+ return nil if work_package.done_ratio.nil? || work_package.estimated_hours.nil?++ completed_work = work_package.estimated_hours * work_package.done_ratio / 100.0+ work_package.estimated_hours - completed_workenddef set_version_to_nildiff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex 529b51dcc68..4464a40b12f 100644--- a/spec/services/work_packages/set_attributes_service_spec.rb+++ b/spec/services/work_packages/set_attributes_service_spec.rb@@ -93,9 +93,7 @@ RSpec.describe WorkPackages::SetAttributesService,subject- expected_attributes.each do |attribute, key|- expect(work_package.send(attribute)).to eql key- end+ expect(work_package).to have_attributes(expected_attributes)endit "does not persist the work_package" do@@ -184,7 +182,7 @@ RSpec.describe WorkPackages::SetAttributesService,description: "recomputes remaining work according to the % complete value of the new status"end- context "when another status without any default % complete value is set", skip: "TODO: not implemented yet" do+ context "when another status without any default % complete value is set" dolet(:call_attributes) { { status: status_no_pct_complete } }let(:expected_attributes) { { remaining_hours: nil } }@@ -279,7 +277,7 @@ RSpec.describe WorkPackages::SetAttributesService,it_behaves_like "service call", description: "unsets % complete"end- context "when work is increased", skip: "TODO: not implemented yet" do+ context "when work is increased" do# work changed by +10hlet(:call_attributes) { { estimated_hours: 10.0 + 10.0 } }let(:expected_attributes) do@@ -290,7 +288,7 @@ RSpec.describe WorkPackages::SetAttributesService,description: "remaining work is increased by the same amount, and % complete is updated accordingly"end- context "when work is decreased", skip: "TODO: not implemented yet" do+ context "when work is decreased" do# work changed by -2hlet(:call_attributes) { { estimated_hours: 10.0 - 2.0 } }let(:expected_attributes) do@@ -301,7 +299,7 @@ RSpec.describe WorkPackages::SetAttributesService,description: "remaining work is decreased by the same amount, and % complete is updated accordingly"end- context "when work is decreased below remaining work value", skip: "TODO: not implemented yet" do+ context "when work is decreased below remaining work value" do# work changed by -8hlet(:call_attributes) { { estimated_hours: 10.0 - 8.0 } }let(:expected_attributes) do@@ -330,12 +328,45 @@ RSpec.describe WorkPackages::SetAttributesService,context "when both work and remaining work are changed" dolet(:call_attributes) { { estimated_hours: 20, remaining_hours: 2 } }- let(:expected_attributes) { { done_ratio: 90 } }+ let(:expected_attributes) { call_attributes.merge(done_ratio: 90) }it_behaves_like "service call", description: "updates % complete accordingly"endend+ context "given a work package with work being set, and remaining work and % complete being unset" do+ before do+ work_package.estimated_hours = 10+ work_package.remaining_hours = nil+ work_package.done_ratio = nil+ work_package.send(:clear_changes_information)+ end++ context "when work is changed" do+ let(:call_attributes) { { estimated_hours: 20.0 } }+ let(:expected_attributes) { { remaining_hours: 20.0, done_ratio: 0 } }++ it_behaves_like "service call", description: "remaining work is set to the same value and % complete is set to 0%"+ end+ end++ context "given a work package with remaining work being set, and work and % complete being unset" do+ before do+ work_package.estimated_hours = nil+ work_package.remaining_hours = 6.0+ work_package.done_ratio = nil+ work_package.send(:clear_changes_information)+ end++ context "when work is set" do+ let(:call_attributes) { { estimated_hours: 10.0 } }+ let(:expected_attributes) { { remaining_hours: 6.0, done_ratio: 40 } }++ it_behaves_like "service call",+ description: "remaining work is kept to the same value and % complete is updated accordingly"+ end+ end+context "given a work package with work and remaining work unset, and % complete being set" dobefore dowork_package.estimated_hours = nil@@ -344,7 +375,7 @@ RSpec.describe WorkPackages::SetAttributesService,work_package.send(:clear_changes_information)end- context "when work is set", skip: "TODO: not implemented yet" do+ context "when work is set" dolet(:call_attributes) { { estimated_hours: 10.0 } }let(:expected_attributes) do{ remaining_hours: 4.0, done_ratio: 60 }@@ -362,7 +393,7 @@ RSpec.describe WorkPackages::SetAttributesService,work_package.send(:clear_changes_information)end- context "when work is set", skip: "TODO: not implemented yet" do+ context "when work is set" dolet(:call_attributes) { { estimated_hours: 10.0 } }let(:expected_attributes) do{ remaining_hours: 10.0, done_ratio: 0 }