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

Split TableHelpers::Column into multiple files

Christophe Bliard86a4b9818e95
diff --git a/spec/support/table_helpers/column.rb b/spec/support/table_helpers/column.rb
index 30a28e82556..480e71efb3d 100644
--- a/spec/support/table_helpers/column.rb
+++ b/spec/support/table_helpers/column.rb
@@ -29,195 +29,98 @@
#++
require_relative "identifier"
+require_relative "column_type/generic"
+require_relative "column_type/with_identifier_metadata"
+require_relative "column_type/duration"
+require_relative "column_type/hierarchy"
+require_relative "column_type/percentage"
+require_relative "column_type/status"
+require_relative "column_type/subject"
module TableHelpers
- module Column
+ class Column
extend Identifier
+ COLUMN_TYPES = {
+ estimated_hours: ColumnType::Duration,
+ derived_estimated_hours: ColumnType::Duration,
+ remaining_hours: ColumnType::Duration,
+ derived_remaining_hours: ColumnType::Duration,
+ done_ratio: ColumnType::Percentage,
+ derived_done_ratio: ColumnType::Percentage,
+ hierarchy: ColumnType::Hierarchy,
+ status: ColumnType::Status,
+ subject: ColumnType::Subject,
+ __fallback__: ColumnType::Generic
+ }.freeze
+
def self.for(header)
+ new(header:, attribute: attribute_for(header))
+ end
+
+ def self.attribute_for(header)
case header
when /\A\s*estimated hours/i
raise ArgumentError, 'Please use "work" instead of "estimated hours"'
when /derived estimated hours/i
- raise ArgumentError, 'Please use "derived work" instead of "derived estimated hours"'
- when /derived remaining hours/i
- raise ArgumentError, 'Please use "derived remaining work" instead of "derived remaining hours"'
+ raise ArgumentError, 'Please use "∑ work" instead of "derived estimated hours"'
when /\A\s*remaining hours/i
raise ArgumentError, 'Please use "remaining work" instead of "remaining hours"'
+ when /derived remaining hours/i
+ raise ArgumentError, 'Please use "∑ remaining work" instead of "derived remaining hours"'
when /\A\s*work/i
- Duration.new(header:, attribute: :estimated_hours)
+ :estimated_hours
when /(∑|derived|total) work/i
- Duration.new(header:, attribute: :derived_estimated_hours)
+ :derived_estimated_hours
when /\A\s*remaining work/i
- Duration.new(header:, attribute: :remaining_hours)
+ :remaining_hours
when /(∑|derived|total) remaining work/i
- Duration.new(header:, attribute: :derived_remaining_hours)
+ :derived_remaining_hours
when /\A\s*% complete/i
- Percentage.new(header:, attribute: :done_ratio)
+ :done_ratio
when /(∑|derived|total) % complete/i
- Percentage.new(header:, attribute: :derived_done_ratio)
+ :derived_done_ratio
when /end date/i
- Generic.new(header:, attribute: :due_date)
- when /status/
- Status.new(header:)
- when /subject/
- Subject.new(header:)
- when /hierarchy/
- Hierarchy.new(header:)
+ :due_date
+ when /status/, /hierarchy/
+ to_identifier(header)
else
- assert_work_package_attribute_exists(header)
- Generic.new(header:)
- end
- end
-
- def self.assert_work_package_attribute_exists(attribute)
- attribute = to_identifier(attribute).to_s
- return if WorkPackage.attribute_names.include?(attribute)
-
- raise ArgumentError, "WorkPackage does not have an attribute named #{attribute.inspect}"
- end
-
- class Generic
- include Identifier
-
- attr_reader :attribute, :title, :raw_header
-
- def initialize(header:, attribute: nil)
- @raw_header = header
- @title = header.strip
- @attribute = attribute || to_identifier(title)
- end
-
- def format(value)
- value.to_s
- end
-
- def cell_format(value, size)
- format(value).send(text_align, size)
- end
-
- def parse(raw_value)
- raw_value.strip
- end
-
- def text_align
- :ljust
- end
-
- def attribute_value_for(work_package)
- work_package.read_attribute(attribute)
- end
-
- def read_and_update_work_packages_data(work_packages_data)
- work_packages_data.each do |work_package_data|
- work_package_data => { attributes:, row: }
- raw_value = row[raw_header]
- work_package_data.merge!(metadata_for_value(raw_value))
- attributes.merge!(attributes_for_raw_value(raw_value, work_package_data, work_packages_data))
- end
- end
-
- def attributes_for_work_package(work_package)
- { attribute => work_package.read_attribute(attribute) }
- end
-
- def attributes_for_raw_value(raw_value, _data, _work_packages_data)
- { attribute => parse(raw_value) }
- end
-
- def metadata_for_value(_raw_value)
- {}
+ attribute = to_identifier(header)
+ assert_work_package_attribute_exists(attribute)
+ attribute
end
end
- module WithIdentifierMetadata
- include Identifier
+ attr_reader :attribute, :raw_header, :title
- def metadata_for_value(raw_value, *)
- super.merge(identifier: to_identifier(raw_value))
- end
+ def initialize(header:, attribute:)
+ @raw_header = header
+ @title = header.strip
+ @attribute = attribute
end
- class Duration < Generic
- def text_align
- :rjust
- end
-
- def format(value)
- if value.nil?
- ""
- elsif value == value.truncate
- "%sh" % value.to_i
- else
- "%sh" % value
- end
- end
-
- def parse(raw_value)
- raw_value.blank? ? nil : raw_value.to_f
- end
+ def column_type
+ @column_type ||= COLUMN_TYPES.fetch(attribute, COLUMN_TYPES[:__fallback__]).new
end
- class Percentage < Generic
- def text_align
- :rjust
- end
-
- def format(value)
- if value.nil?
- ""
- else
- "%s%%" % value.to_i
- end
- end
+ delegate :format, :cell_format, to: :column_type
- def parse(raw_value)
- raw_value.blank? ? nil : raw_value.to_i
- end
+ def attributes_for_work_package(work_package)
+ column_type.attributes_for_work_package(attribute, work_package)
end
- class Status < Generic
- def attributes_for_work_package(work_package)
- { status: work_package.status.name }
+ def read_and_update_work_packages_data(work_packages_data)
+ work_packages_data.each do |work_package_data|
+ work_package_data.deep_merge!(
+ column_type.extract_data(attribute, raw_header, work_package_data, work_packages_data)
+ )
end
end
- class Subject < Generic
- include WithIdentifierMetadata
- end
-
- class Hierarchy < Generic
- include WithIdentifierMetadata
-
- def attributes_for_work_package(work_package)
- {
- parent: to_identifier(work_package.parent&.subject),
- subject: work_package.subject
- }
- end
-
- def attributes_for_raw_value(raw_value, data, work_packages_data)
- {
- parent: find_parent(data, work_packages_data),
- subject: parse(raw_value)
- }
- end
-
- def metadata_for_value(raw_value)
- super.merge(hierarchy_indent: raw_value[/\A */].size)
- end
-
- private
-
- def find_parent(data, work_packages_data)
- return if data[:hierarchy_indent] == 0
+ def self.assert_work_package_attribute_exists(attribute)
+ return if WorkPackage.attribute_names.include?(attribute.to_s)
- work_packages_data
- .slice(0, data[:index])
- .reverse
- .find { _1[:hierarchy_indent] < data[:hierarchy_indent] }
- .then { _1&.fetch(:identifier) }
- end
+ raise ArgumentError, "WorkPackage does not have an attribute named #{attribute.inspect}"
end
end
end
diff --git a/spec/support/table_helpers/column_type/duration.rb b/spec/support/table_helpers/column_type/duration.rb
new file mode 100644
index 00000000000..31a7cd8a86c
--- /dev/null
+++ b/spec/support/table_helpers/column_type/duration.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module TableHelpers
+ module ColumnType
+ # Column type used for values that represent a duration like work and
+ # remaining work.
+ #
+ # Parse hours or plain floats, for instance "2", "2h", or "3.5".
+ # Format to hours, for instance "2h" or "3.5h".
+ class Duration < Generic
+ def text_align
+ :rjust
+ end
+
+ def format(value)
+ if value.nil?
+ ""
+ elsif value == value.truncate
+ "%sh" % value.to_i
+ else
+ "%sh" % value
+ end
+ end
+
+ def parse(raw_value)
+ raw_value.blank? ? nil : raw_value.to_f
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column_type/generic.rb b/spec/support/table_helpers/column_type/generic.rb
new file mode 100644
index 00000000000..ee527c81911
--- /dev/null
+++ b/spec/support/table_helpers/column_type/generic.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module TableHelpers
+ module ColumnType
+ class Generic
+ def text_align
+ :ljust
+ end
+
+ def format(value)
+ value.to_s
+ end
+
+ def cell_format(value, size)
+ format(value).send(text_align, size)
+ end
+
+ def parse(raw_value)
+ raw_value.strip
+ end
+
+ # Extracts attributes data and metadata from the work package data read
+ # from a table.
+ def extract_data(attribute, raw_header, work_package_data, work_packages_data)
+ raw_value = work_package_data.dig(:row, raw_header)
+
+ {
+ attributes: attributes_for_raw_value(attribute, raw_value, work_package_data, work_packages_data),
+ **metadata_for_raw_value(raw_value)
+ }
+ end
+
+ # Extracts attribute values data from a work package.
+ #
+ # The values are to be displayed in the table.
+ #
+ # Override if:
+ # - multiple attributes are extracted from a work package for a column
+ # type (for instance `Hierarchy` column type extracts both `subject` and
+ # `parent` attributes)
+ # - or if the value is not read from a work package attribute, but from
+ # one of its relations (for instance, `Status` column type reads
+ # `work_package.status.name` to have a string value, because
+ # `work_package.status` alone is not a string).
+ def attributes_for_work_package(attribute, work_package)
+ { attribute => work_package.read_attribute(attribute) }
+ end
+
+ # Extracts attribute values data from the raw value read from a cell.
+ #
+ # Override if:
+ # - multiple attributes are extracted from a single cell raw value for a
+ # column type (for instance `Hierarchy` column type extracts both
+ # `subject` and `parent` attributes)
+ def attributes_for_raw_value(attribute, raw_value, _data, _work_packages_data)
+ { attribute => parse(raw_value) }
+ end
+
+ # Extracts metadata from the raw value read from a cell.
+ #
+ # The metadata is useful to store additional information about the row.
+ #
+ # Override when needing to store additional metadata. For instance the
+ # `Subject` column type stores the work package identifier as metadata.
+ def metadata_for_raw_value(_raw_value)
+ {}
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column_type/hierarchy.rb b/spec/support/table_helpers/column_type/hierarchy.rb
new file mode 100644
index 00000000000..1fabed0c02d
--- /dev/null
+++ b/spec/support/table_helpers/column_type/hierarchy.rb
@@ -0,0 +1,77 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module TableHelpers
+ module ColumnType
+ # Adds the `:identifier` metadata to the row.
+ #
+ # The `:identifier` value is the key being used to identify a work package
+ # by its variable name. It's also the variable name to be used when using
+ # the work packages in the tests.
+ class Hierarchy < Generic
+ include WithIdentifierMetadata
+
+ def attributes_for_work_package(_attribute, work_package)
+ {
+ parent: to_identifier(work_package.parent&.subject),
+ subject: work_package.subject
+ }
+ end
+
+ def attributes_for_raw_value(_attribute, raw_value, data, work_packages_data)
+ {
+ parent: find_parent(raw_value, data, work_packages_data),
+ subject: parse(raw_value)
+ }
+ end
+
+ def metadata_for_raw_value(raw_value)
+ super.merge(hierarchy_indent: hierarchy_indent(raw_value))
+ end
+
+ private
+
+ def hierarchy_indent(raw_value)
+ raw_value[/\A */].size
+ end
+
+ def find_parent(raw_value, data, work_packages_data)
+ hierarchy_indent = hierarchy_indent(raw_value)
+ return if hierarchy_indent == 0
+
+ work_packages_data
+ .slice(0, data[:index])
+ .reverse
+ .find { _1[:hierarchy_indent] < hierarchy_indent }
+ .then { _1&.fetch(:identifier) }
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column_type/percentage.rb b/spec/support/table_helpers/column_type/percentage.rb
new file mode 100644
index 00000000000..141903b2018
--- /dev/null
+++ b/spec/support/table_helpers/column_type/percentage.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+module TableHelpers
+ module ColumnType
+ # Column type used for values that represent a percentage like % complete.
+ #
+ # Parse percentage or plain integers, for instance "10", "20%", or "100%".
+ # Format to percentage, for instance "2%" or "35%".
+ class Percentage < Generic
+ def text_align
+ :rjust
+ end
+
+ def format(value)
+ if value.nil?
+ ""
+ else
+ "%s%%" % value.to_i
+ end
+ end
+
+ def parse(raw_value)
+ raw_value.blank? ? nil : raw_value.to_i
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column_type/status.rb b/spec/support/table_helpers/column_type/status.rb
new file mode 100644
index 00000000000..4ac8f7e2cb2
--- /dev/null
+++ b/spec/support/table_helpers/column_type/status.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module TableHelpers
+ module ColumnType
+ class Status < Generic
+ def attributes_for_work_package(_attribute, work_package)
+ { status: work_package.status.name }
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column_type/subject.rb b/spec/support/table_helpers/column_type/subject.rb
new file mode 100644
index 00000000000..ad8ddb29215
--- /dev/null
+++ b/spec/support/table_helpers/column_type/subject.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module TableHelpers
+ module ColumnType
+ class Subject < Generic
+ include WithIdentifierMetadata
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column_type/with_identifier_metadata.rb b/spec/support/table_helpers/column_type/with_identifier_metadata.rb
new file mode 100644
index 00000000000..e2400306723
--- /dev/null
+++ b/spec/support/table_helpers/column_type/with_identifier_metadata.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module TableHelpers
+ module ColumnType
+ # Adds the `:identifier` metadata to the row.
+ #
+ # The `:identifier` value is the key being used to identify a work package
+ # by its variable name. It's also the variable name to be used when using
+ # the work packages in the tests.
+ module WithIdentifierMetadata
+ include Identifier
+
+ def metadata_for_raw_value(raw_value, *)
+ super.merge(identifier: to_identifier(raw_value))
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/table_helpers/column_spec.rb b/spec/support_spec/table_helpers/column_type/duration_spec.rb
similarity index 58%
rename from spec/support_spec/table_helpers/column_spec.rb
rename to spec/support_spec/table_helpers/column_type/duration_spec.rb
index a421b45d6b7..372185a0414 100644
--- a/spec/support_spec/table_helpers/column_spec.rb
+++ b/spec/support_spec/table_helpers/column_type/duration_spec.rb
@@ -30,57 +30,35 @@
require "spec_helper"
-module TableHelpers::Column
- RSpec.describe Generic do
- subject(:column) { described_class.new(header: "Some header") }
-
- describe "#format" do
- it "renders the value as string" do
- expect(column.format("hello")).to eq "hello"
- expect(column.format(42)).to eq "42"
- expect(column.format(3.5)).to eq "3.5"
- expect(column.format(nil)).to eq ""
- expect(column.format(true)).to eq "true"
- end
- end
-
- describe "#cell_format" do
- it "renders the value on the left side of the cell" do
- expect(column.cell_format("hello", 0)).to eq "hello"
- expect(column.cell_format("hello", 10)).to eq "hello "
- expect(column.cell_format("hello", 20)).to eq "hello "
- end
- end
- end
-
+module TableHelpers::ColumnType
RSpec.describe Duration do
- subject(:column) { described_class.new(header: "Duration in hours") }
+ subject(:column_type) { described_class.new }
describe "#parse" do
it "parses empty string as nil" do
- expect(column.parse("")).to be_nil
+ expect(column_type.parse("")).to be_nil
end
end
describe "#format" do
it 'renders the duration with a "h" suffix' do
- expect(column.format(3.5)).to eq "3.5h"
+ expect(column_type.format(3.5)).to eq "3.5h"
end
it "renders the duration without the decimal part if the decimal part is 0" do
- expect(column.format(3.0)).to eq "3h"
+ expect(column_type.format(3.0)).to eq "3h"
end
it "renders nothing if nil" do
- expect(column.format(nil)).to eq ""
+ expect(column_type.format(nil)).to eq ""
end
end
describe "#cell_format" do
it "renders the duration on the right side of the cell" do
- expect(column.cell_format(3.5, 0)).to eq "3.5h"
- expect(column.cell_format(3.5, 10)).to eq " 3.5h"
- expect(column.cell_format(3.5, 20)).to eq " 3.5h"
+ expect(column_type.cell_format(3.5, 0)).to eq "3.5h"
+ expect(column_type.cell_format(3.5, 10)).to eq " 3.5h"
+ expect(column_type.cell_format(3.5, 20)).to eq " 3.5h"
end
end
end
diff --git a/spec/support_spec/table_helpers/column_type/generic_spec.rb b/spec/support_spec/table_helpers/column_type/generic_spec.rb
new file mode 100644
index 00000000000..93ef294f561
--- /dev/null
+++ b/spec/support_spec/table_helpers/column_type/generic_spec.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+module TableHelpers::ColumnType
+ RSpec.describe Generic do
+ subject(:column_type) { described_class.new }
+
+ describe "#format" do
+ it "renders the value as string" do
+ expect(column_type.format("hello")).to eq "hello"
+ expect(column_type.format(42)).to eq "42"
+ expect(column_type.format(3.5)).to eq "3.5"
+ expect(column_type.format(nil)).to eq ""
+ expect(column_type.format(true)).to eq "true"
+ end
+ end
+
+ describe "#cell_format" do
+ it "renders the value on the left side of the cell" do
+ expect(column_type.cell_format("hello", 0)).to eq "hello"
+ expect(column_type.cell_format("hello", 10)).to eq "hello "
+ expect(column_type.cell_format("hello", 20)).to eq "hello "
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/table_helpers/column_type/hierarchy_spec.rb b/spec/support_spec/table_helpers/column_type/hierarchy_spec.rb
new file mode 100644
index 00000000000..03c98ea5c88
--- /dev/null
+++ b/spec/support_spec/table_helpers/column_type/hierarchy_spec.rb
@@ -0,0 +1,102 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+module TableHelpers::ColumnType
+ RSpec.describe Hierarchy do
+ subject(:column_type) { described_class.new }
+
+ describe "#extract_data" do
+ let(:attribute) { :hierarchy }
+ let(:subject_raw_header) { "Hierarchy " }
+ let(:work_packages_data) do
+ [
+ {
+ index: 0,
+ row: { subject_raw_header => "Parent " }
+ },
+ {
+ index: 1,
+ row: { subject_raw_header => " Child1 " }
+ },
+ {
+ index: 2,
+ row: { subject_raw_header => " Child2 " }
+ },
+ {
+ index: 3,
+ row: { subject_raw_header => " Grand Child" }
+ }
+ ]
+ end
+
+ it "extracts the identifier metadata along with the :subject attribute value" do
+ # check first row only
+ expect(column_type.extract_data(attribute, subject_raw_header, work_packages_data.first, work_packages_data))
+ .to include({ attributes: a_hash_including(subject: "Parent"),
+ identifier: :parent })
+ end
+
+ it "extracts the hierarchy_indent metadata as the number of spaces before the name, " \
+ "and the :parent attribute value holding the identifier of the parent from previous rows" do
+ # first row
+ work_package_data = work_packages_data.first
+ row_extract = column_type.extract_data(attribute, subject_raw_header, work_package_data, work_packages_data)
+ expect(row_extract).to include({ attributes: a_hash_including(parent: nil),
+ hierarchy_indent: 0 })
+
+ # second row
+ work_package_data.deep_merge!(row_extract)
+ work_package_data = work_packages_data.second
+ row_extract = column_type.extract_data(attribute, subject_raw_header, work_package_data, work_packages_data)
+ expect(row_extract).to include({ attributes: a_hash_including(parent: :parent),
+ hierarchy_indent: 2,
+ identifier: :child1 })
+
+ # third row
+ work_package_data.deep_merge!(row_extract)
+ work_package_data = work_packages_data.third
+ row_extract = column_type.extract_data(attribute, subject_raw_header, work_package_data, work_packages_data)
+ expect(row_extract).to include({ attributes: a_hash_including(parent: :parent),
+ hierarchy_indent: 2,
+ identifier: :child2 })
+
+ # fourth row
+ work_package_data.deep_merge!(row_extract)
+ work_package_data = work_packages_data.fourth
+ row_extract = column_type.extract_data(attribute, subject_raw_header, work_package_data, work_packages_data)
+ expect(row_extract).to include({ attributes: a_hash_including(parent: :child2),
+ hierarchy_indent: 5,
+ identifier: :grand_child })
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/table_helpers/column_type/percentage_spec.rb b/spec/support_spec/table_helpers/column_type/percentage_spec.rb
new file mode 100644
index 00000000000..b4050e16c1d
--- /dev/null
+++ b/spec/support_spec/table_helpers/column_type/percentage_spec.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+module TableHelpers::ColumnType
+ RSpec.describe Percentage do
+ subject(:column_type) { described_class.new }
+
+ describe "#parse" do
+ it "parses empty string as nil" do
+ expect(column_type.parse("")).to be_nil
+ end
+ end
+
+ describe "#format" do
+ it 'renders the percentage with a "%" suffix' do
+ expect(column_type.format(30)).to eq "30%"
+ expect(column_type.format(30.9)).to eq "30%"
+ end
+
+ it "renders nothing if nil" do
+ expect(column_type.format(nil)).to eq ""
+ end
+ end
+
+ describe "#cell_format" do
+ it "renders the percentage on the right side of the cell" do
+ expect(column_type.cell_format(20, 0)).to eq "20%"
+ expect(column_type.cell_format(35, 10)).to eq " 35%"
+ expect(column_type.cell_format(57, 20)).to eq " 57%"
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/table_helpers/column_type/status_spec.rb b/spec/support_spec/table_helpers/column_type/status_spec.rb
new file mode 100644
index 00000000000..0b5ed0fba05
--- /dev/null
+++ b/spec/support_spec/table_helpers/column_type/status_spec.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+module TableHelpers::ColumnType
+ RSpec.describe Status do
+ subject(:column_type) { described_class.new }
+
+ describe "#attributes_for_work_package" do
+ let(:status) { build(:status, name: "New") }
+ let(:work_package) { build(:work_package, status:) }
+
+ it "uses the status name as the value for status attribute" do
+ expect(column_type.attributes_for_work_package(:status, work_package))
+ .to eq(status: "New")
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/table_helpers/column_type/subject_spec.rb b/spec/support_spec/table_helpers/column_type/subject_spec.rb
new file mode 100644
index 00000000000..0693157a8b5
--- /dev/null
+++ b/spec/support_spec/table_helpers/column_type/subject_spec.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+module TableHelpers::ColumnType
+ RSpec.describe Subject do
+ subject(:column_type) { described_class.new }
+
+ describe "#extract_data" do
+ it "extracts the identifier metadata along with the :subject attribute value" do
+ attribute = :subject
+ subject_raw_header = "subject "
+ work_packages_data =
+ [
+ {
+ index: 0,
+ row: { subject_raw_header => " Work package 2 " }
+ }
+ ]
+ work_package_data = work_packages_data.first
+
+ expect(column_type.extract_data(attribute, subject_raw_header, work_package_data, work_packages_data))
+ .to eq({ attributes: { subject: "Work package 2" },
+ identifier: :work_package2 })
+ end
+ end
+ end
+end