2025-02-12 09:19:02 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class Relation < ApplicationRecord
|
2022-03-19 23:26:32 +01:00
|
|
|
belongs_to :from, class_name: "WorkPackage"
|
|
|
|
|
belongs_to :to, class_name: "WorkPackage"
|
2018-02-09 09:58:19 +01:00
|
|
|
|
2025-02-12 09:19:02 +01:00
|
|
|
TYPE_RELATES = "relates"
|
|
|
|
|
TYPE_PRECEDES = "precedes"
|
|
|
|
|
TYPE_FOLLOWS = "follows"
|
|
|
|
|
TYPE_BLOCKS = "blocks"
|
|
|
|
|
TYPE_BLOCKED = "blocked"
|
|
|
|
|
TYPE_DUPLICATES = "duplicates"
|
|
|
|
|
TYPE_DUPLICATED = "duplicated"
|
|
|
|
|
TYPE_INCLUDES = "includes"
|
|
|
|
|
TYPE_PARTOF = "partof"
|
|
|
|
|
TYPE_REQUIRES = "requires"
|
|
|
|
|
TYPE_REQUIRED = "required"
|
2022-11-29 21:53:18 +01:00
|
|
|
# The parent/child relation is maintained separately
|
2022-03-19 23:26:32 +01:00
|
|
|
# (in WorkPackage and WorkPackageHierarchy) and a relation cannot
|
|
|
|
|
# have the type 'parent' but this is abstracted to simplify the code.
|
2025-02-12 09:19:02 +01:00
|
|
|
TYPE_PARENT = "parent"
|
|
|
|
|
TYPE_CHILD = "child"
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2025-04-01 12:45:53 +02:00
|
|
|
# The order of the types is important. It's used to build up `ORDERED_TYPES`
|
|
|
|
|
# which is used to order relations of different kind like in the "Add relation"
|
|
|
|
|
# menu or the relations tab.
|
2016-09-09 15:25:29 +02:00
|
|
|
TYPES = {
|
|
|
|
|
TYPE_RELATES => {
|
2025-04-02 09:46:52 +02:00
|
|
|
name: :label_relates_to, sym_name: :label_relates_to, order: 1,
|
|
|
|
|
sym: TYPE_RELATES
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
2024-10-24 13:45:25 -05:00
|
|
|
TYPE_FOLLOWS => {
|
|
|
|
|
name: :label_follows, sym_name: :label_precedes, order: 7,
|
|
|
|
|
sym: TYPE_PRECEDES
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
2025-01-31 10:57:55 +01:00
|
|
|
TYPE_PRECEDES => {
|
|
|
|
|
name: :label_precedes, sym_name: :label_follows, order: 6,
|
|
|
|
|
sym: TYPE_FOLLOWS, reverse: TYPE_FOLLOWS
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
2024-10-24 13:45:25 -05:00
|
|
|
TYPE_DUPLICATES => {
|
2025-04-02 09:46:52 +02:00
|
|
|
name: :label_duplicates, sym_name: :label_duplicated_by, order: 6,
|
|
|
|
|
sym: TYPE_DUPLICATED
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
2024-10-24 13:45:25 -05:00
|
|
|
TYPE_DUPLICATED => {
|
|
|
|
|
name: :label_duplicated_by, sym_name: :label_duplicates, order: 7,
|
|
|
|
|
sym: TYPE_DUPLICATES, reverse: TYPE_DUPLICATES
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
2025-01-31 10:57:55 +01:00
|
|
|
TYPE_BLOCKS => {
|
2025-04-02 09:46:52 +02:00
|
|
|
name: :label_blocks, sym_name: :label_blocked_by, order: 4,
|
|
|
|
|
sym: TYPE_BLOCKED
|
2025-01-31 10:57:55 +01:00
|
|
|
},
|
|
|
|
|
TYPE_BLOCKED => {
|
|
|
|
|
name: :label_blocked_by, sym_name: :label_blocks, order: 5,
|
|
|
|
|
sym: TYPE_BLOCKS, reverse: TYPE_BLOCKS
|
|
|
|
|
},
|
2016-09-09 15:25:29 +02:00
|
|
|
TYPE_INCLUDES => {
|
2017-07-21 16:57:45 +02:00
|
|
|
name: :label_includes, sym_name: :label_part_of, order: 8,
|
2016-10-11 16:20:19 +02:00
|
|
|
sym: TYPE_PARTOF
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
|
|
|
|
TYPE_PARTOF => {
|
2017-07-21 16:57:45 +02:00
|
|
|
name: :label_part_of, sym_name: :label_includes, order: 9,
|
2016-10-11 16:20:19 +02:00
|
|
|
sym: TYPE_INCLUDES, reverse: TYPE_INCLUDES
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
|
|
|
|
TYPE_REQUIRES => {
|
2017-07-21 16:57:45 +02:00
|
|
|
name: :label_requires, sym_name: :label_required, order: 10,
|
2016-10-11 16:20:19 +02:00
|
|
|
sym: TYPE_REQUIRED
|
2016-09-09 15:25:29 +02:00
|
|
|
},
|
|
|
|
|
TYPE_REQUIRED => {
|
2017-07-21 16:57:45 +02:00
|
|
|
name: :label_required, sym_name: :label_requires, order: 11,
|
2016-10-11 16:20:19 +02:00
|
|
|
sym: TYPE_REQUIRES, reverse: TYPE_REQUIRES
|
2016-09-09 15:25:29 +02:00
|
|
|
}
|
|
|
|
|
}.freeze
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2025-04-18 15:56:14 +02:00
|
|
|
ORDERED_TYPES = [*TYPES.keys, TYPE_PARENT, TYPE_CHILD].freeze
|
2025-04-01 12:45:53 +02:00
|
|
|
|
2025-05-27 08:54:23 +02:00
|
|
|
MAX_LAG = 2_000
|
|
|
|
|
MIN_LAG = -MAX_LAG
|
|
|
|
|
|
2022-03-19 23:26:32 +01:00
|
|
|
include ::Scopes::Scoped
|
2017-12-06 07:17:02 +01:00
|
|
|
|
2025-02-27 19:00:15 +01:00
|
|
|
scopes :used_for_scheduling_of,
|
2022-03-19 23:26:32 +01:00
|
|
|
:types,
|
|
|
|
|
:visible
|
2017-10-18 10:39:08 +02:00
|
|
|
|
2022-03-19 23:26:32 +01:00
|
|
|
scope :of_work_package,
|
|
|
|
|
->(work_package) { where(from: work_package).or(where(to: work_package)) }
|
2017-12-13 14:38:35 +01:00
|
|
|
|
2024-04-08 15:42:07 +02:00
|
|
|
scope :follows_with_lag,
|
|
|
|
|
-> { follows.where("lag > 0") }
|
2022-09-20 17:38:53 +02:00
|
|
|
|
2025-02-24 17:46:59 +01:00
|
|
|
scope :of_predecessor,
|
|
|
|
|
->(work_package) { where(to: work_package) }
|
|
|
|
|
|
2024-12-17 16:48:28 +01:00
|
|
|
scope :of_successor,
|
|
|
|
|
->(work_package) { where(from: work_package) }
|
|
|
|
|
|
|
|
|
|
scope :not_of_predecessor,
|
|
|
|
|
->(work_package) { where.not(to: work_package) }
|
|
|
|
|
|
2025-02-24 17:46:59 +01:00
|
|
|
scope :not_of_successor,
|
|
|
|
|
->(work_package) { where.not(from: work_package) }
|
|
|
|
|
|
2025-02-05 14:45:46 +01:00
|
|
|
validates :lag, numericality: {
|
|
|
|
|
allow_nil: true,
|
2025-05-27 08:54:23 +02:00
|
|
|
less_than_or_equal_to: MAX_LAG,
|
|
|
|
|
greater_than_or_equal_to: MIN_LAG
|
2025-02-05 14:45:46 +01:00
|
|
|
}
|
2017-12-19 11:57:13 +01:00
|
|
|
|
2025-02-12 09:19:02 +01:00
|
|
|
validates :to, uniqueness: { scope: :from }
|
2017-12-19 11:57:13 +01:00
|
|
|
|
2022-03-19 23:26:32 +01:00
|
|
|
before_validation :reverse_if_needed
|
2017-12-19 11:57:13 +01:00
|
|
|
|
2013-09-24 13:25:27 +02:00
|
|
|
def other_work_package(work_package)
|
2016-10-11 10:58:53 +02:00
|
|
|
from_id == work_package.id ? to : from
|
2007-05-05 13:22:27 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-09-24 13:25:27 +02:00
|
|
|
# Returns the relation type for +work_package+
|
|
|
|
|
def relation_type_for(work_package)
|
2010-05-01 14:07:36 +00:00
|
|
|
if TYPES[relation_type]
|
2014-11-03 21:35:22 +01:00
|
|
|
if from_id == work_package.id
|
2010-05-01 14:07:36 +00:00
|
|
|
relation_type
|
|
|
|
|
else
|
|
|
|
|
TYPES[relation_type][:sym]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2017-08-29 13:10:01 +02:00
|
|
|
def reverse_type
|
|
|
|
|
Relation::TYPES[relation_type] && Relation::TYPES[relation_type][:sym]
|
|
|
|
|
end
|
|
|
|
|
|
2013-09-24 13:25:27 +02:00
|
|
|
def label_for(work_package)
|
2017-08-29 13:10:01 +02:00
|
|
|
key = from_id == work_package.id ? :name : :sym_name
|
|
|
|
|
|
|
|
|
|
TYPES[relation_type] ? TYPES[relation_type][key] : :unknown
|
2007-05-05 13:22:27 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2024-12-16 11:25:10 +01:00
|
|
|
def predecessor = to
|
|
|
|
|
def predecessor_id = to_id
|
|
|
|
|
def successor = from
|
|
|
|
|
def successor_id = from_id
|
|
|
|
|
|
|
|
|
|
def predecessor_date
|
|
|
|
|
predecessor.due_date || predecessor.start_date
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def successor_date
|
|
|
|
|
successor.start_date || successor.due_date
|
|
|
|
|
end
|
|
|
|
|
|
2025-04-09 11:22:08 -04:00
|
|
|
def successor_soonest_start
|
2024-12-16 11:25:10 +01:00
|
|
|
if follows? && predecessor_date
|
2025-04-09 11:22:08 -04:00
|
|
|
days = WorkPackages::Shared::WorkingDays.new
|
2025-05-27 08:54:23 +02:00
|
|
|
days.with_lag(predecessor_date, lag)
|
2010-12-04 18:10:02 +00:00
|
|
|
end
|
2007-05-05 13:22:27 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2017-08-29 13:10:01 +02:00
|
|
|
def <=>(other)
|
|
|
|
|
TYPES[relation_type][:order] <=> TYPES[other.relation_type][:order]
|
2007-05-05 13:22:27 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2022-03-19 23:26:32 +01:00
|
|
|
TYPES.each_key do |type|
|
2024-01-04 17:01:17 +01:00
|
|
|
define_method :"#{type}?" do
|
2022-03-19 23:26:32 +01:00
|
|
|
canonical_type == self.class.canonical_type(type)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-10-11 10:58:53 +02:00
|
|
|
def canonical_type
|
2019-05-27 09:52:39 +02:00
|
|
|
self.class.canonical_type(relation_type)
|
2018-01-11 11:01:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.canonical_type(relation_type)
|
2016-10-11 10:58:53 +02:00
|
|
|
if TYPES.key?(relation_type) &&
|
|
|
|
|
TYPES[relation_type][:reverse]
|
|
|
|
|
TYPES[relation_type][:reverse]
|
|
|
|
|
else
|
|
|
|
|
relation_type
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-01-09 09:59:39 +01:00
|
|
|
private
|
|
|
|
|
|
2010-02-28 11:28:32 +00:00
|
|
|
# Reverses the relation if needed so that it gets stored in the proper way
|
2009-12-18 20:35:16 +00:00
|
|
|
def reverse_if_needed
|
2016-10-11 10:58:53 +02:00
|
|
|
if TYPES.key?(relation_type) && TYPES[relation_type][:reverse]
|
2013-09-24 13:25:27 +02:00
|
|
|
work_package_tmp = to
|
2013-09-19 16:14:50 +02:00
|
|
|
self.to = from
|
2013-09-24 13:25:27 +02:00
|
|
|
self.from = work_package_tmp
|
2010-02-28 11:28:32 +00:00
|
|
|
self.relation_type = TYPES[relation_type][:reverse]
|
2009-12-18 20:35:16 +00:00
|
|
|
end
|
|
|
|
|
end
|
2007-05-05 13:22:27 +00:00
|
|
|
end
|