mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
[#68225] reworked format for hierarchy items
- introduce default formatter instance - added missing specs for persistence service methods
This commit is contained in:
@@ -57,6 +57,6 @@ class CustomField::Hierarchy::Item < ApplicationRecord
|
||||
private
|
||||
|
||||
def formatter
|
||||
@formatter ||= CustomFields::Hierarchy::HierarchicalItemFormatter.new
|
||||
CustomFields::Hierarchy::HierarchicalItemFormatter.default
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -111,7 +111,7 @@ module CustomFields
|
||||
# @param item [CustomField::Hierarchy::Item] the parent of the node
|
||||
# @return [Success(Array<CustomField::Hierarchy::Item>)]
|
||||
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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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! }
|
||||
|
||||
Reference in New Issue
Block a user