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

[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.rb
index 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::Loader
end
def select
- ancestors.select do |ancestor|
- yield ancestor, self
- end
+ [work_package, *ancestors]
+ .reject(&:destroyed?)
+ .select do |work_package|
+ yield work_package, self
+ end
end
def descendants_of(queried_work_package)
@@ -160,7 +162,9 @@ class WorkPackages::UpdateAncestors::Loader
# (when work_package was saved/destroyed)
# Or the set parent before saving
def 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_was
work_package.parent_id_was
else
previous_change_parent_id
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 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::UpdateAncestorsService
attr_accessor :user,
- :work_package
+ :initiator_work_package
def initialize(user:, work_package:)
self.user = user
- self.work_package = work_package
+ self.initiator_work_package = work_package
end
def 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 updates
success = Journal::NotificationConfiguration.with(false) do
modified.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::UpdateAncestorsService
private
+ 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?
end
end
- 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
+ end
end
def 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}")
end
end
- 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::UpdateAncestorsService
ancestor.done_ratio = (aggregate_done_ratio(ancestor, loader) || 0).round
end
- # 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_days
end
- work_package.ignore_non_working_days = descendant_value
+ ancestor.ignore_non_working_days = descendant_value
end
##
@@ -177,7 +184,7 @@ class WorkPackages::UpdateAncestorsService
def 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)
end
def not_zero(value)
@@ -194,9 +201,8 @@ class WorkPackages::UpdateAncestorsService
work_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])
end
def 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.rb
index 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 do
shared_let(:grandgrandparent) do
- create(:work_package)
+ create(:work_package,
+ subject: 'grandgrandparent')
end
shared_let(:grandparent_sibling) do
create(:work_package,
+ subject: 'grandparent sibling',
parent: grandgrandparent)
end
shared_let(:grandparent) do
create(:work_package,
+ subject: 'grandparent',
parent: grandgrandparent)
end
shared_let(:parent) do
create(:work_package,
+ subject: 'parent',
parent: grandparent)
end
shared_let(:sibling) do
create(:work_package,
+ subject: 'sibling',
parent:)
end
- shared_let(:work_package) do
+ shared_let(:work_package, refind: true) do
create(:work_package,
+ subject: 'work package',
parent:)
end
shared_let(:child) do
create(:work_package,
+ subject: 'child',
parent: work_package)
end
@@ -91,9 +98,9 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
parent: 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' do
expect(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]
end
end
@@ -120,18 +127,31 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
let(:include_former_ancestors) { false }
- it 'iterates over the current ancestors' do
+ it 'iterates over the initiator work package and the current ancestors' do
expect(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]
end
end
context 'when removing the parent' do
let(:new_parent) { nil }
- it 'iterates over the former ancestors' do
+ it 'iterates over the initiator work package and the former ancestors' do
expect(subject.select { |ancestor| ancestor })
- .to eql [parent, grandparent, grandgrandparent]
+ .to eq [work_package, parent, grandparent, grandgrandparent]
end
end
@@ -139,18 +159,18 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
let(:new_parent) { nil }
let(:include_former_ancestors) { false }
- it 'loads nothing' do
+ it 'loads only the initiator' do
expect(subject.select { |ancestor| ancestor })
- .to be_empty
+ .to eq [work_package]
end
end
context 'when changing the parent within the same hierarchy upwards' do
let(:new_parent) { grandgrandparent }
- it 'iterates over the former ancestors' do
+ it 'iterates over the initiator and the former ancestors' do
expect(subject.select { |ancestor| ancestor })
- .to eql [parent, grandparent, grandgrandparent]
+ .to eq [work_package, parent, grandparent, grandgrandparent]
end
end
@@ -158,18 +178,18 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
let(:new_parent) { grandgrandparent }
let(:include_former_ancestors) { false }
- it 'iterates over the current ancestors' do
+ it 'iterates over the initiator and the current ancestors' do
expect(subject.select { |ancestor| ancestor })
- .to eql [grandgrandparent]
+ .to eq [work_package, grandgrandparent]
end
end
context 'when changing the parent within the same hierarchy sideways' do
let(:new_parent) { sibling }
- it 'iterates over the current ancestors' do
+ it 'iterates over the initiator and the current ancestors' do
expect(subject.select { |ancestor| ancestor })
- .to eql [sibling, parent, grandparent, grandgrandparent]
+ .to eq [work_package, sibling, parent, grandparent, grandgrandparent]
end
end
@@ -177,18 +197,18 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
let(:new_parent) { sibling }
let(:include_former_ancestors) { false }
- it 'iterates over the current ancestors' do
+ it 'iterates over the initiator and the current ancestors' do
expect(subject.select { |ancestor| ancestor })
- .to eql [sibling, parent, grandparent, grandgrandparent]
+ .to eq [work_package, sibling, parent, grandparent, grandgrandparent]
end
end
context 'when changing the parent within the same hierarchy sideways but to a different level' do
let(: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' do
expect(subject.select { |ancestor| ancestor })
- .to eql [grandparent_sibling, parent, grandparent, grandgrandparent]
+ .to eq [work_package, grandparent_sibling, parent, grandparent, grandgrandparent]
end
end
@@ -196,9 +216,9 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
let(: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' do
expect(subject.select { |ancestor| ancestor })
- .to eql [grandparent_sibling, grandgrandparent]
+ .to eq [work_package, grandparent_sibling, grandgrandparent]
end
end
end
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rb
index 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 do
end
end
- 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 do
end
describe '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
+ end
context 'for the new ancestors chain' do
shared_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])
end
end
subject(:call_result) do
@@ -506,13 +527,29 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
end
- 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
+ end
context 'with no remaining hours' do
include_context('when called with children', children_remaining_hours: [nil, nil, nil])