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 table helpers supports hierarchy column in `expect_work_packages`

Christophe Bliard010359aaa304
diff --git a/spec/support/table_helpers/table_data.rb b/spec/support/table_helpers/table_data.rb
index 3937cf0a0b0..e30841aa879 100644
--- a/spec/support/table_helpers/table_data.rb
+++ b/spec/support/table_helpers/table_data.rb
@@ -85,15 +85,52 @@ module TableHelpers
Table.new(work_packages_by_identifier, relations)
end
+ def hierarchy_levels
+ identifiers = work_packages_data.pluck(:identifier)
+ parents = work_packages_data.pluck(:attributes).pluck(:parent)
+ identifier_parent_tuples = identifiers.zip(parents)
+ levels = {}
+ iterations = 0
+ while identifier_parent_tuples.any? && iterations < identifier_parent_tuples.size
+ identifier, parent = identifier_parent_tuples.shift
+ if parent.nil?
+ levels[identifier] = 0
+ iterations = 0
+ elsif levels.has_key?(parent)
+ levels[identifier] = levels[parent] + 1
+ iterations = 0
+ else
+ identifier_parent_tuples.push([identifier, parent])
+ iterations += 1
+ end
+ end
+ levels
+ end
+
+ def children_by_parent(parent_identifier)
+ work_packages_data
+ .filter { it.dig(:attributes, :parent) == parent_identifier }
+ .pluck(:identifier)
+ end
+
def order_like!(other_table)
- ordered_identifiers = other_table.work_package_identifiers
- extra_identifiers = work_package_identifiers - ordered_identifiers
@work_packages_data = work_packages_data
.index_by { it[:identifier] }
- .values_at(*(ordered_identifiers + extra_identifiers))
+ .values_at(*identifiers_ordered_like(other_table))
.compact
end
+ def identifiers_ordered_like(other_table, parent_identifier = nil, acc = [])
+ other_children = other_table.children_by_parent(parent_identifier)
+ own_children = children_by_parent(parent_identifier)
+ ordered_children = other_children.intersection(own_children) + own_children.difference(other_children)
+ ordered_children.each do |identifier|
+ acc << identifier
+ identifiers_ordered_like(other_table, identifier, acc)
+ end
+ acc
+ end
+
class Factory
include Identifier
diff --git a/spec/support/table_helpers/table_representer.rb b/spec/support/table_helpers/table_representer.rb
index e14dab90447..faa2df3eb09 100644
--- a/spec/support/table_helpers/table_representer.rb
+++ b/spec/support/table_helpers/table_representer.rb
@@ -50,40 +50,58 @@ module TableHelpers
end
def get_header_and_values(column, table_data)
+ header =
+ if column.attribute == :schedule
+ schedule_column_representer.column_title
+ else
+ column.title
+ end
+ values = get_values(column, table_data)
+ [header, *values]
+ end
+
+ def get_values(column, table_data)
if column.attribute == :schedule
- header = schedule_column_representer.column_title
start_dates = table_data.values_for_attribute(:start_date)
due_dates = table_data.values_for_attribute(:due_date)
ignore_non_working_days_values = ignore_non_working_days_values(table_data)
- values = start_dates.zip(due_dates, ignore_non_working_days_values).map do |start_date, due_date, ignore_non_working_days|
+ start_dates.zip(due_dates, ignore_non_working_days_values).map do |start_date, due_date, ignore_non_working_days|
schedule_column_representer.span(start_date, due_date, ignore_non_working_days)
end
+ elsif column.attribute == :hierarchy
+ table_data
+ .values_for_attribute(:subject)
+ .zip(get_hierarchy_levels(table_data))
+ .map! { |subject, level| column.format("#{' ' * level}#{subject}") }
else
- header = column.title
- values = table_data
+ table_data
.values_for_attribute(column.attribute)
- .map! { column.format(_1) }
+ .map! { column.format(it) }
end
- [header, *values]
end
# look into the other tables to find the ignore_non_working_days values of
# work packages for the given table data
def ignore_non_working_days_values(table_data)
master_values = table_data.work_packages_data.pluck(:attributes).pluck(:subject, :ignore_non_working_days).to_h
- other_values = tables_data.reject { _1 == table_data }
- .map { _1.work_packages_data.pluck(:attributes).pluck(:subject, :ignore_non_working_days).to_h.compact }
+ other_values = tables_data.reject { it == table_data }
+ .map { it.work_packages_data.pluck(:attributes).pluck(:subject, :ignore_non_working_days).to_h.compact }
.reduce({}) { |acc, element| acc.reverse_merge(element) }
master_values.map do |subject, ignore_non_working_days|
ignore_non_working_days.nil? ? other_values[subject] : ignore_non_working_days
end
end
+ def get_hierarchy_levels(table_data)
+ identifiers = table_data.work_packages_data.pluck(:identifier)
+ table_data.hierarchy_levels.values_at(*identifiers)
+ end
+
def normalize_width(cells, column)
header, *values = cells
width = column_width(column)
header = header.ljust(width)
- values.map! { column.align(_1, width) }
+ values.map! { column.align(it, width) }
[header, *values]
end
@@ -100,17 +118,16 @@ module TableHelpers
if column.attribute == :schedule
schedule_column_representer.column_size
else
- values = tables_data.flat_map { _1.values_for_attribute(column.attribute) }
- values_max_size = values.map { column.format(_1).size }.max
- [column.title.size, values_max_size].compact.max
+ values = tables_data.flat_map { |table_data| get_values(column, table_data) }
+ [column.title.size, *values.map(&:size)].compact.max
end
end
end
def schedule_column_representer
@schedule_column_representer ||= begin
- start_dates = tables_data.flat_map { _1.values_for_attribute(:start_date) }
- due_dates = tables_data.flat_map { _1.values_for_attribute(:due_date) }
+ start_dates = tables_data.flat_map { it.values_for_attribute(:start_date) }
+ due_dates = tables_data.flat_map { it.values_for_attribute(:due_date) }
ScheduleColumnFormatter.new(start_dates, due_dates)
end
end
diff --git a/spec/support_spec/table_helpers/table_data_spec.rb b/spec/support_spec/table_helpers/table_data_spec.rb
index 688967bfd38..f3514bb4ddc 100644
--- a/spec/support_spec/table_helpers/table_data_spec.rb
+++ b/spec/support_spec/table_helpers/table_data_spec.rb
@@ -258,6 +258,38 @@ module TableHelpers
end
end
+ describe "#hierarchy_levels" do
+ it "returns 0 for each identifier when there is not hierarchy defined" do
+ table_representation = <<~TABLE
+ | subject |
+ | wp1 |
+ | wp2 |
+ TABLE
+ table_data = described_class.for(table_representation)
+ expect(table_data.hierarchy_levels).to eq({ wp1: 0, wp2: 0 })
+ end
+
+ it "when using hierarchy column, returns 0 for root level work packages, and 1 more for each hierarchy level" do
+ table_representation = <<~TABLE
+ | hierarchy |
+ | parent |
+ | child |
+ TABLE
+ table_data = described_class.for(table_representation)
+ expect(table_data.hierarchy_levels).to eq({ parent: 0, child: 1 })
+
+ table_representation = <<~TABLE
+ | hierarchy |
+ | parent |
+ | child1 |
+ | grandchild11 |
+ | child2 |
+ TABLE
+ table_data = described_class.for(table_representation)
+ expect(table_data.hierarchy_levels).to eq({ parent: 0, child1: 1, grandchild11: 2, child2: 1 })
+ end
+ end
+
describe "#order_like" do
it "orders the table data like the given table" do
table_representation = <<~TABLE
@@ -320,6 +352,80 @@ module TableHelpers
table_data.order_like!(other_table_data)
expect(table_data.work_package_identifiers).to eq(%i[another_one work_package extra_one])
end
+
+ it "deals well with hierarchies when present" do
+ table_representation = <<~TABLE
+ | hierarchy |
+ | parent |
+ | child |
+ | other |
+ TABLE
+ table_data = described_class.for(table_representation)
+
+ other_table_representation = <<~TABLE
+ | hierarchy |
+ | parent |
+ | child renamed |
+ | other |
+ TABLE
+ other_table_data = described_class.for(other_table_representation)
+
+ expect(table_data.work_package_identifiers)
+ .to eq(%i[parent child other])
+
+ table_data.order_like!(other_table_data)
+ expect(table_data.work_package_identifiers)
+ .to eq(%i[parent child other])
+ end
+
+ it "deals well with big hierarchies when present" do # rubocop:disable RSpec/ExampleLength
+ table_representation = <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | wp3 |
+ | parent |
+ | child2 |
+ | grandchild21 |
+ | child1 |
+ | grandchild13 |
+ | grandchild12 |
+ | grandgrandchild121 |
+ | child3 |
+ TABLE
+ table_data = described_class.for(table_representation)
+
+ other_table_representation = <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | parent |
+ | child1 |
+ | grandchild11 |
+ | grandchild12 |
+ | child2 |
+ | child3 |
+ | grandchild31 |
+ | grandchild32 |
+ | wp3 |
+ TABLE
+ other_table_data = described_class.for(other_table_representation)
+
+ expect(table_data.work_package_identifiers)
+ .to eq(%i[wp1
+ wp3
+ parent
+ child2 grandchild21
+ child1 grandchild13 grandchild12 grandgrandchild121
+ child3])
+
+ table_data.order_like!(other_table_data)
+ expect(table_data.work_package_identifiers)
+ .to eq(%i[wp1
+ parent
+ child1 grandchild12 grandgrandchild121 grandchild13
+ child2 grandchild21
+ child3
+ wp3])
+ end
end
end
end
diff --git a/spec/support_spec/table_helpers/table_representer_spec.rb b/spec/support_spec/table_helpers/table_representer_spec.rb
index bf979773eb8..1d3a662aeee 100644
--- a/spec/support_spec/table_helpers/table_representer_spec.rb
+++ b/spec/support_spec/table_helpers/table_representer_spec.rb
@@ -235,5 +235,102 @@ module TableHelpers
end
end
end
+
+ describe "hierarchy column" do
+ let(:table) do
+ <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | parent |
+ | child1 |
+ | grandchild11 |
+ | grandchild12 |
+ | child2 |
+ | child3 |
+ | grandchild31 |
+ | grandchild32 |
+ | wp3 |
+ TABLE
+ end
+ let(:columns) { [Column.for("hierarchy")] }
+
+ it "is rendered as a hierarchy" do
+ expect(representer.render(table_data)).to eq <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | parent |
+ | child1 |
+ | grandchild11 |
+ | grandchild12 |
+ | child2 |
+ | child3 |
+ | grandchild31 |
+ | grandchild32 |
+ | wp3 |
+ TABLE
+ end
+
+ context "when there are no work packages" do
+ let(:table_data) do
+ TableData.from_work_packages([], columns)
+ end
+
+ it "renders no rows" do
+ expect(representer.render(table_data)).to eq <<~TABLE
+ | hierarchy |
+ TABLE
+ end
+ end
+
+ context "when using a second table for the column size" do
+ let(:twin_table) do
+ <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | wp3 |
+ | parent |
+ | child2 |
+ | grandchild21 |
+ | child1 |
+ | grandchild13 |
+ | grandchild12 |
+ | grandgrandchild121 |
+ | child3 |
+ TABLE
+ end
+ let(:twin_table_data) { TableData.for(twin_table) }
+
+ let(:tables_data) { [table_data, twin_table_data] }
+
+ it "adapts the column size to the largest of both tables so they are diffable" do # rubocop:disable RSpec/ExampleLength
+ expect(representer.render(table_data)).to eq <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | parent |
+ | child1 |
+ | grandchild11 |
+ | grandchild12 |
+ | child2 |
+ | child3 |
+ | grandchild31 |
+ | grandchild32 |
+ | wp3 |
+ TABLE
+ expect(representer.render(twin_table_data)).to eq <<~TABLE
+ | hierarchy |
+ | wp1 |
+ | wp3 |
+ | parent |
+ | child2 |
+ | grandchild21 |
+ | child1 |
+ | grandchild13 |
+ | grandchild12 |
+ | grandgrandchild121 |
+ | child3 |
+ TABLE
+ end
+ end
+ end
end
end