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

Make result order returned by get_descendants deterministic

Jan Sandbrinkc749ca1393da
diff --git a/app/forms/custom_fields/inputs/multi_select_list.rb b/app/forms/custom_fields/inputs/multi_select_list.rb
index 2a1a756c376..a280c24861e 100644
--- a/app/forms/custom_fields/inputs/multi_select_list.rb
+++ b/app/forms/custom_fields/inputs/multi_select_list.rb
@@ -82,7 +82,6 @@ class CustomFields::Inputs::MultiSelectList < CustomFields::Inputs::Base::Autoco
CustomFields::Hierarchy::HierarchicalItemService.new
.get_descendants(item: @custom_field.hierarchy_root, include_self: false)
.value_or([])
- .sort_by(&:ancestry_path)
end
def selected?(custom_option)
diff --git a/app/forms/custom_fields/inputs/single_select_list.rb b/app/forms/custom_fields/inputs/single_select_list.rb
index 0e1edc4944c..8030485f635 100644
--- a/app/forms/custom_fields/inputs/single_select_list.rb
+++ b/app/forms/custom_fields/inputs/single_select_list.rb
@@ -77,7 +77,6 @@ class CustomFields::Inputs::SingleSelectList < CustomFields::Inputs::Base::Autoc
CustomFields::Hierarchy::HierarchicalItemService.new
.get_descendants(item: @custom_field.hierarchy_root, include_self: false)
.value_or([])
- .sort_by(&:ancestry_path)
end
def selected?(custom_option)
diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
index 498b62c43f6..d801b75be0c 100644
--- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
+++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
@@ -99,11 +99,9 @@ module CustomFields
# @param include_self [Boolean] flag
# @return [Success(Array<CustomField::Hierarchy::Item>)]
def get_descendants(item:, include_self: true)
- if include_self
- Success(item.self_and_descendants)
- else
- Success(item.descendants)
- end
+ result = item.self_and_descendants_preordered
+ result = result.offset(1) unless include_self
+ Success(result)
end
# Move an item/node to a new parent item/node
diff --git a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
index 76038df3732..4d47e32d4f9 100644
--- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
+++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
@@ -194,7 +194,6 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService, with_ee: [:cust
expect(result).to be_success
descendants = result.value!
- expect(descendants.size).to eq(3)
expect(descendants).to contain_exactly(mara, subitem, subitem2)
end
end
@@ -213,7 +212,6 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService, with_ee: [:cust
expect(result).to be_success
descendants = result.value!
- expect(descendants.size).to eq(2)
expect(descendants).to contain_exactly(subitem, subitem2)
end
end