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
Fix error when changing parent of sub-sub-work_package
Paul Donohue11449d809631
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex 468085799f0..2786deadfe4 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -36,8 +36,7 @@ class WorkPackages::UpdateAncestorsServiceenddef call(attributes)- modified = update_ancestors(attributes)- modified += update_former_ancestors(attributes)+ modified = update_current_and_former_ancestors(attributes)set_journal_note(modified)@@ -57,22 +56,31 @@ class WorkPackages::UpdateAncestorsServiceprivate- def update_ancestors(attributes)- work_package.ancestors.includes(:status).select do |ancestor|+ def update_current_and_former_ancestors(attributes)+ to_update = get_current_ancestors++ if (%i(parent_id parent) & attributes).any?+ to_update += get_former_ancestors+ to_update.uniq!+ end++ to_update.select do |ancestor|inherit_attributes(ancestor, attributes)ancestor.changed?endend- def update_former_ancestors(attributes)- return [] unless (%i(parent_id parent) & attributes).any? && previous_parent_id+ def get_current_ancestors+ work_package.ancestors.includes(:status)+ end++ def get_former_ancestors+ return [] unless previous_parent_idparent = WorkPackage.find(previous_parent_id)- ([parent] + parent.ancestors).each do |ancestor|- inherit_attributes!(ancestor)- end.select(&:changed?)+ [parent] + parent.ancestorsenddef inherit_attributes!(ancestor)@@ -103,11 +111,9 @@ class WorkPackages::UpdateAncestorsServicereturn if WorkPackage.use_status_for_done_ratio? && ancestor.status && ancestor.status.default_done_ratio# done ratio = weighted average ratio of leaves- ratio = aggregate_done_ratio(ancestor)+ ratio = (aggregate_done_ratio(ancestor) || 0)- if ratio- ancestor.done_ratio = ratio.round- end+ ancestor.done_ratio = ratio.roundend##diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex d6f77c38ba4..340beb16131 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -340,4 +340,78 @@ describe WorkPackages::UpdateAncestorsService, type: :model, with_mail: false doit_behaves_like 'updates the attributes within the new hierarchy'endend++ context 'with a common ancestor' do+ let(:status) { open_status }+ let(:done_ratio) { 50 }+ let(:estimated_hours) { 7.0 }++ let!(:grandparent) do+ create :work_package,+ derived_estimated_hours: estimated_hours,+ done_ratio: done_ratio+ end+ let!(:old_parent) do+ create :work_package,+ parent: grandparent,+ derived_estimated_hours: estimated_hours,+ done_ratio: done_ratio+ end+ let!(:new_parent) do+ create :work_package,+ parent: grandparent+ end+ let!(:work_package) do+ create :work_package,+ parent: old_parent,+ status: status,+ estimated_hours: estimated_hours,+ done_ratio: done_ratio+ end++ subject do+ work_package.parent = new_parent+ # In this test case, derived_estimated_hours and done_ratio will not+ # inherently change on grandparent. However, if work_package has siblings+ # then changing its parent could cause derived_estimated_hours and/or+ # done_ratio on grandparent to inherently change. To verify that+ # grandparent can be properly updated in that case without making this+ # test dependent on the implementation details of the+ # derived_estimated_hours and done_ratio calculations, force+ # derived_estimated_hours and done_ratio to change at the same time as the+ # parent.+ work_package.estimated_hours = (estimated_hours + 1)+ work_package.done_ratio = (done_ratio + 1)+ work_package.save!++ described_class+ .new(user: user,+ work_package: work_package)+ .call(%i(parent))+ end++ before do+ subject+ end++ it 'is successful' do+ expect(subject)+ .to be_success+ end++ it 'returns both the former and new ancestors in the dependent results without duplicates' do+ expect(subject.dependent_results.map(&:result))+ .to match_array [new_parent, grandparent, old_parent]+ end++ it 'updates the done_ratio of the former parent' do+ expect(old_parent.reload(select: :done_ratio).done_ratio)+ .to be 0+ end++ it 'updates the estimated_hours of the former parent' do+ expect(old_parent.reload(select: :derived_estimated_hours).derived_estimated_hours)+ .to be_nil+ end+ endend