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

[59174] Added group by for hierarchy items

Andreas Pfohle531573bba4d
diff --git a/app/models/custom_field/hierarchy/item.rb b/app/models/custom_field/hierarchy/item.rb
index dcb12cc2759..19bc4a7dd78 100644
--- a/app/models/custom_field/hierarchy/item.rb
+++ b/app/models/custom_field/hierarchy/item.rb
@@ -35,4 +35,6 @@ class CustomField::Hierarchy::Item < ApplicationRecord
has_closure_tree order: "sort_order", numeric_order: true, dont_order_roots: true, dependent: :destroy
scope :including_children, -> { includes(children: :children) }
+
+ def to_s = label
end
diff --git a/app/models/custom_field/order_statements.rb b/app/models/custom_field/order_statements.rb
index 0e4da57f8f5..3f3153db061 100644
--- a/app/models/custom_field/order_statements.rb
+++ b/app/models/custom_field/order_statements.rb
@@ -92,7 +92,7 @@ module CustomField::OrderStatements
private
- def can_be_used_for_grouping? = field_format.in?(%w[list date bool int float string link])
+ def can_be_used_for_grouping? = field_format.in?(%w[list date bool int float string link hierarchy])
# Template for all the join statements.
#
diff --git a/app/models/query/results/group_by.rb b/app/models/query/results/group_by.rb
index c3097fdb03e..045a3b8669c 100644
--- a/app/models/query/results/group_by.rb
+++ b/app/models/query/results/group_by.rb
@@ -89,11 +89,46 @@ module ::Query::Results::GroupBy
if custom_field.list?
transform_list_custom_field_keys(custom_field, groups)
+ elsif custom_field.field_format_hierarchy?
+ transform_hierarchy_custom_field_keys(custom_field, groups)
else
transform_single_custom_field_keys(custom_field, groups)
end
end
+ def transform_hierarchy_custom_field_keys(custom_field, groups)
+ items = hierarchy_items_for_keys(custom_field, groups)
+
+ groups.transform_keys do |key|
+ if custom_field.multi_value?
+ (key ? key.split(".") : []).map do |subkey|
+ items[subkey]
+ end
+ else
+ items[key] || nil
+ end
+ end
+ end
+
+ # rubocop:disable Metrics/AbcSize
+ def hierarchy_items_for_keys(custom_field, groups)
+ keys = groups.keys.map { |k| k ? k.split(".") : [] }.flatten.uniq
+
+ CustomFields::Hierarchy::HierarchicalItemService
+ .new
+ .get_descendants(item: custom_field.hierarchy_root, include_self: false)
+ .fmap { |list| list.filter { |item| keys.include?(item.label) } }
+ .fmap { |list| list.index_by(&:label) }
+ .either(
+ ->(list) { list },
+ ->(error) do
+ msg = "#{I18n.t('api_v3.errors.code_500')} #{error}"
+ raise ::API::Errors::InternalError.new(msg)
+ end
+ )
+ end
+ # rubocop:enable Metrics/AbcSize
+
def transform_list_custom_field_keys(custom_field, groups)
options = custom_options_for_keys(custom_field, groups)
@@ -103,7 +138,7 @@ module ::Query::Results::GroupBy
options[subkey].first
end
else
- options[key] ? options[key].first : nil
+ options[key]&.first
end
end
end
@@ -111,7 +146,7 @@ module ::Query::Results::GroupBy
def custom_options_for_keys(custom_field, groups)
keys = groups.keys.map { |k| k ? k.split(".") : [] }
# Because of multi select cfs we might end up having overlapping groups
- # (e.g group "1" and group "1.3" and group "3" which represent concatenated ids).
+ # (e.g. group "1" and group "1.3" and group "3" which represent concatenated ids).
# This can result in us having ids in the keys array multiple times (e.g. ["1", "1", "3", "3"]).
# If we were to use the keys array with duplicates to find the actual custom options,
# AR would throw an error as the number of records returned does not match the number
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 6f46cffc495..cfe1a35ac6a 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -84,7 +84,6 @@ module API
def self.resources(name,
except: [],
only: %i[index show create_form update_form schema])
-
(Array(only) - Array(except)).each do |method|
send(method, name)
end
@@ -211,6 +210,11 @@ module API
"#{root}/custom_fields/#{id}"
end
+ # Needed as the method is derived from the name of the class for the query API representation.
+ def self.custom_field_hierarchy_item(id)
+ custom_field_item(id)
+ end
+
def self.custom_field_item(id)
"#{root}/custom_field_items/#{id}"
end