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

[59539] switch scheduling mode when modifying follows relations

Christophe Bliard26fbc5522a08
diff --git a/app/models/relation.rb b/app/models/relation.rb
index 88c2de6ee7c..89e31241965 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -105,6 +105,12 @@ class Relation < ApplicationRecord
scope :follows_with_lag,
-> { follows.where("lag > 0") }
+ scope :of_successor,
+ ->(work_package) { where(from: work_package) }
+
+ scope :not_of_predecessor,
+ ->(work_package) { where.not(to: work_package) }
+
validates :lag, numericality: {
allow_nil: true,
less_than_or_equal_to: 2_147_483_647,
diff --git a/app/models/work_packages/scopes/for_scheduling.rb b/app/models/work_packages/scopes/for_scheduling.rb
index 9cf41621a0b..710003f61ea 100644
--- a/app/models/work_packages/scopes/for_scheduling.rb
+++ b/app/models/work_packages/scopes/for_scheduling.rb
@@ -30,6 +30,7 @@
module WorkPackages::Scopes
module ForScheduling
extend ActiveSupport::Concern
+ using CoreExtensions::SquishSql
class_methods do
# Fetches all work packages that need to be evaluated for eventual
@@ -108,13 +109,13 @@ module WorkPackages::Scopes
# @param work_packages WorkPackage[] A set of work packages for which the
# set of related work packages that might be subject to reschedule is
# fetched.
- def for_scheduling(work_packages)
+ def for_scheduling(work_packages, switching_to_automatic_mode: [])
return none if work_packages.empty?
sql = <<~SQL.squish
WITH
RECURSIVE
- #{scheduling_paths_sql(work_packages)}
+ #{scheduling_paths_sql(work_packages, switching_to_automatic_mode:)}
SELECT id
FROM to_schedule
@@ -159,7 +160,11 @@ module WorkPackages::Scopes
#
# Paths whose ending work package is marked to be manually scheduled are
# not joined with any more.
- def scheduling_paths_sql(work_packages)
+ def scheduling_paths_sql(work_packages, switching_to_automatic_mode: [])
+ automatic_ids = switching_to_automatic_mode.map do |wp|
+ ::OpenProject::SqlSanitization.sanitize("(:id)", id: wp.id)
+ end.join(", ")
+
values = work_packages.map do |wp|
::OpenProject::SqlSanitization
.sanitize "(:id, false, false, true)",
@@ -167,6 +172,13 @@ module WorkPackages::Scopes
end.join(", ")
<<~SQL.squish
+ -- All work packages that are switching to automatic scheduling mode
+ -- but are still seen as manually scheduled from the database's perspective.
+ switching_to_automatic_mode (id) AS (
+ SELECT id::bigint FROM (VALUES #{automatic_ids.presence || '(NULL)'}) AS t(id)
+ ),
+
+ -- recursively fetch all work packages that are eligible for rescheduling
to_schedule (id, manually, hierarchy_up, origin) AS (
SELECT * FROM (VALUES#{values}) AS t(id, manually, hierarchy_up, origin)
@@ -175,9 +187,14 @@ module WorkPackages::Scopes
SELECT
relations.from_id id,
- (related_work_packages.schedule_manually
- OR (COALESCE(descendants.manually, false)
- AND NOT (to_schedule.origin AND relations.hierarchy_up))
+ (
+ (
+ related_work_packages.schedule_manually
+ AND switching_to_automatic_mode.id IS NULL
+ ) OR (
+ COALESCE(descendants.manually, false)
+ AND NOT (to_schedule.origin AND relations.hierarchy_up)
+ )
) manually,
relations.hierarchy_up,
false origin
@@ -211,6 +228,8 @@ module WorkPackages::Scopes
) relations ON relations.to_id = to_schedule.id
LEFT JOIN work_packages related_work_packages
ON relations.from_id = related_work_packages.id
+ LEFT JOIN switching_to_automatic_mode
+ ON related_work_packages.id = switching_to_automatic_mode.id
LEFT JOIN LATERAL (
SELECT
descendant_hierarchies.ancestor_id from_id,
diff --git a/app/services/relations/base_service.rb b/app/services/relations/base_service.rb
index 7a657b5d4ab..1fe76bf3e7d 100644
--- a/app/services/relations/base_service.rb
+++ b/app/services/relations/base_service.rb
@@ -33,6 +33,7 @@ class Relations::BaseService < BaseServices::BaseCallable
attr_accessor :user
def initialize(user:)
+ super()
self.user = user
end
@@ -54,20 +55,22 @@ class Relations::BaseService < BaseServices::BaseCallable
end
def set_defaults(model)
- if Relation::TYPE_FOLLOWS == model.relation_type
+ if model.follows?
model.lag ||= 0
else
model.lag = nil
end
end
- def reschedule(model)
+ def reschedule(relation)
schedule_result = WorkPackages::SetScheduleService
- .new(user:, work_package: model.to)
+ .new(user:,
+ work_package: relation.predecessor,
+ switching_to_automatic_mode: switching_to_automatic_mode(relation))
.call
- # The to-work_package will not be altered by the schedule service so
- # we do not have to save the result of the service.
+ # The predecessor work package will not be altered by the schedule service so
+ # we do not have to save the result of the service, only the dependent results.
save_result = if schedule_result.success?
schedule_result.dependent_results.all? { |dr| !dr.result.changed? || dr.result.save(validate: false) }
end || false
@@ -76,4 +79,32 @@ class Relations::BaseService < BaseServices::BaseCallable
schedule_result
end
+
+ def switching_to_automatic_mode(relation)
+ if should_switch_successor_to_automatic_mode?(relation)
+ [relation.successor]
+ else
+ []
+ end
+ end
+
+ def should_switch_successor_to_automatic_mode?(relation)
+ relation.follows? \
+ && creating? \
+ && last_successor_relation?(relation) \
+ && has_no_children?(relation.successor)
+ end
+
+ def creating?
+ self.class.name.include?("Create")
+ end
+
+ def last_successor_relation?(relation)
+ Relation.follows.of_successor(relation.successor)
+ .not_of_predecessor(relation.predecessor).none?
+ end
+
+ def has_no_children?(work_package)
+ !WorkPackage.exists?(parent: work_package)
+ end
end
diff --git a/app/services/relations/delete_service.rb b/app/services/relations/delete_service.rb
index 51074fda7e7..6d89133a4ab 100644
--- a/app/services/relations/delete_service.rb
+++ b/app/services/relations/delete_service.rb
@@ -26,4 +26,32 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Relations::DeleteService < BaseServices::Delete; end
+class Relations::DeleteService < BaseServices::Delete
+ def after_perform(_result)
+ result = super
+ if result.success? && successor_must_switch_to_manual_mode?
+ deleted_relation.successor.update(schedule_manually: true)
+ end
+ result
+ end
+
+ private
+
+ def deleted_relation
+ model
+ end
+
+ def successor_must_switch_to_manual_mode?
+ deleted_relation.follows? \
+ && successor_has_dates? \
+ && was_last_relation_to_the_successor?
+ end
+
+ def successor_has_dates?
+ deleted_relation.successor.start_date.present? || deleted_relation.successor.due_date.present?
+ end
+
+ def was_last_relation_to_the_successor?
+ Relation.follows.of_successor(deleted_relation.successor).none?
+ end
+end
diff --git a/app/services/work_packages/schedule_dependency.rb b/app/services/work_packages/schedule_dependency.rb
index ed6976377a0..a87980fd37f 100644
--- a/app/services/work_packages/schedule_dependency.rb
+++ b/app/services/work_packages/schedule_dependency.rb
@@ -41,10 +41,11 @@
# package, but are necessary to accurately determine the new start and due
# dates of the moving work packages.
class WorkPackages::ScheduleDependency
- attr_accessor :dependencies
+ attr_accessor :dependencies, :switching_to_automatic_mode
- def initialize(moved_work_packages)
+ def initialize(moved_work_packages, switching_to_automatic_mode: [])
self.moved_work_packages = Array(moved_work_packages)
+ self.switching_to_automatic_mode = Array(switching_to_automatic_mode)
preload_scheduling_data
@@ -136,7 +137,7 @@ class WorkPackages::ScheduleDependency
def moving_work_packages
@moving_work_packages ||= WorkPackage
- .for_scheduling(moved_work_packages)
+ .for_scheduling(moved_work_packages, switching_to_automatic_mode:)
end
# All work packages preloaded during initialization.
@@ -166,6 +167,8 @@ class WorkPackages::ScheduleDependency
# rehydrate the predecessors and followers of follows relations
rehydrate_follows_relations
+
+ fix_switching_to_automatic_mode_work_packages
end
# Returns all the descendants of moved and moving work packages that are not
@@ -207,4 +210,13 @@ class WorkPackages::ScheduleDependency
relation.to = work_package_by_id(relation.to_id)
end
end
+
+ def fix_switching_to_automatic_mode_work_packages
+ ids = switching_to_automatic_mode.map(&:id)
+ known_work_packages.each do |work_package|
+ if ids.include?(work_package.id)
+ work_package.schedule_manually = false
+ end
+ end
+ end
end
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index 8969656962d..a6b2108c490 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -27,12 +27,13 @@
#++
class WorkPackages::SetScheduleService
- attr_accessor :user, :work_packages, :initiated_by
+ attr_accessor :user, :work_packages, :initiated_by, :switching_to_automatic_mode
- def initialize(user:, work_package:, initiated_by: nil)
+ def initialize(user:, work_package:, initiated_by: nil, switching_to_automatic_mode: [])
self.user = user
self.work_packages = Array(work_package)
self.initiated_by = initiated_by
+ self.switching_to_automatic_mode = switching_to_automatic_mode
end
def call(changed_attributes = %i(start_date due_date))
@@ -95,7 +96,7 @@ class WorkPackages::SetScheduleService
def schedule_following
altered = []
- WorkPackages::ScheduleDependency.new(work_packages).in_schedule_order do |scheduled, dependency|
+ WorkPackages::ScheduleDependency.new(work_packages, switching_to_automatic_mode:).in_schedule_order do |scheduled, dependency|
reschedule(scheduled, dependency)
altered << scheduled if scheduled.changed?
diff --git a/spec/services/relations/scheduling_mode_switching_spec.rb b/spec/services/relations/scheduling_mode_switching_spec.rb
new file mode 100644
index 00000000000..821bb3f8d62
--- /dev/null
+++ b/spec/services/relations/scheduling_mode_switching_spec.rb
@@ -0,0 +1,331 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe "Scheduling mode switching", # rubocop:disable RSpec/DescribeClass
+ with_settings: { journal_aggregation_time_minutes: 0 } do
+ create_shared_association_defaults_for_work_package_factory
+
+ shared_let(:user) { create(:admin) }
+
+ context "when creating a non-follows relation" do
+ context "with 2 manually scheduled work packages" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ TABLE
+
+ before do
+ attributes = {
+ "relation_type" => "relates",
+ "from_id" => succ.id,
+ "to_id" => pred.id
+ }
+ Relations::CreateService.new(user:).call(attributes)
+ end
+
+ it "keeps work package scheduling mode" do
+ expect_work_packages_after_reload([pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ TABLE
+ expect(pred.journals.count).to eq(1)
+ expect(succ.journals.count).to eq(1)
+ end
+ end
+ end
+
+ context "when creating a follows relation" do
+ context "with 2 manually scheduled work packages" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ TABLE
+
+ before do
+ attributes = {
+ "relation_type" => "follows",
+ "from_id" => succ.id,
+ "to_id" => pred.id
+ }
+ Relations::CreateService.new(user:).call(attributes)
+ end
+
+ it "switches successor scheduling mode to automatic and reschedules it accordingly" do
+ expect_work_packages_after_reload([pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | automatic |
+ TABLE
+ expect(pred.journals.count).to eq(1)
+ expect(succ.journals.count).to eq(2)
+ end
+ end
+
+ context "with a precedes relation with 2 manually scheduled work packages" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ TABLE
+
+ before do
+ attributes = {
+ "relation_type" => "precedes",
+ "from_id" => pred.id,
+ "to_id" => succ.id
+ }
+ Relations::CreateService.new(user:).call(attributes)
+ end
+
+ it "switches successor scheduling mode to automatic and reschedules it accordingly" do
+ expect_work_packages_after_reload([pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | automatic |
+ TABLE
+ expect(pred.journals.count).to eq(1)
+ expect(succ.journals.count).to eq(2)
+ end
+ end
+
+ context "with work package being manually scheduled and having an already existing predecessor" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode | predecessors
+ | work package | XX | manual | existing pred
+ | existing pred | XX | manual |
+ | another pred | XX | manual |
+ TABLE
+
+ before do
+ attributes = {
+ "relation_type" => "follows",
+ "from_id" => work_package.id, # successor
+ "to_id" => another_pred.id # predecessor
+ }
+ Relations::CreateService.new(user:).call(attributes)
+ end
+
+ it "keeps work package scheduling mode (manual) and does not reschedule" do
+ expect_work_packages_after_reload([work_package, existing_pred, another_pred], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | work package | XX | manual |
+ | existing pred | XX | manual |
+ | another pred | XX | manual |
+ TABLE
+ expect(work_package.journals.count).to eq(1)
+ end
+ end
+
+ context "with work package being manually scheduled and having already at least one child" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode |
+ | work package | XX | manual |
+ | existing child | XX | manual |
+ | pred | XX | manual |
+ TABLE
+
+ before do
+ attributes = {
+ "relation_type" => "follows",
+ "from_id" => work_package.id, # successor
+ "to_id" => pred.id # predecessor
+ }
+ Relations::CreateService.new(user:).call(attributes)
+ end
+
+ it "keeps work package scheduling mode (manual) and does not reschedule" do
+ expect_work_packages_after_reload([work_package, existing_child, pred], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | work package | XX | manual |
+ | existing child | XX | manual |
+ | pred | XX | manual |
+ TABLE
+ expect(work_package.journals.count).to eq(1)
+ end
+ end
+ end
+
+ ## TODO: Add tests for changing the relation type (currently supported by API only)
+ context "when updating an existing follows relation" do
+ context "with 2 manually scheduled work packages" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode | predecessors
+ | pred | XX | manual |
+ | succ | XX | manual | pred
+ TABLE
+
+ before do
+ relation = Relation.last
+ update_attributes = {
+ "description" => "my description"
+ }
+ Relations::UpdateService.new(user:, model: relation).call(update_attributes)
+ end
+
+ it "keeps work package scheduling mode" do
+ expect_work_packages_after_reload([pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ TABLE
+ expect(Relation.last.description).to eq("my description")
+ expect(pred.journals.count).to eq(1)
+ expect(succ.journals.count).to eq(1)
+ end
+ end
+ end
+
+ context "when deleting a non-follows relation" do
+ context "with an automatically scheduled successor for which it's the last relation" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode | related to
+ | wp1 | XX | manual |
+ | wp2 | XX | automatic | wp1
+ TABLE
+
+ before do
+ relation = Relation.last
+ Relations::DeleteService.new(user:, model: relation).call
+ end
+
+ it "does not switch work package scheduling mode" do
+ expect(Relation.count).to eq(0)
+ expect_work_packages_after_reload([wp1, wp2], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | wp1 | XX | manual |
+ | wp2 | XX | automatic |
+ TABLE
+ expect(wp2.journals.count).to eq(1)
+ end
+ end
+ end
+
+ # TODO: Add the case where two relations exist, one is deleted and the successor needs rescheduling
+ context "when deleting a follows relation" do
+ context "with an automatically scheduled successor for which it's the last relation" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode | predecessors
+ | pred | XX | manual |
+ | succ | XX | automatic | pred
+ TABLE
+
+ before do
+ relation = Relation.last
+ Relations::DeleteService.new(user:, model: relation).call
+ end
+
+ it "switches work package to manual scheduling mode and keeps the dates" do
+ expect(Relation.count).to eq(0)
+ expect_work_packages_after_reload([pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ TABLE
+ expect(succ.journals.count).to eq(2)
+ end
+ end
+
+ context "with an automatically scheduled successor without any dates for which it's the last relation" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode | predecessors
+ | pred | | manual |
+ | succ | | automatic | pred
+ TABLE
+
+ before do
+ relation = _table.relation(predecessor: "pred", successor: "succ")
+ Relations::DeleteService.new(user:, model: relation).call
+ end
+
+ it "keeps work package scheduling mode" do
+ expect(Relation.count).to eq(0)
+ expect_work_packages_after_reload([pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | | manual |
+ | succ | | automatic |
+ TABLE
+ expect(succ.journals.count).to eq(1) # no modifications
+ end
+ end
+
+ context "with an automatically scheduled successor for which it's not the last relation" do
+ let_work_packages(<<~TABLE)
+ | subject | MTWTFSS | scheduling mode | predecessors
+ | pred | XX | manual |
+ | another pred | XX | manual |
+ | succ | XX | automatic | pred, another pred
+ TABLE
+
+ before do
+ relation = _table.relation(predecessor: "pred", successor: "succ")
+ Relations::DeleteService.new(user:, model: relation).call
+ end
+
+ it "keeps work package scheduling mode" do
+ expect(Relation.count).to eq(1)
+ expect_work_packages_after_reload([pred, another_pred, succ], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | another pred | XX | manual |
+ | succ | XX | automatic |
+ TABLE
+ expect(succ.journals.count).to eq(1) # no modifications
+ end
+ end
+
+ context "with an automatically scheduled successor which has at least one child" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode | predecessors
+ | pred | XX | manual |
+ | succ | XX | automatic | pred
+ | child | XX | manual |
+ TABLE
+
+ before do
+ relation = Relation.last
+ Relations::DeleteService.new(user:, model: relation).call
+ end
+
+ it "switches work package to manual scheduling mode and keeps the dates" do
+ expect(Relation.count).to eq(0)
+ expect_work_packages_after_reload([pred, succ, child], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode |
+ | pred | XX | manual |
+ | succ | XX | manual |
+ | child | XX | manual |
+ TABLE
+ expect(succ.journals.count).to eq(2)
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/column.rb b/spec/support/table_helpers/column.rb
index ac712a1de6e..6ef79ca360a 100644
--- a/spec/support/table_helpers/column.rb
+++ b/spec/support/table_helpers/column.rb
@@ -35,7 +35,8 @@ require_relative "column_type/days_counting"
require_relative "column_type/duration"
require_relative "column_type/hierarchy"
require_relative "column_type/percentage"
-require_relative "column_type/predecessors"
+require_relative "column_type/predecessor_relations"
+require_relative "column_type/related_to_relations"
require_relative "column_type/schedule"
require_relative "column_type/scheduling_mode"
require_relative "column_type/status"
@@ -54,7 +55,8 @@ module TableHelpers
derived_done_ratio: ColumnType::Percentage,
hierarchy: ColumnType::Hierarchy,
ignore_non_working_days: ColumnType::DaysCounting,
- predecessors: ColumnType::Predecessors,
+ predecessor_relations: ColumnType::PredecessorRelations,
+ related_to_relations: ColumnType::RelatedToRelations,
schedule: ColumnType::Schedule,
schedule_manually: ColumnType::SchedulingMode,
status: ColumnType::Status,
@@ -97,7 +99,9 @@ module TableHelpers
when /\s*scheduling mode\s*/
:schedule_manually
when /\s*predecessors\s*/
- :predecessors
+ :predecessor_relations
+ when /\s*relate[ds][ _]to\s*/
+ :related_to_relations
when /status/, /hierarchy/
to_identifier(header)
else
diff --git a/spec/support/table_helpers/column_type/predecessors.rb b/spec/support/table_helpers/column_type/predecessor_relations.rb
similarity index 85%
rename from spec/support/table_helpers/column_type/predecessors.rb
rename to spec/support/table_helpers/column_type/predecessor_relations.rb
index 24ef01cd943..a81e249fbd2 100644
--- a/spec/support/table_helpers/column_type/predecessors.rb
+++ b/spec/support/table_helpers/column_type/predecessor_relations.rb
@@ -48,7 +48,7 @@ module TableHelpers
#
# Adapted from (now deleted) original implementation
# in `spec/support/schedule_helpers/chart_builder.rb`.
- class Predecessors < Generic
+ class PredecessorRelations < Generic
def attributes_for_work_package(_attribute, _work_package)
{}
end
@@ -60,31 +60,20 @@ module TableHelpers
end
def parse_predecessors(predecessors)
- predecessors.reduce({}) do |data, predecessor|
- case parse_predecessor(predecessor)
- in {relations: relation}
- data[:relations] ||= []
- data[:relations] << relation
- end
- data
+ relations = predecessors.map do |predecessor|
+ parse_predecessor(predecessor)
end
- end
-
- def relations_for_raw_value(raw_value)
- raw_value.split
- {}
+ { relations: }.compact_blank
end
def parse_predecessor(predecessor)
case predecessor
when /^(?:follows)?\s*(.+?)(?: with lag (\d+))?\s*$/
{
- relations: {
- raw: predecessor,
- type: :follows,
- predecessor: $1,
- lag: $2.to_i
- }
+ raw: predecessor,
+ type: :follows,
+ with: $1,
+ lag: $2.to_i
}
else
spell_checker = DidYouMean::SpellChecker.new(
diff --git a/spec/support/table_helpers/column_type/related_to_relations.rb b/spec/support/table_helpers/column_type/related_to_relations.rb
new file mode 100644
index 00000000000..7c91b03ed6d
--- /dev/null
+++ b/spec/support/table_helpers/column_type/related_to_relations.rb
@@ -0,0 +1,69 @@
+# 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 relates/related to relations to work packages.
+ #
+ # Supported texts:
+ # - :wp
+ #
+ # They can be combined by separated them with commas: "wp1, wp2".
+ #
+ # Example:
+ #
+ # | subject | related to |
+ # | main | |
+ # | other one | main |
+ class RelatedToRelations < Generic
+ def attributes_for_work_package(_attribute, _work_package)
+ {}
+ end
+
+ def extract_data(_attribute, raw_header, work_package_data, _work_packages_data)
+ relations =
+ work_package_data.dig(:row, raw_header)
+ .split(",")
+ .map(&:strip)
+ .compact_blank
+ .map { |name| make_related_to_relation(name) }
+ { relations: }.compact_blank
+ end
+
+ def make_related_to_relation(name)
+ {
+ raw: name,
+ type: :relates,
+ with: name
+ }
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/table.rb b/spec/support/table_helpers/table.rb
index d31a1e0debb..aab27841cd6 100644
--- a/spec/support/table_helpers/table.rb
+++ b/spec/support/table_helpers/table.rb
@@ -42,8 +42,23 @@ module TableHelpers
@work_packages_by_identifier.values
end
- def relation(successor:)
- @relations.find { |relation| relation.follows? && relation.from.subject == successor }
+ # Finds a relation by its predecessor and/or successor.
+ #
+ # Example:
+ #
+ # relation(successor: "succ")
+ #
+ # will return the first created follows/precedes relation having the successor with subject "succ".
+ #
+ # @param predecessor [String, nil] the predecessor's subject name
+ # @param successor [String, nil] the successor's subjectname
+ # @return [Relation, nil] the relation or nil if no relation matches
+ def relation(predecessor: nil, successor: nil)
+ @relations.find do |relation|
+ relation.follows? \
+ && (predecessor.nil? || relation.predecessor.subject == predecessor) \
+ && (successor.nil? || relation.successor.subject == successor)
+ end
end
def relations
diff --git a/spec/support/table_helpers/table_data.rb b/spec/support/table_helpers/table_data.rb
index 2885dba4e48..b02e13bdce7 100644
--- a/spec/support/table_helpers/table_data.rb
+++ b/spec/support/table_helpers/table_data.rb
@@ -109,7 +109,7 @@ module TableHelpers
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)
+ create_relations(identifier)
end
[work_packages_by_identifier, relations]
end
@@ -125,15 +125,17 @@ module TableHelpers
end
end
- def create_follows_relations(identifier)
+ def create_relations(identifier)
work_package_relations(identifier).each do |relation|
- predecessor = find_work_package_by_name(relation[:predecessor])
- follower = work_packages_by_identifier[identifier]
+ to = find_work_package_by_name(relation[:with])
+ from = work_packages_by_identifier[identifier]
+ extra_attributes = { lag: relation[:lag] }.compact
relations << FactoryBot.create(
- :follows_relation,
- from: follower,
- to: predecessor,
- lag: relation[:lag]
+ :relation,
+ relation_type: relation[:type],
+ from:,
+ to:,
+ **extra_attributes
)
end
end
diff --git a/spec/support_spec/table_helpers/column_type/predecessors_spec.rb b/spec/support_spec/table_helpers/column_type/predecessor_relations_spec.rb
similarity index 80%
rename from spec/support_spec/table_helpers/column_type/predecessors_spec.rb
rename to spec/support_spec/table_helpers/column_type/predecessor_relations_spec.rb
index b62f486b85f..376479aca19 100644
--- a/spec/support_spec/table_helpers/column_type/predecessors_spec.rb
+++ b/spec/support_spec/table_helpers/column_type/predecessor_relations_spec.rb
@@ -31,7 +31,7 @@
require "spec_helper"
module TableHelpers::ColumnType
- RSpec.describe Predecessors do
+ RSpec.describe PredecessorRelations do
subject(:column_type) { described_class.new }
def parsed_data(table)
@@ -65,8 +65,8 @@ module TableHelpers::ColumnType
TABLE
expect(work_package_data)
.to eq([
- [{ raw: "follows main with lag 3", type: :follows, predecessor: "main", lag: 3 }],
- [{ raw: "main with lag 3", type: :follows, predecessor: "main", lag: 3 }]
+ [{ raw: "follows main with lag 3", type: :follows, with: "main", lag: 3 }],
+ [{ raw: "main with lag 3", type: :follows, with: "main", lag: 3 }]
])
end
@@ -78,8 +78,8 @@ module TableHelpers::ColumnType
TABLE
expect(work_package_data)
.to eq([
- [{ raw: "follows main", type: :follows, predecessor: "main", lag: 0 }],
- [{ raw: "main", type: :follows, predecessor: "main", lag: 0 }]
+ [{ raw: "follows main", type: :follows, with: "main", lag: 0 }],
+ [{ raw: "main", type: :follows, with: "main", lag: 0 }]
])
end
@@ -92,13 +92,13 @@ module TableHelpers::ColumnType
expect(work_package_data)
.to eq([
[
- { raw: "follows wp1", type: :follows, predecessor: "wp1", lag: 0 },
- { raw: "follows wp2", type: :follows, predecessor: "wp2", lag: 0 }
+ { raw: "follows wp1", type: :follows, with: "wp1", lag: 0 },
+ { raw: "follows wp2", type: :follows, with: "wp2", lag: 0 }
],
[
- { raw: "follows wp1", type: :follows, predecessor: "wp1", lag: 0 },
- { raw: "wp2", type: :follows, predecessor: "wp2", lag: 0 },
- { raw: "wp3", type: :follows, predecessor: "wp3", lag: 0 }
+ { raw: "follows wp1", type: :follows, with: "wp1", lag: 0 },
+ { raw: "wp2", type: :follows, with: "wp2", lag: 0 },
+ { raw: "wp3", type: :follows, with: "wp3", lag: 0 }
]
])
end
diff --git a/spec/support_spec/table_helpers/column_type/related_to_relations_spec.rb b/spec/support_spec/table_helpers/column_type/related_to_relations_spec.rb
new file mode 100644
index 00000000000..7a310932920
--- /dev/null
+++ b/spec/support_spec/table_helpers/column_type/related_to_relations_spec.rb
@@ -0,0 +1,87 @@
+# 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 RelatedToRelations 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
+ | related to |
+ | |
+ TABLE
+ expect(work_package_data[:relations]).to be_nil
+ expect(work_package_data[:attributes]).to be_empty
+
+ work_package_data = parsed_data(<<~TABLE).first
+ | relates to
+ |
+ TABLE
+ expect(work_package_data[:relations]).to be_nil
+ expect(work_package_data[:attributes]).to be_empty
+ end
+ end
+
+ describe "<work package name>" do
+ it "stores related_to relations in work_package_data" do
+ work_package_data = parsed_data(<<~TABLE).pluck(:relations)
+ | related to |
+ | main |
+ TABLE
+ expect(work_package_data)
+ .to eq([
+ [{ raw: "main", type: :relates, with: "main" }]
+ ])
+ end
+
+ it "can store multiple relations" do
+ work_package_data = parsed_data(<<~TABLE).pluck(:relations)
+ | related to |
+ | wp1, wp2, wp3 |
+ TABLE
+ expect(work_package_data)
+ .to eq([
+ [
+ { raw: "wp1", type: :relates, with: "wp1" },
+ { raw: "wp2", type: :relates, with: "wp2" },
+ { raw: "wp3", type: :relates, with: "wp3" }
+ ]
+ ])
+ end
+ end
+ 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 680153fc6fd..581437a8f68 100644
--- a/spec/support_spec/table_helpers/table_data_spec.rb
+++ b/spec/support_spec/table_helpers/table_data_spec.rb
@@ -159,7 +159,7 @@ module TableHelpers
)
end
- it "creates relations between work packages out of the table data" do
+ it "creates 'follows' relations between work packages out of the table data" do
table_representation = <<~TABLE
subject | predecessors
main |
@@ -176,6 +176,23 @@ module TableHelpers
expect(follower.follows_relations.first.lag).to eq(2)
end
+ it "creates 'relates' relations between work packages out of the table data" do
+ table_representation = <<~TABLE
+ subject | related to
+ main |
+ other | main
+ 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)
+ other = table.work_package(:other)
+ expect(other.relations.relates.count).to eq(1)
+ expect(other.relations.relates.first.to).to eq(main)
+ expect(other.relations.relates.first.lag).to be_nil
+ end
+
it "raises an error if a given status name does not exist" do
table_representation = <<~TABLE
subject | status |