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

include child relation type

ulfertsc69f19343264
diff --git a/app/models/queries/filters/strategies/relation.rb b/app/models/queries/filters/strategies/relation.rb
index 5a5c4006e10..a8a2bc1a7e2 100644
--- a/app/models/queries/filters/strategies/relation.rb
+++ b/app/models/queries/filters/strategies/relation.rb
@@ -31,7 +31,8 @@ module Queries::Filters::Strategies
delegate :allowed_values_subset,
to: :filter
- self.supported_operators = ::Relation::TYPES.keys + %w(parent children)
+ # 'children' used to be supported by the API although 'child' would be more fitting.
+ self.supported_operators = ::Relation::TYPES.keys + [::Relation::TYPE_PARENT, ::Relation::TYPE_CHILD, 'children']
self.default_operator = ::Relation::TYPE_RELATES
def validate
diff --git a/app/models/queries/work_packages/filter/relatable_filter.rb b/app/models/queries/work_packages/filter/relatable_filter.rb
index 41920c54b5f..90ed51b4096 100644
--- a/app/models/queries/work_packages/filter/relatable_filter.rb
+++ b/app/models/queries/work_packages/filter/relatable_filter.rb
@@ -47,6 +47,17 @@ class Queries::WorkPackages::Filter::RelatableFilter < Queries::WorkPackages::Fi
end
def scope
- WorkPackage.relatable(WorkPackage.find_by(id: values.first), operator)
+ WorkPackage.relatable(WorkPackage.find_by(id: values.first), scope_operator)
+ end
+
+ private
+
+ # 'children' used to be supported by the API although 'child' would be more fitting.
+ def scope_operator
+ if operator == 'children'
+ Relation::TYPE_CHILD
+ else
+ operator
+ end
end
end
diff --git a/app/models/relation.rb b/app/models/relation.rb
index bc361f5e6cb..0dcd16237ac 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -41,10 +41,11 @@ class Relation < ApplicationRecord
TYPE_PARTOF = 'partof'.freeze
TYPE_REQUIRES = 'requires'.freeze
TYPE_REQUIRED = 'required'.freeze
- # The parent relation is maintained separately
+ # The parent/child relation is maintained separately
# (in WorkPackage and WorkPackageHierarchy) and a relation cannot
# have the type 'parent' but this is abstracted to simplify the code.
TYPE_PARENT = 'parent'.freeze
+ TYPE_CHILD = 'child'.freeze
TYPES = {
TYPE_RELATES => {
diff --git a/app/models/work_packages/scopes/relatable.rb b/app/models/work_packages/scopes/relatable.rb
index f8c648b8815..9c6008e4b7c 100644
--- a/app/models/work_packages/scopes/relatable.rb
+++ b/app/models/work_packages/scopes/relatable.rb
@@ -68,10 +68,10 @@ module WorkPackages::Scopes
# 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.
+ # For the sake of this scope, hierarchy relations (Relation::TYPE_PARENT and Relation::TYPE_CHILD) are also included
+ # in the list of possible relation_types even though they are 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 calculations 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 undirected and does not carry a lot of semantic, the work packages
@@ -94,6 +94,11 @@ module WorkPackages::Scopes
# "No circle for trees". However WP3 would be relatable to.
# 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.
+ # * Relation::TYPE_CHILD: The relation essentially follows the same rules, albeit inverted, as PARENT since it is
+ # the non canonical equivalent of it. Again, the parent relation of current children will be altered upon creating a new
+ # relation. Because of that, all descendants (as opposed to ancestors as is the case for PARENT) can be related to.
+ # The following in depth discussion focuses on the PARENT relation to walk through the special cases of the hierarchical
+ # relation. Whenever PARENT is mentioned, CHILD is also included.
#
# The implementation focuses on excluding candidates. It does so in two parts:
# * Excluding all work packages with which a direct relation already exist (with additions for PARENT relations).
@@ -101,7 +106,7 @@ module WorkPackages::Scopes
#
# The first is straightforward for all relation types except for PARENT relations. For that majority, whenever there is
# a relation of any type except PARENT either to or from the work package queried for, it is excluded. For PARENT relations,
- # both the descendants of the queried for work package as well as the decendants of any directly related work packages are
+ # both the descendants of the queried for work package as well as the descendants of any directly related work packages are
# excluded as well since creating a PARENT relationship to one such work package would result in a relation up or down the
# hierarchy which violates the ancestor/descendant rule.
#
@@ -168,8 +173,11 @@ module WorkPackages::Scopes
def relatable(work_package, relation_type)
return all if work_package.new_record?
- scope = if relation_type == Relation::TYPE_PARENT
- not_having_potential_tree_relation(work_package)
+ scope = case relation_type
+ when Relation::TYPE_PARENT
+ not_having_potential_tree_relation_parent(work_package)
+ when Relation::TYPE_CHILD
+ not_having_potential_tree_relation_child(work_package)
else
where.not(id: directly_related(work_package))
end
@@ -211,7 +219,7 @@ module WorkPackages::Scopes
private
- def not_having_potential_tree_relation(work_package)
+ def not_having_potential_tree_relation_parent(work_package)
# 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,
@@ -227,6 +235,16 @@ module WorkPackages::Scopes
.where.not(id: descendant_or_self_ids_of(directly_related(descendant_or_self_ids_of(work_package))))
end
+ def not_having_potential_tree_relation_child(work_package)
+ # On a child relationship, explicitly remove the current children 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 children from places where it should not show up,
+ # e.g. in an auto completer.
+ where.not(id: directly_related(ancestor_or_self_ids_of(work_package)))
+ .where.not(id: ancestor_or_self_ids_of(directly_related(ancestor_or_self_ids_of(work_package))))
+ .where.not(id: where(parent_id: work_package.id).select(:id))
+ end
+
def non_relatable_paths_sql(work_package, relation_type)
<<~SQL.squish
related (id,
@@ -261,8 +279,11 @@ module WorkPackages::Scopes
end
def non_recursive_relatable_values(work_package, relation_type)
- hierarchy_condition = if relation_type == Relation::TYPE_PARENT
+ hierarchy_condition = case relation_type
+ when Relation::TYPE_PARENT
"work_package_hierarchies.ancestor_id = :id"
+ when Relation::TYPE_CHILD
+ "work_package_hierarchies.descendant_id = :id"
else
"(work_package_hierarchies.ancestor_id = :id OR work_package_hierarchies.descendant_id = :id)"
end
@@ -279,7 +300,7 @@ module WorkPackages::Scopes
false from_to_id,
false includes_from_relation,
false includes_to_relation,
- work_package_hierarchies.descendant_id = :id includes_hierarchy,
+ false includes_hierarchy,
true origin
FROM
work_package_hierarchies
@@ -293,14 +314,13 @@ module WorkPackages::Scopes
end
def joined_existing_connections(relation_type)
- unions = [existing_hierarchy_lateral]
+ unions = [existing_hierarchy_lateral(with_descendants: relation_type != Relation::TYPE_CHILD)]
case relation_type
- when Relation::TYPE_PARENT
+ when Relation::TYPE_PARENT, Relation::TYPE_CHILD
unions << existing_relation_of_type_lateral(Relation::TYPE_FOLLOWS, limit_direction: true)
unions << existing_relation_of_type_lateral(Relation::TYPE_PRECEDES, limit_direction: true)
else
- unions << existing_hierarchy_lateral
unions << existing_relation_of_type_lateral(relation_type)
end
@@ -350,7 +370,10 @@ module WorkPackages::Scopes
end
# rubocop:enable Metrics/PerceivedComplexity
- def existing_hierarchy_lateral
+ def existing_hierarchy_lateral(with_descendants: true)
+ hierarchy_condition = ["work_package_hierarchies.descendant_id = related.id"]
+ hierarchy_condition << "work_package_hierarchies.ancestor_id = related.id" if with_descendants
+
<<~SQL.squish
SELECT
CASE
@@ -363,20 +386,20 @@ module WorkPackages::Scopes
false from_to_id,
related.includes_from_relation,
related.includes_to_relation,
- work_package_hierarchies.descendant_id = related.id includes_hierarchy,
+ true includes_hierarchy,
false origin
FROM
work_package_hierarchies
WHERE
related.from_hierarchy = false AND
- (work_package_hierarchies.ancestor_id = related.id OR work_package_hierarchies.descendant_id = related.id)
+ (#{hierarchy_condition.join(' OR ')})
AND (work_package_hierarchies.generations != 0)
SQL
end
def blocklist_condition(relation_type)
case relation_type
- when Relation::TYPE_PARENT
+ when Relation::TYPE_PARENT, Relation::TYPE_CHILD
"NOT includes_hierarchy"
else
'1 = 1'
@@ -388,6 +411,12 @@ module WorkPackages::Scopes
.where(ancestor_id: work_packages)
.select(:descendant_id)
end
+
+ def ancestor_or_self_ids_of(work_packages)
+ WorkPackageHierarchy
+ .where(descendant_id: work_packages)
+ .select(:ancestor_id)
+ 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 126fe18102a..4f19c40d37d 100644
--- a/spec/models/work_packages/scopes/relatable_spec.rb
+++ b/spec/models/work_packages/scopes/relatable_spec.rb
@@ -126,15 +126,6 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
end
-
- context "for the '#{Relation::TYPE_PARENT}'" do
- let(:relation_type) { Relation::TYPE_PARENT }
-
- it 'contains the unrelated_work_package' do
- expect(relatable)
- .to match_array([unrelated_work_package])
- end
- end
end
context 'with a directly related work package' do
@@ -245,6 +236,28 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation and the existing relations being 'follows'" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+ 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 'child' relation and the existing relations being 'precedes'" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+ 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 }
@@ -269,6 +282,20 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array([transitively_related_work_package])
end
end
+
+ context "for a 'child' relation and the existing relations being 'blocks'" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+ 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 parent blocks the child. 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
end
context "with a child" do
@@ -352,6 +379,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it 'contains aunt' do
+ expect(relatable)
+ .to match_array [aunt]
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -383,6 +419,19 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation with a follows relation between child and aunt" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ before do
+ create(:follows_relation, from: origin_child, to: aunt)
+ end
+
+ it 'contains aunt' do
+ expect(relatable)
+ .to match_array [aunt]
+ end
+ end
+
context "for a 'relates' relation with a follows relation between child and aunt" do
let(:relation_type) { Relation::TYPE_RELATES }
@@ -424,6 +473,62 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ 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 descendant chain of 3 work packages' do
+ let(:grandchild) do
+ create(:work_package, parent: origin_child)
+ end
+ let(:grand_grandchild) do
+ create(:work_package, parent: grandchild)
+ end
+
+ let!(:existing_work_packages) { [origin, origin_child, grandchild, grand_grandchild] }
+
+ 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 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it 'contains grandchild and grand_grandchild' do
+ expect(relatable)
+ .to match_array [grandchild, grand_grandchild]
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -462,6 +567,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array [predecessor_parent]
end
end
+
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
end
context 'with two predecessors being in a hierarchy' do
@@ -485,6 +599,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to be_empty
end
end
+
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
end
context 'with a predecessor having a parent that has a predecessor' do
@@ -512,6 +635,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it 'is empty' do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -583,6 +715,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the successor of the predecessor's parent" do
+ expect(relatable)
+ .to match_array [predecessor_parent_successor]
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -654,6 +795,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -725,6 +875,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the predecessor of the successor's parent" do
+ expect(relatable)
+ .to match_array [successor_parent_predecessor]
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -788,6 +947,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -842,6 +1010,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
context "for a 'follows' relation" do
let(:relation_type) { Relation::TYPE_FOLLOWS }
@@ -896,12 +1073,21 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
context "for a 'parent' relation" do
let(:relation_type) { Relation::TYPE_PARENT }
- it "contains the parent of the child's successor" do
+ it "contains the parent of the child's successor and the grandparent" do
expect(relatable)
.to match_array [child_successor_parent, child_successor_grandparent]
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the child's successor and that's ancestors" do
+ expect(relatable)
+ .to match_array [child_successor, child_successor_parent, child_successor_grandparent]
+ end
+ end
+
context "for a 'precedes' relation" do
let(:relation_type) { Relation::TYPE_PRECEDES }
@@ -953,6 +1139,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the child's predecessor and that's ancestors" do
+ expect(relatable)
+ .to match_array [child_predecessor, child_predecessor_parent, child_predecessor_grandparent]
+ end
+ end
+
context "for a 'precedes' relation" do
let(:relation_type) { Relation::TYPE_PRECEDES }
@@ -1004,6 +1199,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the work package blocked by the child and that's ancestors" do
+ expect(relatable)
+ .to match_array [child_blocked, child_blocked_parent, child_blocked_grandparent]
+ end
+ end
+
context "for a 'precedes' relation" do
let(:relation_type) { Relation::TYPE_PRECEDES }
@@ -1070,6 +1274,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
end
end
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the predecessor's child" do
+ expect(relatable)
+ .to match_array [predecessor_child]
+ end
+ end
+
context "for a 'precedes' relation" do
let(:relation_type) { Relation::TYPE_PRECEDES }
@@ -1156,6 +1369,15 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array [blocks_parent]
end
end
+
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the child" do
+ expect(relatable)
+ .to match_array [blocks_child]
+ end
+ end
end
context 'with a blocked work package that has a child and a parent' do
@@ -1198,5 +1420,14 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to match_array [blocked_parent]
end
end
+
+ context "for a 'child' relation" do
+ let(:relation_type) { Relation::TYPE_CHILD }
+
+ it "contains the child" do
+ expect(relatable)
+ .to match_array [blocked_child]
+ end
+ end
end
end