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
[50960] Derive remaining hours from summing self and descendants
Christophe Bliard7bc933a0e4fb
diff --git a/app/services/work_packages/update_ancestors/loader.rb b/app/services/work_packages/update_ancestors/loader.rbindex ce4aa638a27..d7974858e2f 100644--- a/app/services/work_packages/update_ancestors/loader.rb+++ b/app/services/work_packages/update_ancestors/loader.rb@@ -31,9 +31,11 @@ class WorkPackages::UpdateAncestors::Loaderenddef select- ancestors.select do |ancestor|- yield ancestor, self- end+ [work_package, *ancestors]+ .reject(&:destroyed?)+ .select do |work_package|+ yield work_package, self+ endenddef descendants_of(queried_work_package)@@ -160,7 +162,9 @@ class WorkPackages::UpdateAncestors::Loader# (when work_package was saved/destroyed)# Or the set parent before savingdef previous_parent_id- if work_package.parent_id.nil? && work_package.parent_id_was+ if work_package.parent_id && work_package.destroyed?+ work_package.parent_id+ elsif work_package.parent_id.nil? && work_package.parent_id_waswork_package.parent_id_waselseprevious_change_parent_iddiff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex da2a256227e..f95842a2b20 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -28,26 +28,28 @@class WorkPackages::UpdateAncestorsServiceattr_accessor :user,- :work_package+ :initiator_work_packagedef initialize(user:, work_package:)self.user = user- self.work_package = work_package+ self.initiator_work_package = work_packageenddef call(attributes)modified = update_current_and_former_ancestors(attributes)- set_journal_note(modified)+ modified_ancestors = modified.reject { |wp| initiator?(wp) }++ set_journal_note(modified_ancestors)# Do not send notification for parent updatessuccess = Journal::NotificationConfiguration.with(false) domodified.all? { |wp| wp.save(validate: false) }end- result = ServiceResult.new(success:, result: work_package)+ result = ServiceResult.new(success:, result: initiator_work_package)- modified.each do |wp|+ modified_ancestors.each do |wp|result.add_dependent!(ServiceResult.new(success: !wp.changed?, result: wp))end@@ -56,46 +58,50 @@ class WorkPackages::UpdateAncestorsServiceprivate+ def initiator?(work_package)+ work_package == initiator_work_package+ end+def update_current_and_former_ancestors(attributes)include_former_ancestors = attributes.intersect?(%i[parent_id parent])WorkPackages::UpdateAncestors::Loader- .new(work_package, include_former_ancestors)+ .new(initiator_work_package, include_former_ancestors).select do |ancestor, loader|- inherit_attributes(ancestor, loader, attributes)+ derive_attributes(ancestor, loader, attributes)ancestor.changed?endend- def inherit_attributes(ancestor, loader, attributes)- return unless attributes_justify_inheritance?(attributes)-- # Estimated hours need to be calculated before the done_ratio below.- # The aggregation only depends on estimated hours.- derive_estimated_hours(ancestor, loader) if inherit?(attributes, :estimated_hours)-- # Progress (done_ratio or also: percentDone) depends on both- # the completion of sub-WPs, as well as the estimated hours- # as a weight factor. So changes in estimated hours also have- # to trigger a recalculation of done_ratio.- inherit_done_ratio(ancestor, loader) if inherit?(attributes, :done_ratio) || inherit?(attributes, :estimated_hours)-- derive_remaining_hours(ancestor, loader) if inherit?(attributes, :remaining_hours)-- inherit_ignore_non_working_days(ancestor, loader) if inherit?(attributes, :ignore_non_working_days)- end-- def inherit?(attributes, attribute)- ([attribute, :parent, :parent_id] & attributes).any?+ def derive_attributes(work_package, loader, attributes)+ return unless modified_attributes_justify_derivation?(attributes)++ {+ # Estimated hours need to be calculated before the done_ratio below.+ # The aggregation only depends on estimated hours.+ %i[estimated_hours] => :derive_estimated_hours,+ # Progress (done_ratio or also: percentDone) depends on both+ # the completion of sub-WPs, as well as the estimated hours+ # as a weight factor. So changes in estimated hours also have+ # to trigger a recalculation of done_ratio.+ %i[done_ratio estimated_hours] => :derive_done_ratio,+ %i[remaining_hours] => :derive_remaining_hours,+ %i[ignore_non_working_days] => :derive_ignore_non_working_days+ }.each do |derivative_attributes, method|+ if attributes.intersect?(derivative_attributes + %i[parent parent_id])+ send(method, work_package, loader)+ end+ endenddef set_journal_note(work_packages)work_packages.each do |wp|- wp.journal_notes = I18n.t('work_package.updated_automatically_by_child_changes', child: "##{work_package.id}")+ wp.journal_notes = I18n.t('work_package.updated_automatically_by_child_changes', child: "##{initiator_work_package.id}")endend- def inherit_done_ratio(ancestor, loader)+ def derive_done_ratio(ancestor, loader)+ return if initiator?(ancestor)return if WorkPackage.done_ratio_disabled?return if WorkPackage.use_status_for_done_ratio? && ancestor.status && ancestor.status.default_done_ratio@@ -104,20 +110,21 @@ class WorkPackages::UpdateAncestorsServiceancestor.done_ratio = (aggregate_done_ratio(ancestor, loader) || 0).roundend- # Sets the ignore_non_working_days to true if any ancestor has its value set to true.+ # Sets the ignore_non_working_days to true if any descendant has its value set to true.# If there is no value returned from the descendants, that means that the work package in# question no longer has a descendant. But since we are in the service going up the ancestor chain,# such a work package is the former parent. The property of such a work package is reset to `false`.- def inherit_ignore_non_working_days(work_package, loader)- return if work_package.schedule_manually+ def derive_ignore_non_working_days(ancestor, loader)+ return if initiator?(ancestor)+ return if ancestor.schedule_manually- descendant_value = ignore_non_working_days_of_descendants(work_package, loader)+ descendant_value = ignore_non_working_days_of_descendants(ancestor, loader)if descendant_value.nil?- descendant_value = work_package.ignore_non_working_days+ descendant_value = ancestor.ignore_non_working_daysend- work_package.ignore_non_working_days = descendant_value+ ancestor.ignore_non_working_days = descendant_valueend##@@ -177,7 +184,7 @@ class WorkPackages::UpdateAncestorsServicedef derive_remaining_hours(work_package, loader)descendants = loader.descendants_of(work_package)- work_package.derived_remaining_hours = not_zero(all_remaining_hours(descendants).sum.to_f)+ work_package.derived_remaining_hours = not_zero(all_remaining_hours([work_package] + descendants).sum.to_f)enddef not_zero(value)@@ -194,9 +201,8 @@ class WorkPackages::UpdateAncestorsServicework_packages.map(&:remaining_hours).reject { |hours| hours.to_f.zero? }end- def attributes_justify_inheritance?(attributes)- %i[estimated_hours done_ratio parent parent_id status status_id ignore_non_working_days- remaining_hours].intersect?(attributes)+ def modified_attributes_justify_derivation?(attributes)+ attributes.intersect?(%i[estimated_hours done_ratio parent parent_id ignore_non_working_days remaining_hours])enddef ignore_non_working_days_of_descendants(ancestor, loader)diff --git a/spec/services/work_packages/update_ancestors/loader_spec.rb b/spec/services/work_packages/update_ancestors/loader_spec.rbindex 7243c76bf24..ef7137bc9f9 100644--- a/spec/services/work_packages/update_ancestors/loader_spec.rb+++ b/spec/services/work_packages/update_ancestors/loader_spec.rb@@ -28,30 +28,37 @@ require 'spec_helper'RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model doshared_let(:grandgrandparent) do- create(:work_package)+ create(:work_package,+ subject: 'grandgrandparent')endshared_let(:grandparent_sibling) docreate(:work_package,+ subject: 'grandparent sibling',parent: grandgrandparent)endshared_let(:grandparent) docreate(:work_package,+ subject: 'grandparent',parent: grandgrandparent)endshared_let(:parent) docreate(:work_package,+ subject: 'parent',parent: grandparent)endshared_let(:sibling) docreate(:work_package,+ subject: 'sibling',parent:)end- shared_let(:work_package) do+ shared_let(:work_package, refind: true) docreate(:work_package,+ subject: 'work package',parent:)endshared_let(:child) docreate(:work_package,+ subject: 'child',parent: work_package)end@@ -91,9 +98,9 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model doparent: new_parent)end- it 'iterates over both current and former ancestors' do+ it 'iterates over the initiator work package, and both its current and former ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [new_parent, new_grandparent, new_grandgrandparent, parent, grandparent, grandgrandparent]+ .to eq [work_package, new_parent, new_grandparent, new_grandgrandparent, parent, grandparent, grandgrandparent]endend@@ -120,18 +127,31 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model dolet(:include_former_ancestors) { false }- it 'iterates over the current ancestors' do+ it 'iterates over the initiator work package and the current ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [new_parent, new_grandparent, new_grandgrandparent]+ .to eq [work_package, new_parent, new_grandparent, new_grandgrandparent]+ end+ end++ context 'when destroying the initiator' do+ subject do+ work_package.destroy!++ instance+ end++ it 'iterates over the former ancestors' do+ expect(subject.select { |ancestor| ancestor })+ .to eq [parent, grandparent, grandgrandparent]endendcontext 'when removing the parent' dolet(:new_parent) { nil }- it 'iterates over the former ancestors' do+ it 'iterates over the initiator work package and the former ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [parent, grandparent, grandgrandparent]+ .to eq [work_package, parent, grandparent, grandgrandparent]endend@@ -139,18 +159,18 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model dolet(:new_parent) { nil }let(:include_former_ancestors) { false }- it 'loads nothing' do+ it 'loads only the initiator' doexpect(subject.select { |ancestor| ancestor })- .to be_empty+ .to eq [work_package]endendcontext 'when changing the parent within the same hierarchy upwards' dolet(:new_parent) { grandgrandparent }- it 'iterates over the former ancestors' do+ it 'iterates over the initiator and the former ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [parent, grandparent, grandgrandparent]+ .to eq [work_package, parent, grandparent, grandgrandparent]endend@@ -158,18 +178,18 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model dolet(:new_parent) { grandgrandparent }let(:include_former_ancestors) { false }- it 'iterates over the current ancestors' do+ it 'iterates over the initiator and the current ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [grandgrandparent]+ .to eq [work_package, grandgrandparent]endendcontext 'when changing the parent within the same hierarchy sideways' dolet(:new_parent) { sibling }- it 'iterates over the current ancestors' do+ it 'iterates over the initiator and the current ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [sibling, parent, grandparent, grandgrandparent]+ .to eq [work_package, sibling, parent, grandparent, grandgrandparent]endend@@ -177,18 +197,18 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model dolet(:new_parent) { sibling }let(:include_former_ancestors) { false }- it 'iterates over the current ancestors' do+ it 'iterates over the initiator and the current ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [sibling, parent, grandparent, grandgrandparent]+ .to eq [work_package, sibling, parent, grandparent, grandgrandparent]endendcontext 'when changing the parent within the same hierarchy sideways but to a different level' dolet(:new_parent) { grandparent_sibling }- it 'iterates over the former and the current ancestors' do+ it 'iterates over the initiator and its former and current ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [grandparent_sibling, parent, grandparent, grandgrandparent]+ .to eq [work_package, grandparent_sibling, parent, grandparent, grandgrandparent]endend@@ -196,9 +216,9 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model dolet(:new_parent) { grandparent_sibling }let(:include_former_ancestors) { false }- it 'iterates over the former and the current ancestors' do+ it 'iterates over the initiator and its former and current ancestors' doexpect(subject.select { |ancestor| ancestor })- .to eql [grandparent_sibling, grandgrandparent]+ .to eq [work_package, grandparent_sibling, grandgrandparent]endendenddiff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex f70c54f789a..18caa8b27d3 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -74,6 +74,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do(statuses.size - 1).downto(0).map do |i|create(:work_package,parent:,+ subject: "child #{i}",status: statuses[i] == :open ? open_status : closed_status,estimated_hours: estimated_hours[i],done_ratio: done_ratios[i],@@ -81,7 +82,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendend- shared_let(:parent) { create(:work_package, status: open_status) }+ shared_let(:parent) { create(:work_package, subject: 'parent', status: open_status) }subject do# In the call we only use estimated_hours (instead of also adding@@ -474,16 +475,36 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doenddescribe 'remaining_hours propagation' do- shared_let(:parent) { create(:work_package, status: open_status) }+ shared_let(:parent) { create(:work_package, subject: 'parent', status: open_status) }++ context 'when setting remaining hours of a work package' do+ before do+ parent.remaining_hours = 2.0+ end++ subject(:call_result) do+ described_class.new(user:, work_package: parent)+ .call(%i(remaining_hours))+ end++ it 'sets its derived value to the same value' do+ expect { call_result }+ .to change(parent, :derived_remaining_hours).from(nil).to(2.0)+ expect(call_result).to be_success+ expect(call_result.dependent_results).to be_empty+ end+ endcontext 'for the new ancestors chain' doshared_context 'when called with children' do |children_remaining_hours:|let(:children) do(children_remaining_hours.size - 1).downto(0).map do |i|create(:work_package,+ subject: "child #{i}",parent:,status: open_status,- remaining_hours: children_remaining_hours[i])+ remaining_hours: children_remaining_hours[i],+ derived_remaining_hours: children_remaining_hours[i])endendsubject(:call_result) do@@ -506,13 +527,29 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendend- include_examples 'derived remaining hours',- children_remaining_hours: [0.0, 2.0, nil],- expected_derived_remaining_hours: 2.0+ context 'with parent having no remaining hours' do+ include_examples 'derived remaining hours',+ children_remaining_hours: [0.0, 2.0, nil],+ expected_derived_remaining_hours: 2.0- include_examples 'derived remaining hours',- children_remaining_hours: [1, 2, 5, 42],- expected_derived_remaining_hours: 50+ include_examples 'derived remaining hours',+ children_remaining_hours: [1, 2, 5, 42],+ expected_derived_remaining_hours: 50+ end++ context 'with parent having 2.0 remaining hours' do+ before do+ parent.update(remaining_hours: 2.0)+ end++ include_examples 'derived remaining hours',+ children_remaining_hours: [0.0, 2.0, nil],+ expected_derived_remaining_hours: 4.0++ include_examples 'derived remaining hours',+ children_remaining_hours: [1, 2, 5, 42],+ expected_derived_remaining_hours: 52+ endcontext 'with no remaining hours' doinclude_context('when called with children', children_remaining_hours: [nil, nil, nil])