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

[49409] Exclude work packages with excluded status from totals computation

Christophe Bliard2563d84e9207
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 6b6450db188..828ddc2d26e 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -293,6 +293,10 @@ class WorkPackage < ApplicationRecord
end
alias_method :is_milestone?, :milestone?
+ def included_in_totals_calculation?
+ !status.excluded_from_totals
+ end
+
def done_ratio
if WorkPackage.use_status_for_done_ratio? && status && status.default_done_ratio
status.default_done_ratio
diff --git a/app/services/work_packages/update_ancestors/loader.rb b/app/services/work_packages/update_ancestors/loader.rb
index 21eda6038a1..3e9646e5f99 100644
--- a/app/services/work_packages/update_ancestors/loader.rb
+++ b/app/services/work_packages/update_ancestors/loader.rb
@@ -25,6 +25,22 @@
# See COPYRIGHT and LICENSE files for more details.
class WorkPackages::UpdateAncestors::Loader
+ DESCENDANT_ATTRIBUTES = {
+ id: "id",
+ parent_id: "parent_id",
+ estimated_hours: "estimated_hours",
+ remaining_hours: "remaining_hours",
+ status_excluded_from_totals: "statuses.excluded_from_totals",
+ schedule_manually: "schedule_manually",
+ ignore_non_working_days: "ignore_non_working_days"
+ }.freeze
+
+ WorkPackageLikeStruct = Data.define(*DESCENDANT_ATTRIBUTES.keys) do
+ def included_in_totals_calculation?
+ !status_excluded_from_totals
+ end
+ end
+
def initialize(work_package, include_former_ancestors)
self.work_package = work_package
self.include_former_ancestors = include_former_ancestors
@@ -101,10 +117,10 @@ class WorkPackages::UpdateAncestors::Loader
.descendants
.where.not(id: queried_work_package.id)
- attributes = selected_descendants_attributes
scope
- .pluck(*attributes)
- .map { |p| WorkPackageLikeStruct.new(**attributes.zip(p).to_h) }
+ .left_joins(:status)
+ .pluck(*DESCENDANT_ATTRIBUTES.values)
+ .map { |p| WorkPackageLikeStruct.new(**DESCENDANT_ATTRIBUTES.keys.zip(p).to_h) }
end
# Returns the current ancestors sorted by distance (called generations in the table)
@@ -124,11 +140,6 @@ class WorkPackages::UpdateAncestors::Loader
end
end
- def selected_descendants_attributes
- # By having the id in here, we can avoid DISTINCT queries squashing duplicate values
- %i[id parent_id estimated_hours remaining_hours schedule_manually ignore_non_working_days]
- end
-
##
# Get the previous parent ID
# This could either be +parent_id_was+ if parent was changed
@@ -151,8 +162,4 @@ class WorkPackages::UpdateAncestors::Loader
previous_parent_changes&.first
end
-
- WorkPackageLikeStruct = Data.define(:id, :parent_id,
- :estimated_hours, :remaining_hours,
- :schedule_manually, :ignore_non_working_days)
end
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 7627840b488..0ced628eac7 100644
--- a/app/services/work_packages/update_ancestors_service.rb
+++ b/app/services/work_packages/update_ancestors_service.rb
@@ -99,7 +99,7 @@ class WorkPackages::UpdateAncestorsService
# or the derived remaining hours, depending on the % Complete mode
# currently active.
#
- %i[estimated_hours remaining_hours] => :derive_total_estimated_and_remaining_hours,
+ %i[estimated_hours remaining_hours status status_id] => :derive_total_estimated_and_remaining_hours,
%i[estimated_hours remaining_hours done_ratio status status_id] => :derive_done_ratio,
%i[ignore_non_working_days] => :derive_ignore_non_working_days
}.each do |derivative_attributes, method|
@@ -155,7 +155,9 @@ class WorkPackages::UpdateAncestorsService
return if no_children?(work_package, loader)
work_packages = [work_package] + loader.descendants_of(work_package)
- values = work_packages.filter_map(&attribute)
+ values = work_packages
+ .filter(&:included_in_totals_calculation?)
+ .filter_map(&attribute)
return if values.empty?
values.sum.to_f
diff --git a/db/migrate/20240424093311_add_excluded_from_totals_to_statuses.rb b/db/migrate/20240424093311_add_excluded_from_totals_to_statuses.rb
new file mode 100644
index 00000000000..dd3e6fbb96d
--- /dev/null
+++ b/db/migrate/20240424093311_add_excluded_from_totals_to_statuses.rb
@@ -0,0 +1,5 @@
+class AddExcludedFromTotalsToStatuses < ActiveRecord::Migration[7.1]
+ def change
+ add_column :statuses, :excluded_from_totals, :boolean, default: false, null: false
+ end
+end
diff --git a/spec/factories/status_factory.rb b/spec/factories/status_factory.rb
index ddaba8fc587..586a9082c56 100644
--- a/spec/factories/status_factory.rb
+++ b/spec/factories/status_factory.rb
@@ -31,11 +31,21 @@ FactoryBot.define do
sequence(:name) { |n| "status #{n}" }
is_closed { false }
is_readonly { false }
+ excluded_from_totals { false }
+
+ trait :excluded_from_totals do
+ excluded_from_totals { true }
+ end
factory :closed_status do
is_closed { true }
end
+ factory :rejected_status do
+ excluded_from_totals
+ name { "Rejected" }
+ end
+
factory :default_status do
is_default { true }
end
diff --git a/spec/services/work_packages/update_ancestors/loader_spec.rb b/spec/services/work_packages/update_ancestors/loader_spec.rb
index 6e20a2ca707..8986890fcdb 100644
--- a/spec/services/work_packages/update_ancestors/loader_spec.rb
+++ b/spec/services/work_packages/update_ancestors/loader_spec.rb
@@ -27,6 +27,17 @@
require "spec_helper"
RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
+ shared_let(:user) { create(:user) }
+ shared_let(:project) { create(:project_with_types) }
+ shared_let(:included_status) { create(:status) }
+ shared_let(:excluded_status) { create(:rejected_status) }
+
+ before_all do
+ set_factory_default(:project_with_types, project)
+ set_factory_default(:status, included_status)
+ set_factory_default(:user, user)
+ end
+
shared_let(:grandgrandparent) do
create(:work_package,
subject: "grandgrandparent")
@@ -226,6 +237,7 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
def work_package_struct(work_package)
attribute_names = WorkPackages::UpdateAncestors::Loader::WorkPackageLikeStruct.members.map(&:to_s)
attributes = work_package.attributes.slice(*attribute_names)
+ attributes[:status_excluded_from_totals] = false
WorkPackages::UpdateAncestors::Loader::WorkPackageLikeStruct.new(**attributes)
end
@@ -235,6 +247,28 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
expect(instance.descendants_of(work_package))
.to contain_exactly(work_package_struct(child))
end
+
+ context "with the child having a status not being excluded from totals calculation" do
+ before do
+ child.update(status: included_status)
+ end
+
+ it "correctly responds true to #included_in_totals_calculation? like a WorkPackage instance" do
+ child = instance.descendants_of(work_package).first
+ expect(child.included_in_totals_calculation?).to be true
+ end
+ end
+
+ context "with the child having a status being excluded from totals calculation" do
+ before do
+ child.update(status: excluded_status)
+ end
+
+ it "correctly responds false to #included_in_totals_calculation? like a WorkPackage instance" do
+ child = instance.descendants_of(work_package).first
+ expect(child.included_in_totals_calculation?).to be false
+ end
+ end
end
context "for the parent" do
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rb
index 1361de2f830..3ea159cd7a6 100644
--- a/spec/services/work_packages/update_ancestors_service_spec.rb
+++ b/spec/services/work_packages/update_ancestors_service_spec.rb
@@ -34,6 +34,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
shared_association_default(:priority) { create(:priority) }
shared_association_default(:open_status, factory_name: :status) { create(:status, name: "Open", default_done_ratio: 0) }
shared_let(:closed_status) { create(:closed_status, name: "Closed", default_done_ratio: 100) }
+ shared_let(:rejected_status) { create(:rejected_status, default_done_ratio: 20) }
shared_let(:user) { create(:user) }
# In order to have dependent values computed, this leverages
@@ -85,6 +86,21 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
end
end
+
+ context "when changing child status to a status excluded from totals calculation" do
+ before do
+ set_attributes_on(child, status: rejected_status)
+ call_update_ancestors_service(child)
+ end
+
+ it "still recomputes child remaining work and updates ancestors total % complete excluding it" do
+ expect_work_packages([parent, child], <<~TABLE)
+ | subject | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete |
+ | parent | Open | 10h | 10h | 10h | 10h | 0% | 0% |
+ | child | Rejected | 5h | | 4h | | 20% | |
+ TABLE
+ end
+ end
end
context "with parent having nothing set, and 2 children having values set (bug #54179)" do
@@ -102,7 +118,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
it "sets parent total % complete to 100% and its total remaining work to 0h" do
- table_work_packages.map(&:reload)
expect_work_packages(table_work_packages, <<~TABLE)
hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
parent | Open | | 15h | | 0h | 0% | 100%
@@ -732,6 +747,52 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
TABLE
end
end
+
+ context "given child becomes excluded from totals calculation because of its status changed to rejected" do
+ shared_let_work_packages(<<~TABLE)
+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
+ parent | Open | 10h | 12h | 1h | 8h | 90% | 33%
+ child | Open | 2h | | 7h | | 29% |
+ TABLE
+
+ subject(:call_result) do
+ set_attributes_on(child, status: rejected_status)
+ call_update_ancestors_service(child)
+ end
+
+ it "computes parent totals excluding the child from calculations accordingly" do
+ expect(call_result).to be_success
+ updated_work_packages = call_result.all_results
+ expect_work_packages(updated_work_packages, <<~TABLE)
+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
+ parent | Open | 10h | 10h | 1h | 1h | 90% | 90%
+ child | Rejected | 2h | | 7h | | 29% |
+ TABLE
+ end
+ end
+
+ context "given child is no longer excluded from totals calculation because of its status changed from rejected" do
+ shared_let_work_packages(<<~TABLE)
+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
+ parent | Open | 10h | 10h | 1h | 1h | 90% | 90%
+ child | Rejected | 2h | | 7h | | 29% |
+ TABLE
+
+ subject(:call_result) do
+ set_attributes_on(child, status: open_status)
+ call_update_ancestors_service(child)
+ end
+
+ it "computes parent totals excluding the child from calculations accordingly" do
+ expect(call_result).to be_success
+ updated_work_packages = call_result.all_results
+ expect_work_packages(updated_work_packages, <<~TABLE)
+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
+ parent | Open | 10h | 12h | 1h | 8h | 90% | 33%
+ child | Open | 2h | | 7h | | 29% |
+ TABLE
+ end
+ end
end
describe "ignore_non_working_days propagation" do