From f9c9780510a5f2ad2e38290aecd04a90bc23de66 Mon Sep 17 00:00:00 2001 From: Bruno Pagno Date: Wed, 27 Nov 2024 09:22:12 +0100 Subject: [PATCH] fix for bulk editting wps with hierarchy items --- app/helpers/custom_fields_helper.rb | 13 +++++++++++++ .../hierarchy/hierarchical_item_service.rb | 9 +++++++-- .../hierarchy/hierarchical_item_service_spec.rb | 11 +++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 9a7607bb245a..1c9d15ae2087 100644 --- a/app/helpers/custom_fields_helper.rb +++ b/app/helpers/custom_fields_helper.rb @@ -172,6 +172,19 @@ def custom_field_tag_for_bulk_edit(name, custom_field, project = nil) # rubocop: options_for_select(base_options + custom_field.possible_values_options(project)), id: field_id, multiple: custom_field.multi_value?) + when "hierarchy" + base_options = [[I18n.t(:label_no_change_option), ""]] + result = CustomFields::Hierarchy::HierarchicalItemService.new + .get_descendants(item: custom_field.hierarchy_root, include_self: false) + .either( + ->(items) { items }, + ->(_) { [] } + ) + options = base_options + result.map do |item| + label = item.short.present? ? "#{item.label} (#{item.short})" : item.label + [label, item.id] + end + styled_select_tag(field_name, options_for_select(options), id: field_id, multiple: custom_field.multi_value?) else styled_text_field_tag(field_name, "", id: field_id) end diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb index 415caf1ed0cc..8d9421054652 100644 --- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb +++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb @@ -90,9 +90,14 @@ def get_branch(item:) # Gets all descendant nodes in a tree starting from the item/node. # @param item [CustomField::Hierarchy::Item] the node + # @param include_self [Boolean] flag # @return [Success(Array)] - def get_descendants(item:) - Success(item.self_and_descendants) + def get_descendants(item:, include_self: true) + if include_self + Success(item.self_and_descendants) + else + Success(item.descendants) + end 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 2756565477ef..e88ed99bf4e1 100644 --- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb +++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb @@ -188,6 +188,17 @@ expect(result.value!).to match_array(subitem2) end end + + context "when does not include self" do + it "returns all descendants not including the item passed" do + result = service.get_descendants(item: mara, include_self: false) + expect(result).to be_success + + descendants = result.value! + expect(descendants.size).to eq(2) + expect(descendants).to contain_exactly(subitem, subitem2) + end + end end describe "#move_item" do