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

fix relatable scope

ulferts4a37880a72ae
diff --git a/app/models/work_packages/scopes/relatable.rb b/app/models/work_packages/scopes/relatable.rb
index 366447e474f..bf74904940d 100644
--- a/app/models/work_packages/scopes/relatable.rb
+++ b/app/models/work_packages/scopes/relatable.rb
@@ -29,9 +29,9 @@ module WorkPackages::Scopes
extend ActiveSupport::Concern
class_methods do
- # Returns all work packages that are relatable to the provided work package with a relation of the provided type.
+ # Returns all work packages that are relatable to the provided work package via new a relation of the provided type.
#
- # For most relation types, e.g. includes the three following rules must be satisfied:
+ # For most relation types, e.g. 'includes', the four following rules must be satisfied:
# * Non circular: relations cannot form a circle, e.g. a -> b -> c -> a.
# * Single relation: only one relation can be created between any two work packages. E.g. it is not possible to create
# two relations like this:
@@ -40,33 +40,114 @@ module WorkPackages::Scopes
# * Ancestor/descendant: relations cannot be drawn between ancestor and descendants. It is important to note though,
# that relations can be created between any two work packages within the same tree as long as they are not in a direct
# or transitive ancestor/descendant relationship. So a relation between siblings is just as possible as one between
- # aunt and nephew.
+ # aunt and nephew. Additionally, a transitive relationship which leaves the tree for any one hop, is exempt from that rule
+ # so it is possible to have child --- blocks ---> other --- blocks ---> parent.
# * No circles between trees: The ancestor/descendant chain is considered bidirectional when calculating relatability.
# This means that starting from the work package both the descendants as well as the ancestors are considered. That
# way, relations like
- # Child 1 --- parent ---> Parent 1 ---> follows ---> Parent 2 ---> child ---> Child 2 --- follows ---> Child 1
+ # Child tree 1
+ # --- parent ---> Parent tree 1
+ # --- follows ---> Parent tree 2
+ # --- child ---> Child tree 2
+ # --- follows ---> Child tree 1
# are prevented.
#
+ # For some of the relations, this has actual relevance (FOLLOWS, PRECEDES, PARENT because of scheduling) while for the
+ # most, it is simply a question of semantics which is not manifested in code (all the other).
+ #
# For the sake of this scope, the parent relation (Relation::TYPE_PARENT) is also included in the list of relation_types
# even though it is not stored in the same data structure. All Relation::TYPE_* values can be provided even those that
# are not canonical, e.g. Relation::TYPE_PRECEDES. The calculation for those relation types are then inverted and the
# canonical type is used, e.g. Relation::TYPE_FOLLOWS.
#
# There are a couple of exceptions and additions to the limitations outlined above for the following types:
- # * Relation::TYPE_RELATES: Since this is essentially unrelated and does not carry a lot of semantic, the work packages
+ # * Relation::TYPE_RELATES: Since this is essentially undirected and does not carry a lot of semantic, the work packages
# are simply somehow related, such relations do not follow the "non circular" nor the "ancestor/descendant" rule.
# * Relation::TYPE_PARENT: Since creating a new relationship will remove the old parent relationship, current ancestors
# (except the direct parent) are relatable to. Descendants however are not since that would create a circle.
+ # In addition to the existing hierarchy, the FOLLOWS relationships are taken into account. Predecessors and successors
+ # (FOLLOWS relationship) may also not be related to via a PARENT relation. However, parents and children of those
+ # predecessors/successors can be related to. But they still need to be considered in the code since they might be
+ # part of an almost completed circle which would be closed if a work package is added as a parent. E.g. in
+ # WP1 --- follows ---> WP2 --- parent ---> WP3 --- follows ---> WP4
+ # WP4 is not a valid parent candidate whereas WP3 is.
+ # Work packages related via follows/precedes to any descendant of a work package are exempt from being relatable right
+ # away as it would create a circle.
+ #
+ # The implementation focuses on excluding candidates. It does so in two parts:
+ # * Excluding all work packages with which a direct relation already exist.
+ # * Excluding work packages that are related transitively.
+ #
+ # The first is straightforward. The second is more complicated and also depends on the type of relation that is
+ # queried for. It uses a CTE to recursively find all work packages with which a transitive relationship of interest based
+ # on the rules outlined above exist.
+ #
+ # For most of the relation types, this includes work packages in the direction towards or away from the
+ # work package to be related to the hierarchy (ancestors and descendants - not siblings and aunts).
+ # Note that only one of the directions is of interest, which is the one opposite to the one that is queried for via the
+ # type. This is to prevent a circle of relationships. For the 'related' type, only the ancestors and descendants are of
+ # interest.
#
- # The implementation right away excludes all work packages with which a direct relation already exists
- # and uses a CTE to find all the work packages with which a transitive relationship based on the rules outlined above
- # exist.
+ # For FOLLOWS/PRECEDES relationships both directions need to be taken into account, and again, this includes those of the
+ # hierarchy. This prevents creating relations in a structure like this:
+ # queried WP
+ # --- follows ---> Predecessor WP
+ # --- parent ---> Predecessor's parent WP
+ # <-- follows --- Predecessor's parent's successor WP
+ # Where neither of the work packages is allowed to become a successor. However, taking both directions is not
+ # necessary from the tree of the queried for work package itself. Just inversing the example above (not the parent child):
+ # queried WP
+ # <-- follows --- Successor WP
+ # --- parent ---> Successor's parent WP
+ # --- follows --> Successor's parent's predecessor WP
+ # all work package are potential successors.
+ #
+ # For PARENT relationships, both directions actually need to be followed even from the queried for work package and
+ # its descendants. However, if the direction of the path is inverted all the work packages on the path
+ # afterwards are valid targets and need not be followed up on. In the example above for FOLLOWS/PRECEDES, both
+ # "Predecessor's parent's successor WP" as well as "Successor's parent's predecessor WP" are valid.
+ # Just for completeness of the example, both parent work packages are valid targets as well. But a predecessor of
+ # the predecessor's target would not be valid.
+ # It is also important to note, that existing ancestors are of no importance since this part of the structure will change.
+ # Creating a parent relationship is destructive since there can only ever be one.
+ #
+ # The result is a blocklist which will include work packages that can not be related to. The list is not complete
+ # as it will not include the work packages related by different relation types so those are added additionally. For the
+ # PARENT relationship, work packages directly related to any of the descendants are added as well. Since work packages
+ # related to the queried work packages only by a path containing both FOLLOWS and PRECEDES relationships are valid targets,
+ # these are filtered out as well. It would be possible to optimize the CTE here to not have them included
+ # in the first place.
+ #
+ # The CTE has the following columns:
+ # * id - the id of the work packages currently related. This is the result of the CTE.
+ # * from_(hierarchy/from_id/to_id) - booleans to prevent that the CTE returns back the path calculated in the previous
+ # iteration.
+ # * origin - boolean to indicate whether the work package is the queried for work package or its ancestor/descendant
+ # (only descendant for PARENT). Such a work package is never a valid target.
+ # Additionally, it is used to limit proceeding in both directions for FOLLOWS/PRECEDES.
+ # * includes_(from_relation/to_relation) - booleans about the direction (from_id -> to_id or to_id -> from_id) of the path
+ # (the relations followed).
+ # This is relevant for a queried for PARENT relation. In that case, relations need
+ # to be followed from the queried for work package (and its descendants) in both
+ # directions. But only the direction taken from that origin needs to be followed
+ # henceforth.
+ # * includes_hierarchy - boolean indicating that the last relation taken was a hierarchy relation. For a queried for
+ # PARENT relation, whenever that is the case, the work package is a valid relation target although
+ # it appears in the CTE.
def relatable(work_package, relation_type)
return all if work_package.new_record?
scope = not_having_directed_relation(work_package, relation_type)
- .not_having_direct_relation(work_package)
- .where.not(id: work_package)
+ .not_having_direct_relation(work_package, relation_type)
+ .where.not(id: work_package.id)
+
+ # On a parent relationship, explicitly remove the former parent (which might be the current one as well)
+ # from the list of work packages one can relate to. This is not strictly necessary since it would not
+ # cause faulty relationships but doing it removes the parent from places where it should not show up,
+ # e.g. in an auto completer.
+ if relation_type == Relation::TYPE_PARENT && work_package.parent_id_was
+ scope = scope.where.not(id: work_package.parent_id_was)
+ end
if Setting.cross_project_work_package_relations
scope
@@ -75,9 +156,17 @@ module WorkPackages::Scopes
end
end
- def not_having_direct_relation(work_package)
- where.not(id: Relation.where(from: work_package).select(:to_id))
- .where.not(id: Relation.where(to: work_package).select(:from_id))
+ def not_having_direct_relation(work_package, relation_type)
+ origin = if relation_type == Relation::TYPE_PARENT
+ WorkPackageHierarchy
+ .where(ancestor_id: work_package.id)
+ .select(:descendant_id)
+ else
+ work_package.id
+ end
+
+ where.not(id: Relation.where(from_id: origin).select(:to_id))
+ .where.not(id: Relation.where(to_id: origin).select(:from_id))
end
def not_having_directed_relation(work_package, relation_type)
@@ -87,42 +176,41 @@ module WorkPackages::Scopes
#{non_relatable_paths_sql(work_package, relation_type)}
SELECT id
- FROM #{related_cte_name}
+ FROM related
+ WHERE #{blocklist_condition(relation_type)}
SQL
- scope = where("work_packages.id NOT IN (#{Arel.sql(sql)})")
-
- if relation_type == Relation::TYPE_PARENT
- # Explicitly allow ancestors except the parent.
- # This only works because an ancestor cannot be already linked to its work package.
- # The #parent_id field of the work package cannot be trusted at this point since it might have
- # an unpersisted change.
- ancestors_without_parent = WorkPackageHierarchy
- .where(descendant_id: work_package.id)
- .where('generations > 1')
-
- scope
- .or(where(id: ancestors_without_parent.select(:ancestor_id)))
- else
- scope
- end
+ where("work_packages.id NOT IN (#{Arel.sql(sql)})")
end
private
def non_relatable_paths_sql(work_package, relation_type)
<<~SQL.squish
- #{related_cte_name} (id, from_hierarchy) AS (
+ related (id,
+ from_hierarchy,
+ from_from_id,
+ from_to_id,
+ includes_from_relation,
+ includes_to_relation,
+ includes_hierarchy,
+ origin) AS (
- #{non_recursive_relatable_values(work_package)}
+ #{non_recursive_relatable_values(work_package, relation_type)}
UNION
SELECT
relations.id,
- relations.from_hierarchy
+ relations.from_hierarchy,
+ relations.from_from_id,
+ relations.from_to_id,
+ relations.includes_from_relation,
+ relations.includes_to_relation,
+ relations.includes_hierarchy,
+ relations.origin
FROM
- #{related_cte_name}
+ related
JOIN LATERAL (
#{joined_existing_connections(relation_type)}
) relations ON 1 = 1
@@ -130,9 +218,31 @@ module WorkPackages::Scopes
SQL
end
- def non_recursive_relatable_values(work_package)
+ def non_recursive_relatable_values(work_package, relation_type)
+ hierarchy_condition = if relation_type == Relation::TYPE_PARENT
+ "work_package_hierarchies.ancestor_id = :id"
+ else
+ "(work_package_hierarchies.ancestor_id = :id OR work_package_hierarchies.descendant_id = :id)"
+ end
+
sql = <<~SQL.squish
- SELECT * FROM (VALUES(:id, false)) AS t(id, from_hierarchy)
+ SELECT
+ CASE
+ WHEN work_package_hierarchies.ancestor_id = :id
+ THEN work_package_hierarchies.descendant_id
+ ELSE work_package_hierarchies.ancestor_id
+ END id,
+ true from_hierarchy,
+ false from_from_id,
+ false from_to_id,
+ false includes_from_relation,
+ false includes_to_relation,
+ work_package_hierarchies.descendant_id = :id includes_hierarchy,
+ true origin
+ FROM
+ work_package_hierarchies
+ WHERE
+ #{hierarchy_condition}
SQL
::OpenProject::SqlSanitization
@@ -143,44 +253,68 @@ module WorkPackages::Scopes
def joined_existing_connections(relation_type)
unions = [existing_hierarchy_lateral]
- if relation_type != Relation::TYPE_RELATES
+ case relation_type
+ when Relation::TYPE_FOLLOWS
+ unions << existing_relation_of_type_lateral(Relation::TYPE_FOLLOWS)
+ unions << existing_relation_of_type_lateral(Relation::TYPE_PRECEDES, limit_origin: true)
+ when Relation::TYPE_PRECEDES
+ unions << existing_relation_of_type_lateral(Relation::TYPE_FOLLOWS, limit_origin: true)
+ unions << existing_relation_of_type_lateral(Relation::TYPE_PRECEDES)
+ when Relation::TYPE_PARENT
+ unions << existing_relation_of_type_lateral(Relation::TYPE_FOLLOWS, limit_direction: true)
+ unions << existing_relation_of_type_lateral(Relation::TYPE_PRECEDES, limit_direction: true)
+ when Relation::TYPE_RELATES
+ # Nothing
+ else
unions << existing_relation_of_type_lateral(relation_type)
end
unions.join(' UNION ')
end
- def existing_relation_of_type_lateral(relation_type)
- # In case a 'parent' is queried for, when it comes to relations,
- # it is in fact relations of type 'follows' that are of interest
- # which is why they are switched for here.
- # Otherwise, the canonical type has to be used as that is the only one
- # that is stored in the database.
- canonical_type = if relation_type == Relation::TYPE_PARENT
- Relation::TYPE_FOLLOWS
- else
- Relation.canonical_type(relation_type)
- end
-
- direction1, direction2 = if canonical_type == relation_type
+ # rubocop:disable Metrics/PerceivedComplexity
+ def existing_relation_of_type_lateral(relation_type, limit_origin: false, limit_direction: false)
+ canonical_type = Relation.canonical_type(relation_type)
+
+ is_canonical = canonical_type == relation_type
+ true_on_canonical = is_canonical ? 'TRUE' : 'FALSE'
+ false_on_canonical = is_canonical ? 'FALSE' : 'TRUE'
+
+ direction1, direction2 = if is_canonical
%w[from_id to_id]
else
%w[to_id from_id]
end
+ direction_limit = if limit_direction && is_canonical
+ 'related.includes_to_relation'
+ elsif limit_direction
+ 'related.includes_from_relation'
+ end
+
sql = <<~SQL.squish
SELECT
#{direction1} id,
- false from_hierarchy
+ false from_hierarchy,
+ #{true_on_canonical} from_from_id,
+ #{false_on_canonical} from_to_id,
+ related.includes_from_relation OR #{true_on_canonical} includes_from_relation,
+ related.includes_to_relation OR #{false_on_canonical} includes_to_relation,
+ false includes_hierarchy,
+ false origin
FROM
relations
- WHERE (relations.#{direction2} = #{related_cte_name}.id AND relations.relation_type = :relation_type)
+ WHERE (relations.#{direction2} = related.id AND relations.relation_type = :relation_type)
+ AND NOT related.from_#{direction2}
+ #{limit_origin ? 'AND NOT related.origin' : ''}
+ #{direction_limit ? "AND NOT #{direction_limit}" : ''}
SQL
::OpenProject::SqlSanitization
.sanitize sql,
relation_type: canonical_type
end
+ # rubocop:enable Metrics/PerceivedComplexity
def existing_hierarchy_lateral
<<~SQL.squish
@@ -190,17 +324,29 @@ module WorkPackages::Scopes
THEN work_package_hierarchies.descendant_id
ELSE work_package_hierarchies.ancestor_id
END id,
- true from_hierarchy
+ true from_hierarchy,
+ false from_from_id,
+ false from_to_id,
+ related.includes_from_relation,
+ related.includes_to_relation,
+ work_package_hierarchies.descendant_id = related.id includes_hierarchy,
+ false origin
FROM
work_package_hierarchies
WHERE
- #{related_cte_name}.from_hierarchy = false AND
- (work_package_hierarchies.ancestor_id = #{related_cte_name}.id OR work_package_hierarchies.descendant_id = #{related_cte_name}.id)
+ related.from_hierarchy = false AND
+ (work_package_hierarchies.ancestor_id = related.id OR work_package_hierarchies.descendant_id = related.id)
+ AND (work_package_hierarchies.generations != 0)
SQL
end
- def related_cte_name
- 'related'
+ def blocklist_condition(relation_type)
+ case relation_type
+ when Relation::TYPE_PARENT
+ "NOT includes_hierarchy"
+ else
+ '1 = 1'
+ end
end
end
end
diff --git a/spec/models/work_packages/scopes/relatable_spec.rb b/spec/models/work_packages/scopes/relatable_spec.rb
index afbba556ab4..a2c3bff51f7 100644
--- a/spec/models/work_packages/scopes/relatable_spec.rb
+++ b/spec/models/work_packages/scopes/relatable_spec.rb
@@ -27,6 +27,8 @@
require 'spec_helper'
describe WorkPackages::Scopes::Relatable, '.relatable scope' do
+ create_shared_association_defaults_for_work_package_factory
+
let(:project) { create(:project) }
let(:origin) { create(:work_package, project:) }
let(:unrelated_work_package) { create(:work_package, project:) }
@@ -148,6 +150,16 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to be_empty
end
end
+
+ context "with the queried for relation being '#{current_type}' and the existing one something different" do
+ let(:relation_type) { current_type }
+ let(:directly_related_work_package_type) { Relation::TYPES.keys[(Relation::TYPES.keys.find_index(current_type) + 1)] }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
end
end
@@ -206,11 +218,58 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
# complicated to switch around the relations
subject(:relatable) { WorkPackage.relatable(transitively_related_work_package, relation_type) }
- it 'is empty' do
+ it 'includes the not directly related work package' do
expect(relatable)
.to match_array [origin]
end
end
+
+ context "for a 'parent' relation and the existing relations being 'follows'" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+ let(:directly_related_work_package_type) { Relation::TYPE_FOLLOWS }
+ let(:transitively_related_work_package_type) { Relation::TYPE_FOLLOWS }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'parent' relation and the existing relations being 'precedes'" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+ let(:directly_related_work_package_type) { Relation::TYPE_PRECEDES }
+ let(:transitively_related_work_package_type) { Relation::TYPE_PRECEDES }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'parent' relation and the existing relations being 'blocks'" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+ let(:directly_related_work_package_type) { Relation::TYPE_BLOCKS }
+ let(:transitively_related_work_package_type) { Relation::TYPE_BLOCKS }
+
+ # This leads to a relationship that, on the domain level does not really make sense where at least
+ # transitively, the child blocks the parent. But since such a relation does not strictly carry that
+ # semantic in the system, the relationship is not prohibited.
+ it 'contains the transitively related work package' do
+ expect(relatable)
+ .to match_array([transitively_related_work_package])
+ end
+ end
+
+ context "for a 'blocks' relation and the existing relations being 'blocks'" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+ let(:directly_related_work_package_type) { Relation::TYPE_BLOCKS }
+ let(:transitively_related_work_package_type) { Relation::TYPE_BLOCKS }
+
+ it 'contains the transitively related work package' do
+ expect(relatable)
+ .to match_array([transitively_related_work_package])
+ end
+ end
end
context "with a child" do
@@ -234,13 +293,13 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
let(:other_child) do
create(:work_package, project:, parent: other_parent).tap do |wp|
- create(:relation, from: wp, to: origin_child, relation_type: other_relation_type)
+ create(:relation, from: wp, to: origin_child, relation_type: existing_relation_type)
end
end
let!(:existing_work_packages) { [origin_child, other_parent, other_child] }
context "for a 'follows' and the existing relation being a follows in the opposite direction" do
- let(:other_relation_type) { Relation::TYPE_FOLLOWS }
+ let(:existing_relation_type) { Relation::TYPE_FOLLOWS }
let(:relation_type) { Relation::TYPE_FOLLOWS }
it 'is empty' do
@@ -251,7 +310,7 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
context "for a 'follows' and the existing relation being a follows in the same direction" do
# Using precedes will lead to the relation being reversed
- let(:other_relation_type) { Relation::TYPE_PRECEDES }
+ let(:existing_relation_type) { Relation::TYPE_PRECEDES }
let(:relation_type) { Relation::TYPE_FOLLOWS }
it 'contains the work packages in the other hierarchy' do
@@ -259,6 +318,27 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array [other_parent, other_child]
end
end
+
+ context "for a 'blocks' and the existing relation being a blocks in the same direction" do
+ # Using blocked will lead to the relation being reversed
+ let(:existing_relation_type) { Relation::TYPE_BLOCKED }
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it 'contains the work packages in the other hierarchy' do
+ expect(relatable)
+ .to match_array [other_parent, other_child]
+ end
+ end
+
+ context "for a 'blocks' and the existing relation being a blocks in the opposite direction" do
+ let(:existing_relation_type) { Relation::TYPE_BLOCKS }
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
end
context 'with a child, parent, grandparent and aunt' do
@@ -273,6 +353,24 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it 'contains aunt' do
+ expect(relatable)
+ .to match_array [aunt]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it 'contains aunt' do
+ expect(relatable)
+ .to match_array [aunt]
+ end
+ end
+
context "for a 'parent' relation with a follows relation between child and aunt" do
let(:relation_type) { Relation::TYPE_PARENT }
@@ -298,6 +396,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array [aunt]
end
end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it 'contains aunt' do
+ expect(relatable)
+ .to match_array [aunt]
+ end
+ end
end
context 'with an ancestor chain of 3 work packages' do
@@ -317,5 +424,645 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array [grandparent, grand_grandparent]
end
end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+ end
+
+ context 'with a predecessor having a parent' do
+ let(:predecessor_parent) do
+ create(:work_package, project:)
+ end
+ let(:predecessor) do
+ create(:work_package, project:, parent: predecessor_parent).tap do |pre|
+ create(:relation, from: origin, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [predecessor_parent, predecessor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the predecessor's parent" do
+ expect(relatable)
+ .to match_array [predecessor_parent]
+ end
+ end
+ end
+
+ context 'with two predecessors being in a hierarchy' do
+ let(:predecessor_parent) do
+ create(:work_package, project:).tap do |pre|
+ create(:relation, from: origin, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let(:predecessor) do
+ create(:work_package, project:, parent: predecessor_parent).tap do |pre|
+ create(:relation, from: origin, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [predecessor_parent, predecessor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+ end
+
+ context 'with a predecessor having a parent that has a predecessor' do
+ let(:predecessor_parent_predecessor) do
+ create(:work_package, project:).tap do |pre|
+ create(:relation, from: predecessor_parent, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let(:predecessor_parent) do
+ create(:work_package, project:)
+ end
+ let(:predecessor) do
+ create(:work_package, project:, parent: predecessor_parent).tap do |pre|
+ create(:relation, from: origin, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [predecessor_parent_predecessor, predecessor_parent, predecessor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the predecessor's parent" do
+ expect(relatable)
+ .to match_array [predecessor_parent]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_predecessor]
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_predecessor]
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_predecessor]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_predecessor]
+ end
+ end
+ end
+
+ context 'with a predecessor having a parent that has a successor' do
+ let(:predecessor_parent_successor) do
+ create(:work_package, project:).tap do |suc|
+ create(:relation, to: predecessor_parent, from: suc, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let(:predecessor_parent) do
+ create(:work_package, project:)
+ end
+ let(:predecessor) do
+ create(:work_package, project:, parent: predecessor_parent).tap do |pre|
+ create(:relation, from: origin, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [predecessor_parent_successor, predecessor_parent, predecessor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the predecessor's parent and its successor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_successor]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_successor]
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_successor]
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_successor]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the predecessor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [predecessor_parent, predecessor_parent_successor]
+ end
+ end
+ end
+
+ context 'with a successor having a parent that has a successor' do
+ let(:successor_parent_successor) do
+ create(:work_package, project:).tap do |suc|
+ create(:relation, to: successor_parent, from: suc, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let(:successor_parent) do
+ create(:work_package, project:)
+ end
+ let(:successor) do
+ create(:work_package, project:, parent: successor_parent).tap do |suc|
+ create(:relation, to: origin, from: suc, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [successor_parent_successor, successor_parent, successor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the successor's parent" do
+ expect(relatable)
+ .to match_array [successor_parent]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "is contains the successor's parent and that parent's successor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_successor]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the successor's parent and that parent's successor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_successor]
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "contains the successor's parent and that parent's successor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_successor]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the successor's parent and that parent's successor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_successor]
+ end
+ end
+ end
+
+ context 'with a successor having a parent that has a predecessor' do
+ let(:successor_parent_predecessor) do
+ create(:work_package, project:).tap do |pre|
+ create(:relation, from: successor_parent, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let(:successor_parent) do
+ create(:work_package, project:)
+ end
+ let(:successor) do
+ create(:work_package, project:, parent: successor_parent).tap do |suc|
+ create(:relation, to: origin, from: suc, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [successor_parent_predecessor, successor_parent, successor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the successor's parent and its predecessor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_predecessor]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "is contains the successor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_predecessor]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the successor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_predecessor]
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "contains the successor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_predecessor]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the successor's parent and that parent's predecessor" do
+ expect(relatable)
+ .to match_array [successor_parent, successor_parent_predecessor]
+ end
+ end
+ end
+
+ context 'with a parent that has a predecessor' do
+ let(:parent_predecessor) do
+ create(:work_package, project:).tap do |pre|
+ create(:follows_relation, from: parent, to: pre)
+ end
+ end
+ let!(:existing_work_packages) { [parent, parent_predecessor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the parent's predecessor" do
+ expect(relatable)
+ .to match_array [parent_predecessor]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "contains the parent's predecessor" do
+ expect(relatable)
+ .to match_array [parent_predecessor]
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the parent's predecessor" do
+ expect(relatable)
+ .to match_array [parent_predecessor]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the parent's predecessor" do
+ expect(relatable)
+ .to match_array [parent_predecessor]
+ end
+ end
+ end
+
+ context 'with a parent that has a successor' do
+ let(:parent_successor) do
+ create(:work_package, project:).tap do |suc|
+ create(:follows_relation, to: parent, from: suc)
+ end
+ end
+ let!(:existing_work_packages) { [parent, parent_successor] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the parent's successor" do
+ expect(relatable)
+ .to match_array [parent_successor]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "contains the parent's successor" do
+ expect(relatable)
+ .to match_array [parent_successor]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the parent's successor" do
+ expect(relatable)
+ .to match_array [parent_successor]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the parent's successor" do
+ expect(relatable)
+ .to match_array [parent_successor]
+ end
+ end
+ end
+
+ context 'with a child that has a successor that has a parent and a grandparent' do
+ let(:child_successor) do
+ create(:work_package, project:, parent: child_successor_parent).tap do |suc|
+ create(:follows_relation, from: suc, to: origin_child)
+ end
+ end
+ let(:child_successor_parent) do
+ create(:work_package, project:, parent: child_successor_grandparent)
+ end
+ let(:child_successor_grandparent) do
+ create(:work_package, project:)
+ end
+ let!(:existing_work_packages) { [origin_child, child_successor, child_successor_parent, child_successor_grandparent] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the parent of the child's successor" do
+ expect(relatable)
+ .to match_array [child_successor_parent, child_successor_grandparent]
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "contains the child's successor and the parent of that" do
+ expect(relatable)
+ .to match_array [child_successor, child_successor_parent, child_successor_grandparent]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the child's successor and the parent of that" do
+ expect(relatable)
+ .to match_array [child_successor, child_successor_parent, child_successor_grandparent]
+ end
+ end
+ end
+
+ context 'with a child that blocks a work package that has a parent and a grandparent' do
+ let(:child_blocked) do
+ create(:work_package, project:, parent: child_blocked_parent).tap do |wp|
+ create(:relation, relation_type: Relation::TYPE_BLOCKS, from: origin_child, to: wp)
+ end
+ end
+ let(:child_blocked_parent) do
+ create(:work_package, project:, parent: child_blocked_grandparent)
+ end
+ let(:child_blocked_grandparent) do
+ create(:work_package, project:)
+ end
+ let!(:existing_work_packages) { [origin_child, child_blocked, child_blocked_parent, child_blocked_grandparent] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the parent of the child's blocked work package" do
+ expect(relatable)
+ .to match_array [child_blocked_parent, child_blocked_grandparent]
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "contains the child's blocked work package and its ancestors" do
+ expect(relatable)
+ .to match_array [child_blocked, child_blocked_parent, child_blocked_grandparent]
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "contains the child's blocked work package and its ancestors" do
+ expect(relatable)
+ .to match_array [child_blocked, child_blocked_parent, child_blocked_grandparent]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the child's blocked work package and its ancestors" do
+ expect(relatable)
+ .to match_array [child_blocked, child_blocked_parent, child_blocked_grandparent]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the child's blocked work package and its ancestors" do
+ expect(relatable)
+ .to match_array [child_blocked, child_blocked_parent, child_blocked_grandparent]
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+ end
+
+ context 'with a predecessor that has a child' do
+ let(:predecessor_child) do
+ create(:work_package, project:, parent: predecessor)
+ end
+ let(:predecessor) do
+ create(:work_package, project:).tap do |pre|
+ create(:relation, from: origin, to: pre, relation_type: Relation::TYPE_FOLLOWS)
+ end
+ end
+ let!(:existing_work_packages) { [origin, predecessor, predecessor_child] }
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'precedes' relation" do
+ let(:relation_type) { Relation::TYPE_PRECEDES }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'follows' relation" do
+ let(:relation_type) { Relation::TYPE_FOLLOWS }
+
+ it "contains the predecessor's child" do
+ expect(relatable)
+ .to match_array [predecessor_child]
+ end
+ end
+
+ context "for a 'relates' relation" do
+ let(:relation_type) { Relation::TYPE_RELATES }
+
+ it "contains the predecessor's child" do
+ expect(relatable)
+ .to match_array [predecessor_child]
+ end
+ end
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "contains the predecessor's child" do
+ expect(relatable)
+ .to match_array [predecessor_child]
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "contains the predecessor's child" do
+ expect(relatable)
+ .to match_array [predecessor_child]
+ end
+ end
end
end