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
Update `derived_done_ratio` on update_ancestores callback
Aaron Contreras02b434edf5a4
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rbindex daf636c16b3..663081668e7 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -53,9 +53,9 @@ module WorkPackagesattribute :project_idattribute :done_ratio,- writable: ->(*) {- model.leaf? && Setting.work_package_done_ratio == 'field'- }+ writable: ->(*) do+ Setting.work_package_done_ratio == 'field'+ endattribute :derived_done_ratio,writable: falsediff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex a655759f778..6211526354c 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -117,7 +117,7 @@ class WorkPackages::UpdateAncestorsServicereturn if WorkPackage.use_status_for_done_ratio? && ancestor.status && ancestor.status.default_done_ratio# done ratio = weighted average ratio of leaves- ancestor.done_ratio = (aggregate_done_ratio(ancestor, loader) || 0).round+ ancestor.derived_done_ratio = (aggregate_done_ratio(ancestor, loader) || 0).roundend# Sets the ignore_non_working_days to true if any descendant has its value set to true.diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex d7acf7ab7d8..2d71f18350b 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -677,8 +677,6 @@ RSpec.describe WorkPackages::BaseContract doenddescribe 'percentage done' do- it_behaves_like 'a parent unwritable property', :done_ratio-context 'when % Complete inferred by status',with_settings: { work_package_done_ratio: 'status' } doit_behaves_like 'invalid if changed', :done_ratiodiff --git a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rbindex 9c17fe39236..e6d24160d44 100644--- a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb+++ b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb@@ -179,9 +179,9 @@ RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema doexpect(subject).not_to be_writable(:done_ratio)end- it 'is not writable when the work package is a parent' do+ it 'is writable when the work package is a parent' doallow(work_package).to receive(:leaf?).and_return(false)- expect(subject).not_to be_writable(:done_ratio)+ expect(subject).to be_writable(:done_ratio)endit 'is writable when the work package is a leaf' dodiff --git a/spec/services/work_packages/create_service_integration_spec.rb b/spec/services/work_packages/create_service_integration_spec.rbindex 8956765506c..83719faf6c9 100644--- a/spec/services/work_packages/create_service_integration_spec.rb+++ b/spec/services/work_packages/create_service_integration_spec.rb@@ -153,7 +153,7 @@ RSpec.describe WorkPackages::CreateService, 'integration', type: :model do# parent updatedparent.reload- expect(parent.done_ratio)+ expect(parent.derived_done_ratio).to eql attributes[:done_ratio]expect(parent.start_date).to eql attributes[:start_date]diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex 7e34e24c36e..fee7201ae30 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -68,14 +68,15 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:child) { create(:work_package, parent:, status: open_status) }shared_examples 'updates % complete of ancestors' do- it 'considers the work package as 100 % complete and sets the % complete value of the ancestors accordingly' do+ it 'considers the work package as 100 % complete and sets the ' \+ 'derived % complete value of the ancestors accordingly' doexpect doupdated_attributes = child.changes.keys.map(&:to_sym)described_class.new(user:, work_package: child).call(updated_attributes)parent.reloadend- .to change(parent, :done_ratio).from(0).to(100)+ .to change(parent, :derived_done_ratio).from(0).to(100)endend@@ -112,8 +113,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(parent)end- it 'has the expected aggregate done ratio' do- expect(subject.dependent_results.first.result.done_ratio)+ it 'has the expected derived done ratio' do+ expect(subject.dependent_results.first.result.derived_done_ratio).to eq aggregate_done_ratioend@@ -140,7 +141,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dosubject do# In the call we only use estimated_hours (instead of also adding# done_ratio) in order to test that changes in estimated hours- # trigger a recalculation of done_ration, because estimated hours+ # trigger a recalculation of done_ratio, because estimated hours# act as weights in this calculation.described_class.new(user:,@@ -340,22 +341,22 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(parent, grandparent)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the former parent' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the former parent' doexpect dosubjectparent.reloadend- .to change(parent, :done_ratio).to(sibling_done_ratio)+ .to change(parent, :derived_done_ratio).to(sibling_done_ratio).and change(parent, :derived_estimated_hours).to(parent_estimated_hours + sibling_estimated_hours).and change(parent, :derived_remaining_hours).to(sibling_remaining_hours)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the former grandparent' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the former grandparent' doexpect dosubjectgrandparent.reloadend- .to change(grandparent, :done_ratio).to(sibling_done_ratio)+ .to change(grandparent, :derived_done_ratio).to(sibling_done_ratio).and change(grandparent, :derived_estimated_hours).to(parent_estimated_hours + sibling_estimated_hours).and change(grandparent, :derived_remaining_hours).to(sibling_remaining_hours)end@@ -400,22 +401,23 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(parent, grandparent)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent' doexpect dosubjectparent.reloadend- .to change(parent, :done_ratio).to(done_ratio)+ .to change(parent, :derived_done_ratio).to(done_ratio).and change(parent, :derived_estimated_hours).to(parent_estimated_hours + estimated_hours).and change(parent, :derived_remaining_hours).to(parent_remaining_hours + remaining_hours)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the new grandparent' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours ' \+ 'of the new grandparent' doexpect dosubjectgrandparent.reloadend- .to change(grandparent, :done_ratio).to(done_ratio)+ .to change(grandparent, :derived_done_ratio).to(done_ratio).and change(grandparent, :derived_estimated_hours).to(parent_estimated_hours + estimated_hours).and change(grandparent, :derived_remaining_hours).to(parent_remaining_hours + remaining_hours)end@@ -462,7 +464,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:grandparent) docreate(:work_package,subject: 'common grandparent',- done_ratio: done_ratio / 2, # two children having [done_ratio, 0]+ derived_done_ratio: done_ratio / 2, # two children having [done_ratio, 0]derived_estimated_hours: estimated_hours,derived_remaining_hours: remaining_hours)end@@ -470,7 +472,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model docreate(:work_package,subject: 'old parent',parent: grandparent,- done_ratio:,+ derived_done_ratio: done_ratio,derived_estimated_hours: estimated_hours,derived_remaining_hours: remaining_hours)end@@ -521,32 +523,33 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(new_parent, grandparent, old_parent)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the former parent to 0' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours ' \+ 'of the former parent to 0' doexpect dosubjectold_parent.reloadend- .to change(old_parent, :done_ratio).to(0)+ .to change(old_parent, :derived_done_ratio).to(0).and change(old_parent, :derived_estimated_hours).to(nil).and change(old_parent, :derived_remaining_hours).to(nil)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent' doexpect dosubjectnew_parent.reloadend- .to change(new_parent, :done_ratio).to(new_done_ratio)+ .to change(new_parent, :derived_done_ratio).to(new_done_ratio).and change(new_parent, :derived_estimated_hours).to(new_estimated_hours).and change(new_parent, :derived_remaining_hours).to(new_remaining_hours)end- it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the grandparent' do+ it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the grandparent' doexpect dosubjectgrandparent.reloadend- .to change(grandparent, :done_ratio).to(new_done_ratio / 2) # two children having [new_done_ratio, 0] now+ .to change(grandparent, :derived_done_ratio).to(new_done_ratio / 2) # two children having [new_done_ratio, 0] now.and change(grandparent, :derived_estimated_hours).to(new_estimated_hours).and change(grandparent, :derived_remaining_hours).to(new_remaining_hours)enddiff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rbindex 122c1c52003..ee03f96046a 100644--- a/spec/services/work_packages/update_service_integration_spec.rb+++ b/spec/services/work_packages/update_service_integration_spec.rb@@ -495,7 +495,7 @@ RSpec.describe WorkPackages::UpdateService, 'integration tests', type: :model dograndparent_work_package].each do |wp|wp.reload- expect(wp.done_ratio)+ expect(wp.derived_done_ratio).to eql(calculated_ratio.to_i)end