From d1e65c30df3c3bbbd524ea6ec6fc49fa213fd8dd Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Fri, 5 Dec 2025 12:51:09 +0100 Subject: [PATCH] [#68225] reworked format for hierarchy items - introduce default formatter instance - added missing specs for persistence service methods --- app/models/custom_field/hierarchy/item.rb | 2 +- .../hierarchy/hierarchical_item_formatter.rb | 27 ++++++++++--------- .../hierarchy/hierarchical_item_service.rb | 2 +- .../weighted_item_list_strategy_spec.rb | 2 +- .../hierarchical_item_formatter_spec.rb | 19 +++++++++---- .../hierarchical_item_service_spec.rb | 23 +++++++++++++++- 6 files changed, 53 insertions(+), 22 deletions(-) diff --git a/app/models/custom_field/hierarchy/item.rb b/app/models/custom_field/hierarchy/item.rb index d634e4896ab..bae58bd5963 100644 --- a/app/models/custom_field/hierarchy/item.rb +++ b/app/models/custom_field/hierarchy/item.rb @@ -57,6 +57,6 @@ class CustomField::Hierarchy::Item < ApplicationRecord private def formatter - @formatter ||= CustomFields::Hierarchy::HierarchicalItemFormatter.new + CustomFields::Hierarchy::HierarchicalItemFormatter.default end end diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_formatter.rb b/app/services/custom_fields/hierarchy/hierarchical_item_formatter.rb index 76e66549378..46107b0a623 100644 --- a/app/services/custom_fields/hierarchy/hierarchical_item_formatter.rb +++ b/app/services/custom_fields/hierarchy/hierarchical_item_formatter.rb @@ -33,6 +33,12 @@ module CustomFields class HierarchicalItemFormatter include NumberFormatHelper + class << self + def default + @default ||= new + end + end + # @param [Boolean] path Show the full item ancestor path if true # @param [Boolean] label Show the item label if true # @param [Boolean] suffix Show the item short or weight if true @@ -59,29 +65,24 @@ module CustomFields end def format(item:) - parts = [] + path = [] + path << ancestors(item) if @path + path << item.label if @label - if @path && @label - parts << branch(item) - elsif @path - parts << ancestors(item) - elsif @label - parts << item.label - end + str_parts = [] + str_parts << path.join(" / ") unless path.empty? if @suffix suffix = format_suffix(item) - parts << suffix if suffix.present? + str_parts << suffix if suffix.present? end - parts.join(" ") + str_parts.join(" ") end private - def branch(item) = persistence_service.get_branch(item:).value!.filter_map(&:label).join(" / ") - - def ancestors(item) = persistence_service.get_ancestors(item:).value!.filter_map(&:label).join(" / ") + def ancestors(item) = persistence_service.get_ancestors(item:).value!.filter_map(&:label) def format_suffix(item) return "" if item.short.nil? && item.weight.nil? diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb index a9a8030b28f..2380bf608fd 100644 --- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb +++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb @@ -111,7 +111,7 @@ module CustomFields # @param item [CustomField::Hierarchy::Item] the parent of the node # @return [Success(Array)] def get_ancestors(item:) - Success(item.self_and_ancestors.reverse) + Success(item.ancestors.reverse) end # Gets all descendant nodes in a tree starting from the item/node. diff --git a/spec/models/custom_value/weighted_item_list_strategy_spec.rb b/spec/models/custom_value/weighted_item_list_strategy_spec.rb index 33fcb776554..7d55a5f3689 100644 --- a/spec/models/custom_value/weighted_item_list_strategy_spec.rb +++ b/spec/models/custom_value/weighted_item_list_strategy_spec.rb @@ -149,7 +149,7 @@ RSpec.describe CustomValue::WeightedItemListStrategy do end it "is the hierarchy item label" do - expect(subject).to eql "Foo Bar Baz (42.0)" + expect(subject).to eql "Foo Bar Baz (42)" end context "when item has short value" do diff --git a/spec/services/custom_fields/hierarchy/hierarchical_item_formatter_spec.rb b/spec/services/custom_fields/hierarchy/hierarchical_item_formatter_spec.rb index d15ff1d996e..af4ee30bbb9 100644 --- a/spec/services/custom_fields/hierarchy/hierarchical_item_formatter_spec.rb +++ b/spec/services/custom_fields/hierarchy/hierarchical_item_formatter_spec.rb @@ -57,9 +57,9 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemFormatter, let(:vlf) { service.insert_item(contract_class: wil_contract, parent: radio, label: "VLF", weight: 100000).value! } # rubocop:enable Layout/LineLength - subject(:formatter) { described_class.new } - context "with default formatting options" do + subject(:formatter) { described_class.default } + it "renders an item without ancestors, but with label and suffix" do expect(formatter.format(item: earth)).to eq("Earth (T)") expect(formatter.format(item: sol)).to eq("Sol") @@ -77,10 +77,19 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemFormatter, end context "with specific number formatting" do - subject(:formatter) { described_class.new(suffix_parentheses: false, number_length_limit: 12, number_precision: 12) } + subject(:formatter) { described_class.new(number_length_limit: 12, number_precision: 12) } - it "renders an item with weight without parentheses and custom precision" do - expect(formatter.format(item: x_ray)).to eq("X-Ray 0.00000000001") + it "renders an item with weight with custom precision" do + expect(formatter.format(item: x_ray)).to eq("X-Ray (0.00000000001)") + end + end + + context "with only the suffix without parentheses" do + subject(:formatter) { described_class.new(label: false, suffix_parentheses: false) } + + it "renders an item with weight without parentheses" do + expect(formatter.format(item: x_ray)).to eq("1.0e-11") + expect(formatter.format(item: luna)).to eq("Ln") end end end 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 e7ccd5541c0..3c8c3f1b0b5 100644 --- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb +++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb @@ -176,7 +176,7 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService, with_ee: [:cust end context "with a root node" do - it "returns a empty list" do + it "returns a list with the root node" do result = service.get_branch(item: root) expect(result).to be_success expect(result.value!).to match_array(root) @@ -184,6 +184,27 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService, with_ee: [:cust end end + describe "#get_ancestors" do + context "with a non-root node" do + it "returns all the ancestors to that item" do + result = service.get_ancestors(item: mara) + expect(result).to be_success + + ancestors = result.value! + expect(ancestors.size).to eq(2) + expect(ancestors).to contain_exactly(root, luke) + end + end + + context "with a root node" do + it "returns a empty list" do + result = service.get_ancestors(item: root) + expect(result).to be_success + expect(result.value!).to be_empty + end + end + end + describe "#get_descendants" do let!(:subitem) { service.insert_item(contract_class:, parent: mara, label: "Sub1").value! } let!(:subitem2) { service.insert_item(contract_class:, parent: mara, label: "sub two").value! }