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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add schedule and properties column types to table_helpers
Christophe Bliard2219c89cc48d
diff --git a/config/environments/test.rb b/config/environments/test.rbindex f5826c7c4b1..9d0d09cbe02 100644--- a/config/environments/test.rb+++ b/config/environments/test.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'active_support/core_ext/integer/time'+require "active_support/core_ext/integer/time"# The test environment is used exclusively to run your application's# test suite. You never need to work with it otherwise. Remember that@@ -52,7 +52,7 @@ Rails.application.configure do# Configure public file server for tests with Cache-Control for performance.config.public_file_server.enabled = true- config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }+ config.public_file_server.headers = { "Cache-Control" => "public, max-age=3600" }# Show full error reports and disable caching.config.consider_all_requests_local = true@@ -75,11 +75,11 @@ Rails.application.configure doconfig.action_mailer.delivery_method = :test# Silence deprecations early on for testing on CI- deprecators.silenced = ENV['CI'].present?+ deprecators.silenced = ENV["CI"].present?# Print deprecation notices to the stderr.config.active_support.deprecation =- if ENV['CI']+ if ENV["CI"]:silenceelse:stderr@@ -111,10 +111,14 @@ Rails.application.configure doconfig.cache_store = :file_store, Rails.root.join("tmp", "cache", "paralleltests#{ENV.fetch('TEST_ENV_NUMBER', nil)}")+ # Spring reloads, and therefore needs the application to not cache classes so+ # that relaoding can work.+ config.cache_classes = false+# Use in-memory store for testingRack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new- if ENV['TEST_ENV_NUMBER']+ if ENV["TEST_ENV_NUMBER"]assets_cache_path = Rails.root.join("tmp/cache/assets/paralleltests#{ENV['TEST_ENV_NUMBER']}")config.assets.cache = Sprockets::Cache::FileStore.new(assets_cache_path)enddiff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rbindex 76029f45513..488c72fcecb 100644--- a/spec/controllers/statuses_controller_spec.rb+++ b/spec/controllers/statuses_controller_spec.rb@@ -176,7 +176,7 @@ RSpec.describe StatusesController doperform_enqueued_jobs- expect_work_packages([parent, child, other_child], <<~TABLE)+ expect_work_packages_after_reload([parent, child, other_child], <<~TABLE)subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | New | | | | 10h | 10h | 0%child | Rejected | 10h | 5h | 50% | | |@@ -245,7 +245,7 @@ RSpec.describe StatusesController doperform_enqueued_jobs- expect_work_packages([parent, child, other_child], <<~TABLE)+ expect_work_packages_after_reload([parent, child, other_child], <<~TABLE)subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | New | | | 0% | 10h | 10h | 0%child | Rejected | 10h | 3h | 70% | | |@@ -271,7 +271,7 @@ RSpec.describe StatusesController doperform_enqueued_jobs- expect_work_packages([parent, child, other_child], <<~TABLE)+ expect_work_packages_after_reload([parent, child, other_child], <<~TABLE)subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | New | | | 0% | 10h | 10h | 0%child | Rejected | 10h | 6h | 40% | | |diff --git a/spec/migrations/remove_totals_from_childless_work_packages_spec.rb b/spec/migrations/remove_totals_from_childless_work_packages_spec.rbindex b181c318982..956f7efe5e9 100644--- a/spec/migrations/remove_totals_from_childless_work_packages_spec.rb+++ b/spec/migrations/remove_totals_from_childless_work_packages_spec.rb@@ -69,7 +69,7 @@ RSpec.describe RemoveTotalsFromChildlessWorkPackages, type: :model doendit "removes totals from childless work packages" do- expect_work_packages(table_work_packages.map(&:reload), <<~TABLE)+ expect_work_packages_after_reload(table_work_packages, <<~TABLE)hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completewp totals set | 5h | 3h | 40% | | |wp only pc set | | | 60% | | |diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rbindex 650df99e27f..bff11940815 100644--- a/spec/migrations/update_progress_calculation_spec.rb+++ b/spec/migrations/update_progress_calculation_spec.rb@@ -55,8 +55,7 @@ RSpec.describe UpdateProgressCalculation, type: :model dorun_migration- table.work_packages.map(&:reload)- expect_work_packages(table.work_packages, to)+ expect_work_packages_after_reload(table.work_packages, to)table.work_packagesend@@ -829,7 +828,7 @@ RSpec.describe UpdateProgressCalculation, type: :model doendit "fixes the total values and sets ∑ % complete to nil (not 0) and keeps % complete (unless wrong)" do- expect_work_packages(table_work_packages.map(&:reload), <<~TABLE)+ expect_work_packages_after_reload(table_work_packages, <<~TABLE)subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete |wp zero | | | 0 | | | |wp correct | 100h | 50h | 50 | 100h | 50h | 50 |diff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rbindex 26aa92a63cf..40fea3fc684 100644--- a/spec/services/work_packages/set_schedule_service_working_days_spec.rb+++ b/spec/services/work_packages/set_schedule_service_working_days_spec.rb@@ -42,8 +42,8 @@ RSpec.describe WorkPackages::SetScheduleService, "working days" docontext "with a single successor" docontext "when moving successor will cover non-working days" do- let_schedule(<<~CHART)- days | MTWTFSS |+ let_work_packages(<<~CHART)+ subject | MTWTFSS | propertieswork_package | XX |follower | XXX | follows work_packageCHART@@ -56,8 +56,8 @@ RSpec.describe WorkPackages::SetScheduleService, "working days" doendit "extends to a later due date to keep the same duration" do- expect_schedule(subject.all_results, <<~CHART)- days | MTWTFSS |+ expect_work_packages(subject.all_results, <<~CHART)+ subject | MTWTFSS |work_package | XXXX |follower | X..XX |CHARTdiff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex 12ff0bc8f5e..1590a5af0d8 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -78,7 +78,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,set_attributes_on(child, field => value)call_update_ancestors_service(child)- expect_work_packages([parent, child], <<~TABLE)+ expect_work_packages_after_reload([parent, child], <<~TABLE)| subject | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete || parent | Open | 10h | 15h | 10h | 10h | 0% | 33% || child | Closed | 5h | | 0h | | 100% | |@@ -95,7 +95,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,endit "still recomputes child remaining work and updates ancestors total % complete excluding it" do- expect_work_packages([parent, child], <<~TABLE)+ expect_work_packages_after_reload([parent, child], <<~TABLE)| subject | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete || parent | Open | 10h | 10h | 10h | 10h | 0% | 0% || child | Rejected | 5h | | 4h | | 20% | |@@ -119,7 +119,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,endit "sets parent total % complete to 100% and its total remaining work to 0h" do- expect_work_packages(table_work_packages, <<~TABLE)+ expect_work_packages_after_reload(table_work_packages, <<~TABLE)hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % completeparent | Open | | 15h | | 0h | 0% | 100%child1 | Closed | 10h | | 0h | | 100% |@@ -386,7 +386,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,it "updates the totals of the ancestors" dosubject- expect_work_packages([grandparent, parent, sibling, work_package], <<~TABLE)+ expect_work_packages_after_reload([grandparent, parent, sibling, work_package], <<~TABLE)hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completegrandparent | 3h | 1.5h | 50% | 13h | 5h | 62%parent | 3h | 0h | 100% | 10h | 3.5h | 65%@@ -447,7 +447,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,it "updates the totals of the ancestors" dosubject- expect_work_packages([grandparent, parent, work_package], <<~TABLE)+ expect_work_packages_after_reload([grandparent, parent, work_package], <<~TABLE)hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completegrandparent | | | | 10h | 5h | 50%parent | 3h | 1.5h | 50% | 10h | 5h | 50%@@ -494,7 +494,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,it "updates the totals of the new parent and the former parent" dosubject- expect_work_packages([grandparent, old_parent, new_parent, work_package], <<~TABLE)+ expect_work_packages_after_reload([grandparent, old_parent, new_parent, work_package], <<~TABLE)hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completegrandparent | | | | 10h | 2h | 80%old parent | | | | | |@@ -597,7 +597,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,it "does not update the parent total work" doexpect(call_result).to be_successexpect(call_result.dependent_results).to be_empty- expect_work_packages([parent.reload], <<~TABLE)+ expect_work_packages_after_reload([parent], <<~TABLE)subject | total work |parent | |TABLE@@ -693,7 +693,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService,it "does not update the parent total remaining work" doexpect(call_result).to be_successexpect(call_result.dependent_results).to be_empty- expect_work_packages([parent.reload], <<~TABLE)+ expect_work_packages_after_reload([parent], <<~TABLE)subject | total remaining workparent |TABLEdiff --git a/spec/support/table_helpers/column.rb b/spec/support/table_helpers/column.rbindex 480e71efb3d..49700ba01bc 100644--- a/spec/support/table_helpers/column.rb+++ b/spec/support/table_helpers/column.rb@@ -34,6 +34,8 @@ 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/properties"+require_relative "column_type/schedule"require_relative "column_type/status"require_relative "column_type/subject"@@ -49,6 +51,8 @@ module TableHelpersdone_ratio: ColumnType::Percentage,derived_done_ratio: ColumnType::Percentage,hierarchy: ColumnType::Hierarchy,+ properties: ColumnType::Properties,+ schedule: ColumnType::Schedule,status: ColumnType::Status,subject: ColumnType::Subject,__fallback__: ColumnType::Generic@@ -82,6 +86,10 @@ module TableHelpers:derived_done_ratiowhen /end date/i:due_date+ when /.*MTWTFSS.*/+ :schedule+ when /\s*properties\s*/+ :propertieswhen /status/, /hierarchy/to_identifier(header)else@@ -103,7 +111,7 @@ module TableHelpers@column_type ||= COLUMN_TYPES.fetch(attribute, COLUMN_TYPES[:__fallback__]).newend- delegate :format, :cell_format, to: :column_type+ delegate :format, :align, to: :column_typedef attributes_for_work_package(work_package)column_type.attributes_for_work_package(attribute, work_package)diff --git a/spec/support/table_helpers/column_type/generic.rb b/spec/support/table_helpers/column_type/generic.rbindex ee527c81911..c92f90c8744 100644--- a/spec/support/table_helpers/column_type/generic.rb+++ b/spec/support/table_helpers/column_type/generic.rb@@ -39,8 +39,8 @@ module TableHelpersvalue.to_send- def cell_format(value, size)- format(value).send(text_align, size)+ def align(text, size)+ text.send(text_align, size)enddef parse(raw_value)diff --git a/spec/support/table_helpers/column_type/properties.rb b/spec/support/table_helpers/column_type/properties.rbnew file mode 100644index 00000000000..2a550c8f1b0--- /dev/null+++ b/spec/support/table_helpers/column_type/properties.rb@@ -0,0 +1,99 @@+# 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 to add properties to work packages like "follows wp1 with lag 2".+ #+ # Supported properties:+ # - follows :wp+ # - follows :wp with lag :int+ #+ # Example:+ #+ # | subject | properties |+ # | main | |+ # | follower | follows main with lag 2 |+ # | follower2 | follows follower |+ #+ # Adapted from original implementation in `spec/support/schedule_helpers/chart_builder.rb`.+ class Properties < Generic+ def attributes_for_work_package(_attribute, _work_package)+ {}+ end++ def extract_data(_attribute, raw_header, work_package_data, _work_packages_data)+ properties = work_package_data.dig(:row, raw_header)+ properties = properties.split(",").map(&:strip).compact_blank+ parse_properties(properties)+ end++ def parse_properties(properties)+ properties.reduce({}) do |data, property|+ case parse_property(property)+ in {relations: relation}+ data[:relations] ||= []+ data[:relations] << relation+ end+ data+ end+ end++ def relations_for_raw_value(raw_value)+ raw_value.split+ {}+ end++ def parse_property(property)+ case property+ when /^follows (\w+)(?: with lag (\d+))?/+ {+ relations: {+ raw: property,+ type: :follows,+ predecessor: $1,+ lag: $2.to_i+ }+ }+ else+ spell_checker = DidYouMean::SpellChecker.new(+ dictionary: [+ "follows :wp",+ "follows :wp with lag :int"+ ]+ )+ suggestions = spell_checker.correct(property).map(&:inspect).join(" ")+ did_you_mean = " Did you mean #{suggestions} instead?" if suggestions.present?+ raise "unable to parse property #{property.inspect}.#{did_you_mean}"+ end+ end+ end+ end+enddiff --git a/spec/support/table_helpers/column_type/schedule.rb b/spec/support/table_helpers/column_type/schedule.rbnew file mode 100644index 00000000000..07269136537--- /dev/null+++ b/spec/support/table_helpers/column_type/schedule.rb@@ -0,0 +1,91 @@+# 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 to specify start date and due date using an ascii calendar.+ #+ # The title of the column is "MTWTFSS" to represent the days of the week.+ #+ # The Monday is the next occuring Monday meaning the dates can be different+ # from one test run to another. To always use the same dates please use+ # `travel_to` with a fixed date.+ #+ # Use 'X' to mark the days from start to due date. Only the first and last+ # 'X' are considered as start and due dates.+ # Use '[' to mark the start date only and ']' for the due date only.+ # Any other character is ignored+ #+ # Example:+ #+ # | subject | MTWTFSS |+ # | main | XXX |+ # | crossing non working days | XXXX..XX |+ # | start date only | [ |+ # | due date only | ] |+ # | no dates | |+ #+ # Adapted from original implementation in `spec/support/schedule_helpers/chart_builder.rb`.+ class Schedule < Generic+ def attributes_for_work_package(_attribute, work_package)+ {+ start_date: work_package.start_date,+ due_date: work_package.due_date+ }+ end++ def extract_data(_attribute, raw_header, work_package_data, _work_packages_data)+ raw_value = work_package_data.dig(:row, raw_header)++ {+ attributes: scheduling_attributes(raw_header, raw_value),+ **metadata_for_raw_value(raw_value)+ }+ end++ private++ def scheduling_attributes(reference, timespan)+ nb_days_from_origin_monday = reference.index("M")++ start_pos = timespan.index("[") || timespan.index("X")+ due_pos = timespan.rindex("]") || timespan.rindex("X")+ {+ start_date: start_pos && (current_monday - nb_days_from_origin_monday + start_pos),+ due_date: due_pos && (current_monday - nb_days_from_origin_monday + due_pos)+ }+ end++ def current_monday+ Date.current.next_occurring(:monday)+ end+ end+ end+enddiff --git a/spec/support/table_helpers/example_methods.rb b/spec/support/table_helpers/example_methods.rbindex cb9f6f04c7f..e55c693a035 100644--- a/spec/support/table_helpers/example_methods.rb+++ b/spec/support/table_helpers/example_methods.rb@@ -51,13 +51,42 @@ module TableHelpers# Expect the given work packages to match a visual table representation.#+ # It uses +match_table+ internally. It does not reload the work packages+ # before comparing. To reload, use `expect_work_packages_after_reload`+ #+ # For instance:+ #+ # it 'is scheduled' do+ # expect_work_packages(work_packages, <<~TABLE)+ # subject | work | derived work |+ # parent | 1h | 3h |+ # child | 2h | 2h |+ # TABLE+ # end+ #+ # is equivalent to:+ #+ # it 'is scheduled' do+ # expect(work_packages).to match_table(<<~TABLE)+ # subject | work | derived work |+ # parent | 1h | 3h |+ # child | 2h | 2h |+ # TABLE+ # end+ def expect_work_packages(work_packages, table_representation)+ expect(work_packages).to match_table(table_representation)+ end++ # Expect the given work packages to match a visual table representation+ # after being reloaded.+ ## It uses +match_table+ internally and reloads the work packages from# database before comparing.## For instance:## it 'is scheduled' do- # expect_work_packages(work_packages, <<~TABLE)+ # expect_work_packages_after_reload(work_packages, <<~TABLE)# subject | work | derived work |# parent | 1h | 3h |# child | 2h | 2h |@@ -74,9 +103,9 @@ module TableHelpers# child | 2h | 2h |# TABLE# end- def expect_work_packages(work_packages, table_representation)+ def expect_work_packages_after_reload(work_packages, table_representation)work_packages.each(&:reload)- expect(work_packages).to match_table(table_representation)+ expect_work_packages(work_packages, table_representation)endendenddiff --git a/spec/support/table_helpers/table_data.rb b/spec/support/table_helpers/table_data.rbindex fa4a8a6b572..136ba7752fb 100644--- a/spec/support/table_helpers/table_data.rb+++ b/spec/support/table_helpers/table_data.rb@@ -101,9 +101,13 @@ module TableHelpersenddef create- table_data.work_package_identifiers.map do |identifier|+ table_data.work_package_identifiers.each do |identifier|create_work_package(identifier)end+ # create relations only after having created all work packages+ table_data.work_package_identifiers.each do |identifier| # rubocop:disable Style/CombinableLoops+ create_follows_relations(identifier)+ endwork_packages_by_identifierend@@ -118,6 +122,20 @@ module TableHelpersendend+ def create_follows_relations(identifier)+ relations = work_package_relations(identifier)+ relations.each do |relation|+ predecessor = work_packages_by_identifier[relation[:predecessor].to_sym]+ follower = work_packages_by_identifier[identifier]+ FactoryBot.create(+ :follows_relation,+ from: follower,+ to: predecessor,+ lag: relation[:lag]+ )+ end+ end+def lookup_parent(identifier)if identifier@work_packages_by_identifier[identifier] || create_work_package(identifier)@@ -137,9 +155,16 @@ module TableHelpers@statuses_by_name ||= Status.all.index_by(&:name)end+ def work_package_data(identifier)+ table_data.work_packages_data.find { |wpa| wpa[:identifier] == identifier.to_sym }+ end+def work_package_attributes(identifier)- data = table_data.work_packages_data.find { |wpa| wpa[:identifier] == identifier.to_sym }- data[:attributes]+ work_package_data(identifier)[:attributes]+ end++ def work_package_relations(identifier)+ work_package_data(identifier)[:relations] || []endendenddiff --git a/spec/support/table_helpers/table_parser.rb b/spec/support/table_helpers/table_parser.rbindex f7c8735ea47..33dbe77ec68 100644--- a/spec/support/table_helpers/table_parser.rb+++ b/spec/support/table_helpers/table_parser.rb@@ -31,26 +31,42 @@module TableHelpersclass TableParserdef parse(representation)+ work_packages_data = parse_representation(representation)+ populate_work_package_data(work_packages_data)+ end++ def parse_representation(representation)headers, *rows = representation.split("\n").filter_map { |line| split_line_into_cells(line) }- work_packages_data = rows.map.with_index do |cells, index|+ rows.map.with_index do |cells, index|if cells.size > headers.sizeraise ArgumentError, "Too many cells in row #{index + 1}, have you forgotten some headers?"end+ cells << "" while cells.size < headers.size{attributes: {},index:,row: headers.zip(cells).to_h}end- headers.each do |header|- column = Column.for(header)+ end++ private++ def populate_work_package_data(work_packages_data)+ columns(work_packages_data).each do |column|column.read_and_update_work_packages_data(work_packages_data)endwork_packages_dataend- private+ def headers(work_packages_data)+ work_packages_data.first[:row].keys+ end++ def columns(work_packages_data)+ work_packages_data.first[:row].keys.map { |key| Column.for(key) }+ enddef split_line_into_cells(line)case linediff --git a/spec/support/table_helpers/table_representer.rb b/spec/support/table_helpers/table_representer.rbindex 126ea1fca8c..31b1123c9d4 100644--- a/spec/support/table_helpers/table_representer.rb+++ b/spec/support/table_helpers/table_representer.rb@@ -35,29 +35,107 @@ module TableHelpers@columns = columnsend- # rubocop:disable Style/MultilineBlockChaindef render(table_data)- column_and_cell_sizes- .map do |column, cell_size|- header = column.title.ljust(cell_size)- cells = table_data.values_for_attribute(column.attribute).map { column.cell_format(_1, cell_size) }- [header, *cells]- end+ columns+ .map { |column| formatted_cells_for_column(column, table_data) }.transpose.map { |row| "| #{row.join(' | ')} |\n" }.joinend- # rubocop:enable Style/MultilineBlockChain++ def formatted_cells_for_column(column, table_data)+ get_header_and_values(column, table_data)+ .then { |cells| normalize_width(cells, column) }+ end++ def get_header_and_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)+ values = start_dates.zip(due_dates).map do |start_date, due_date|+ schedule_column_representer.span(start_date, due_date)+ end+ else+ header = column.title+ values = table_data+ .values_for_attribute(column.attribute)+ .map! { column.format(_1) }+ end+ [header, *values]+ end++ def normalize_width(cells, column)+ header, *values = cells+ width = column_width(column)+ header = header.ljust(width)+ values.map! { column.align(_1, width) }+ [header, *values]+ endprivate- def column_and_cell_sizes- @column_and_cell_sizes ||=+ def column_width(column)+ column_widths[column]+ end++ # Calculate the width of each column given values from all tables data.+ def column_widths+ @column_widths ||=columns.index_with do |column|- 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].max+ 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].max+ endendend++ 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) }+ ScheduleColumnFormatter.new(start_dates, due_dates)+ end+ end++ class ScheduleColumnFormatter+ attr_reader :monday, :first_day, :last_day++ def initialize(start_dates, due_dates)+ @monday = Date.current.next_occurring(:monday)+ all_dates = start_dates + due_dates + [@monday, @monday + 6]+ @first_day, @last_day = all_dates.flatten.compact.minmax+ end++ def column_size+ (first_day..last_day).count+ end++ def column_title+ spaced_at(monday, "MTWTFSS")+ end++ def span(start_date, due_date)+ if start_date.nil? && due_date.nil?+ " " * column_size+ elsif due_date.nil?+ spaced_at(start_date, "[")+ elsif start_date.nil?+ spaced_at(due_date, "]")+ else+ span = "X" * (start_date..due_date).count+ spaced_at(start_date, span)+ end+ end++ def spaced_at(date, text)+ nb_days = date - first_day+ spaced = (" " * nb_days) + text+ spaced.ljust(column_size)+ end+ endendenddiff --git a/spec/support_spec/table_helpers/column_type/duration_spec.rb b/spec/support_spec/table_helpers/column_type/duration_spec.rbindex 372185a0414..85d0f25b43a 100644--- a/spec/support_spec/table_helpers/column_type/duration_spec.rb+++ b/spec/support_spec/table_helpers/column_type/duration_spec.rb@@ -54,11 +54,11 @@ module TableHelpers::ColumnTypeendend- describe "#cell_format" do+ describe "#align" doit "renders the duration on the right side of the cell" do- 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"+ expect(column_type.align("3.5h", 0)).to eq "3.5h"+ expect(column_type.align("3.5h", 10)).to eq " 3.5h"+ expect(column_type.align("3.5h", 20)).to eq " 3.5h"endendenddiff --git a/spec/support_spec/table_helpers/column_type/generic_spec.rb b/spec/support_spec/table_helpers/column_type/generic_spec.rbindex 93ef294f561..f9c1f841373 100644--- a/spec/support_spec/table_helpers/column_type/generic_spec.rb+++ b/spec/support_spec/table_helpers/column_type/generic_spec.rb@@ -44,11 +44,11 @@ module TableHelpers::ColumnTypeendend- describe "#cell_format" do+ describe "#align" doit "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 "+ expect(column_type.align("hello", 0)).to eq "hello"+ expect(column_type.align("hello", 10)).to eq "hello "+ expect(column_type.align("hello", 20)).to eq "hello "endendenddiff --git a/spec/support_spec/table_helpers/column_type/percentage_spec.rb b/spec/support_spec/table_helpers/column_type/percentage_spec.rbindex b4050e16c1d..b2b32c72f42 100644--- a/spec/support_spec/table_helpers/column_type/percentage_spec.rb+++ b/spec/support_spec/table_helpers/column_type/percentage_spec.rb@@ -51,11 +51,11 @@ module TableHelpers::ColumnTypeendend- describe "#cell_format" do+ describe "#align" doit "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%"+ expect(column_type.align("20%", 0)).to eq "20%"+ expect(column_type.align("35%", 10)).to eq " 35%"+ expect(column_type.align("57%", 20)).to eq " 57%"endendenddiff --git a/spec/support_spec/table_helpers/column_type/properties_spec.rb b/spec/support_spec/table_helpers/column_type/properties_spec.rbnew file mode 100644index 00000000000..094fa7e305e--- /dev/null+++ b/spec/support_spec/table_helpers/column_type/properties_spec.rb@@ -0,0 +1,91 @@+# 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 Properties do+ subject(:column_type) { described_class.new }++ def parsed_data(table)+ TableHelpers::TableParser.new.parse(table)+ end++ describe "empty" do+ it "stores nothing when empty" do+ work_package_data = parsed_data(<<~TABLE).first+ | properties |+ | |+ TABLE+ expect(work_package_data[:relations]).to be_nil+ expect(work_package_data[:attributes]).to be_empty++ work_package_data = parsed_data(<<~TABLE).first+ | properties+ |+ TABLE+ expect(work_package_data[:relations]).to be_nil+ expect(work_package_data[:attributes]).to be_empty+ end+ end++ describe "follows <predecessor> [with lag <nb_days>]" do+ it "stores follows relations in work_package_data" do+ work_package_data = parsed_data(<<~TABLE).first+ | properties |+ | follows main with lag 3 |+ TABLE+ expect(work_package_data[:relations])+ .to eq([{ raw: "follows main with lag 3", type: :follows, predecessor: "main", lag: 3 }])+ end++ it "has a default lag of 0 days when not specified" do+ work_package_data = parsed_data(<<~TABLE).first+ | properties |+ | follows main |+ TABLE+ expect(work_package_data[:relations])+ .to eq([{ raw: "follows main", type: :follows, predecessor: "main", lag: 0 }])+ end++ it "can store multiple relations" do+ work_package_data = parsed_data(<<~TABLE).first+ | properties |+ | follows wp1, follows wp2 |+ TABLE+ expect(work_package_data[:relations])+ .to eq([+ { raw: "follows wp1", type: :follows, predecessor: "wp1", lag: 0 },+ { raw: "follows wp2", type: :follows, predecessor: "wp2", lag: 0 }+ ])+ end+ end+ end+enddiff --git a/spec/support_spec/table_helpers/column_type/schedule_spec.rb b/spec/support_spec/table_helpers/column_type/schedule_spec.rbnew file mode 100644index 00000000000..011a0b55e17--- /dev/null+++ b/spec/support_spec/table_helpers/column_type/schedule_spec.rb@@ -0,0 +1,131 @@+# 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 Schedule do+ let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June+ let(:tuesday) { Date.new(2022, 6, 21) }+ let(:wednesday) { Date.new(2022, 6, 22) }+ let(:thursday) { Date.new(2022, 6, 23) }+ let(:friday) { Date.new(2022, 6, 24) }+ let(:saturday) { Date.new(2022, 6, 25) }+ let(:sunday) { Date.new(2022, 6, 26) }++ def parsed_attributes(table)+ work_packages_data = TableHelpers::TableParser.new.parse(table)+ work_packages_data.pluck(:attributes)+ end++ before do+ travel_to(fake_today)+ end++ describe "origin day" do+ it "is identified by the 'M' in 'MTWTFSS' in the header and corresponds to the next monday" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | X |+ TABLE+ .to eq([{ start_date: monday, due_date: monday }])+ end++ it "is not identified by mtwtfss which can be used as documentation instead" do+ expect(parsed_attributes(<<~TABLE))+ | mtwtfssMTWTFSSmtwtfss |+ | X |+ TABLE+ .to eq([{ start_date: monday, due_date: monday }])+ end+ end++ describe "work package dates extraction" do+ def parsed_attributes(table)+ work_packages_data = TableHelpers::TableParser.new.parse(table)+ work_packages_data.pluck(:attributes)+ end++ it "recognizes multiple 'X' as the duration spanning from start date and end date" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | XX |+ TABLE+ .to eq([{ start_date: monday, due_date: tuesday }])+ end++ it "recognizes start date and end date outside of the reference week" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | XXXXXX |+ | XXXXXX |+ TABLE+ .to eq([+ { start_date: thursday, due_date: tuesday + 7.days },+ { start_date: thursday - 7.days, due_date: tuesday }+ ])+ end++ it "recognizes '[' as start date, making the due date nil if there are no 'X' or ']' after it" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | [ |+ TABLE+ .to eq([{ start_date: tuesday, due_date: nil }])+ end++ it "recognizes ']' as end date, making the start date nil if there are no 'X' or '[' before it" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | ] |+ TABLE+ .to eq([{ start_date: nil, due_date: thursday }])+ end++ it "sets start date and due date to nil if there are no 'X', '[', or ']' at all" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | |+ TABLE+ .to eq([{ start_date: nil, due_date: nil }])+ end++ it "ignores characters other than 'X', '[', or ']', allowing to use other characters to " \+ "represent other things (like '.' for non-working days)" do+ expect(parsed_attributes(<<~TABLE))+ | MTWTFSS |+ | XX..XX |+ TABLE+ .to include(start_date: thursday, due_date: tuesday + 7.days)+ end+ end+ end+enddiff --git a/spec/support_spec/table_helpers/table_data_spec.rb b/spec/support_spec/table_helpers/table_data_spec.rbindex fe3fd0adcff..edb060e9e61 100644--- a/spec/support_spec/table_helpers/table_data_spec.rb+++ b/spec/support_spec/table_helpers/table_data_spec.rb@@ -71,6 +71,31 @@ module TableHelpersexpect(table_data.values_for_attribute(:remaining_hours)).to eq([3.0, nil])expect(table_data.work_package_identifiers).to eq(%i[work_package another_one])end++ it "can read schedule column data from work packages" do+ expected_table = <<~TABLE+ | subject | MTWTFSS |+ | work package 1 | XXX |+ | work package 2 | ] |+ TABLE++ columns = described_class.for(expected_table).columns+ monday = Date.current.next_occurring(:monday)+ work_package1 = build(:work_package, subject: "work package 1",+ start_date: monday - 2, due_date: monday)+ work_package2 = build(:work_package, subject: "work package 2",+ start_date: nil, due_date: monday + 4)++ table_data = described_class.from_work_packages([work_package1, work_package2], columns)+ expect(table_data.work_packages_data.size).to eq(2)+ expect(table_data.columns.size).to eq(2)+ expect(table_data.headers).to eq(["subject", "MTWTFSS"])+ expect(table_data.values_for_attribute(:start_date))+ .to eq([monday - 2, nil])+ expect(table_data.values_for_attribute(:due_date))+ .to eq([monday, monday + 4])+ expect(table_data.work_package_identifiers).to eq(%i[work_package1 work_package2])+ endenddescribe "#headers" do@@ -112,18 +137,43 @@ module TableHelpersenddescribe "#create_work_packages" do+ let(:monday) { Date.current.next_occurring(:monday) }+it "creates work packages out of the table data" dostatus = create(:status, name: "To do")table_representation = <<~TABLE- subject | status | work |- My wp | To do | 5h |+ subject | status | work | MTWTFSS |+ My wp | To do | 5h | XXX |TABLEtable_data = described_class.for(table_representation)table = table_data.create_work_packagesexpect(table.work_packages.count).to eq(1)expect(table.work_package(:my_wp))- .to have_attributes(subject: "My wp", status:, estimated_hours: 5.0)+ .to have_attributes(+ subject: "My wp",+ status:,+ estimated_hours: 5.0,+ start_date: monday,+ due_date: monday + 2.days+ )+ end++ it "creates relations between work packages out of the table data" do+ table_representation = <<~TABLE+ subject | properties+ main |+ follower | follows main with lag 2+ TABLE++ table_data = described_class.for(table_representation)+ table = table_data.create_work_packages+ expect(table.work_packages.count).to eq(2)+ main = table.work_package(:main)+ follower = table.work_package(:follower)+ expect(follower.follows_relations.count).to eq(1)+ expect(follower.follows_relations.first.to).to eq(main)+ expect(follower.follows_relations.first.lag).to eq(2)endit "raises an error if a given status name does not exist" dodiff --git a/spec/support_spec/table_helpers/table_parser_spec.rb b/spec/support_spec/table_helpers/table_parser_spec.rbindex 4512646fb1f..f45cdb63a1c 100644--- a/spec/support_spec/table_helpers/table_parser_spec.rb+++ b/spec/support_spec/table_helpers/table_parser_spec.rb@@ -82,13 +82,15 @@ RSpec.describe TableHelpers::TableParser do.to raise_error(ArgumentError, "Too many cells in row 1, have you forgotten some headers?")end- it "is ok to have more headers than cells (value of missing cells will be nil)" do+ it "is ok to have more headers than cells (raw values of missing cells will be empty strings)" dotable = <<~TABLEsubject | work | remaining workwp | 4hTABLEparsed_data = described_class.new.parse(table)+ expect(parsed_data.dig(0, :row, " work ")).to eq(" 4h")expect(parsed_data.dig(0, :attributes, :estimated_hours)).to eq(4.0)+ expect(parsed_data.dig(0, :row, " remaining work")).to eq("")expect(parsed_data.dig(0, :attributes, :remaining_hours)).to be_nilenddiff --git a/spec/support_spec/table_helpers/table_representer_spec.rb b/spec/support_spec/table_helpers/table_representer_spec.rbindex 02db8fa197e..575b4c91b51 100644--- a/spec/support_spec/table_helpers/table_representer_spec.rb+++ b/spec/support_spec/table_helpers/table_representer_spec.rb@@ -100,5 +100,68 @@ module TableHelpersTABLEendend++ describe "schedule column" do+ let(:table) do+ <<~TABLE+ | subject | MTWTFSS |+ | wp1 | X |+ | wp2 | [ |+ | wp3 | ] |+ | wp4 | |+ | wp5 | XXX |+ | wp6 | X |+ | wp7 | X |+ | wp8 | X |+ TABLE+ end+ let(:columns) { [Column.for("MTWTFSS")] }++ it "is rendered as a schedule" do+ expect(representer.render(table_data)).to eq <<~TABLE+ | MTWTFSS |+ | X |+ | [ |+ | ] |+ | |+ | XXX |+ | X |+ | X |+ | X |+ TABLE+ end++ context "when using a second table for the size" do+ let(:twin_table) do+ <<~TABLE+ | subject | MTWTFSS |+ | wp5 | XXX |+ | wp9 | XX |+ 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+ expect(representer.render(table_data)).to eq <<~TABLE+ | MTWTFSS |+ | X |+ | [ |+ | ] |+ | |+ | XXX |+ | X |+ | X |+ | X |+ TABLE+ expect(representer.render(twin_table_data)).to eq <<~TABLE+ | MTWTFSS |+ | XXX |+ | XX |+ TABLE+ end+ end+ endendenddiff --git a/spec/workers/work_packages/progress/apply_statuses_change_job_spec.rb b/spec/workers/work_packages/progress/apply_statuses_change_job_spec.rbindex a626c2d08e5..464f3f37674 100644--- a/spec/workers/work_packages/progress/apply_statuses_change_job_spec.rb+++ b/spec/workers/work_packages/progress/apply_statuses_change_job_spec.rb@@ -65,8 +65,7 @@ RSpec.describe WorkPackages::Progress::ApplyStatusesChangeJob dojob.perform_now(cause_type:, status_name:, status_id:, changes:)- table.work_packages.map(&:reload)- expect_work_packages(table.work_packages, to)+ expect_work_packages_after_reload(table.work_packages, to)table.work_packagesenddiff --git a/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rb b/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rbindex 63527a3a1a2..7dd51602129 100644--- a/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rb+++ b/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rb@@ -63,8 +63,7 @@ RSpec.describe WorkPackages::Progress::ApplyTotalPercentCompleteModeChangeJob dojob.perform_now(cause_type:, mode:)- table.work_packages.map(&:reload)- expect_work_packages(table.work_packages, to)+ expect_work_packages_after_reload(table.work_packages, to)table.work_packagesenddiff --git a/spec/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job_spec.rb b/spec/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job_spec.rbindex a9c88dbd37d..3fe40e92d1a 100644--- a/spec/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job_spec.rb+++ b/spec/workers/work_packages/progress/migrate_remove_totals_from_childless_work_packages_job_spec.rb@@ -46,7 +46,7 @@ RSpec.describe WorkPackages::Progress::MigrateRemoveTotalsFromChildlessWorkPackadef expect_performing_job_changes(from:, to:)table = create_table(from)job.perform_now- expect_work_packages(table.work_packages.map(&:reload), to)+ expect_work_packages_after_reload(table.work_packages, to)table.work_packagesend