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

[38030] Add parent relation to relations tab

Christophe Bliard1af5aea5b0d3
diff --git a/app/components/work_package_relations_tab/relation_component.rb b/app/components/work_package_relations_tab/relation_component.rb
index 912b11f7d77..a545b4cd5c2 100644
--- a/app/components/work_package_relations_tab/relation_component.rb
+++ b/app/components/work_package_relations_tab/relation_component.rb
@@ -51,10 +51,6 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
relation_item.related
end
- def child
- relation_item.related if parent_child_relationship?
- end
-
def editable? = editable
private
@@ -62,7 +58,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
def parent_child_relationship? = relation.nil?
def should_render_edit_option?
- # Children have nothing to edit as it's not a relation.
+ # Children and parent can not be edited as it's not a relation.
!parent_child_relationship? && allowed_to_manage_relations?
end
@@ -70,7 +66,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
return false unless editable?
if parent_child_relationship?
- allowed_to_manage_subtasks?
+ allowed_to_manage_subtasks? && relation_item.type.child?
else
allowed_to_manage_relations?
end
@@ -78,7 +74,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
def allowed_to_manage_subtasks?
helpers.current_user.allowed_in_project?(:manage_subtasks, work_package.project) &&
- helpers.current_user.allowed_in_project?(:manage_subtasks, child.project)
+ helpers.current_user.allowed_in_project?(:manage_subtasks, related_work_package.project)
end
def allowed_to_manage_relations?
@@ -121,7 +117,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
def destroy_path
if parent_child_relationship?
- work_package_children_relation_path(work_package, child)
+ work_package_children_relation_path(work_package, related_work_package)
else
work_package_relation_path(work_package, relation)
end
diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rb
index 59b2195e060..49fb989d5a5 100644
--- a/app/components/work_package_relations_tab/relations_mediator.rb
+++ b/app/components/work_package_relations_tab/relations_mediator.rb
@@ -37,17 +37,13 @@ class WorkPackageRelationsTab::RelationsMediator
end
def count
- visible_relations.count + ghost_relations.count
+ [visible_relations, ghost_relations].sum(&:count)
end
def any?
visible_relations.any? || ghost_relations.any?
end
- def all_relations
- visible_relations + ghost_relations
- end
-
def all_relation_items
(visible_relation_items + ghost_relation_items).sort_by(&:order_key)
end
@@ -86,10 +82,11 @@ class WorkPackageRelationsTab::RelationsMediator
# relation
RelationItem = Data.define(:type, :work_package, :related, :relation, :visibility, :closest) do
def initialize(type:, work_package:, relation:, visibility: :visible, closest: false)
+ type = ActiveSupport::StringInquirer.new(type.to_s)
if relation.is_a?(Relation)
related = relation.other_work_package(work_package)
else
- related = relation # for parent-child relations, `relation` parameter holds the child work package
+ related = relation # for parent-child relations, `relation` parameter holds the child or parent work package
relation = nil
end
super(type:, work_package:, related:, relation:, visibility:, closest:)
@@ -114,6 +111,10 @@ class WorkPackageRelationsTab::RelationsMediator
@visible_relations ||= work_package.relations.visible.includes(:to, :from).load
end
+ def visible_parents
+ @visible_parents ||= work_package.parent_id && work_package.parent.visible? ? [work_package.parent] : []
+ end
+
def visible_children
@visible_children ||= work_package.children.visible.load
end
@@ -122,6 +123,10 @@ class WorkPackageRelationsTab::RelationsMediator
@ghost_relations ||= work_package.relations.includes(:to, :from).where.not(id: visible_relations.select(:id)).load
end
+ def ghost_parents
+ @ghost_parents ||= work_package.parent_id && !work_package.parent.visible? ? [work_package.parent] : []
+ end
+
def ghost_children
@ghost_children ||= work_package.children.where.not(id: visible_children.select(:id)).load
end
@@ -132,7 +137,15 @@ class WorkPackageRelationsTab::RelationsMediator
end
def relation_group(type)
- if type == Relation::TYPE_CHILD
+ case type
+ when Relation::TYPE_PARENT
+ RelationGroup.new(
+ type:,
+ work_package:,
+ visible_relations: visible_parents,
+ ghost_relations: ghost_parents
+ )
+ when Relation::TYPE_CHILD
RelationGroup.new(
type:,
work_package:,
@@ -150,7 +163,11 @@ class WorkPackageRelationsTab::RelationsMediator
end
def all_relations_count
- visible_relations.count + ghost_relations.count + visible_children.count + ghost_children.count
+ [
+ visible_relations, ghost_relations,
+ visible_parents, ghost_parents,
+ visible_children, ghost_children
+ ].sum(&:count)
end
private
diff --git a/app/models/relation.rb b/app/models/relation.rb
index 9cf62abcd81..f0ff1548c96 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -99,7 +99,7 @@ class Relation < ApplicationRecord
}
}.freeze
- ORDERED_TYPES = [*TYPES.keys, TYPE_CHILD].freeze
+ ORDERED_TYPES = [*TYPES.keys, TYPE_PARENT, TYPE_CHILD].freeze
include ::Scopes::Scoped
diff --git a/spec/components/work_package_relations_tab/index_component_spec.rb b/spec/components/work_package_relations_tab/index_component_spec.rb
index 050ecbcc4de..d4a05b3edb8 100644
--- a/spec/components/work_package_relations_tab/index_component_spec.rb
+++ b/spec/components/work_package_relations_tab/index_component_spec.rb
@@ -50,6 +50,22 @@ RSpec.describe WorkPackageRelationsTab::IndexComponent, type: :component do
end
end
+ context "with parent relation" do
+ shared_let_work_packages(<<~TABLE)
+ hierarchy | MTWTFSS | scheduling mode |
+ parent | XX | automatic |
+ work_package | XX | manual |
+ TABLE
+
+ it "renders the relations group with the parent work package in it" do
+ expect(render_component).to have_test_selector("op-relation-group-parent")
+ expect(render_component).to have_list "Parent"
+
+ list = page.find(:list, "Parent")
+ expect(list).to have_list_item count: 1, text: /parent/
+ end
+ end
+
context "with child relations" do
shared_let_work_packages(<<~TABLE)
hierarchy | MTWTFSS | scheduling mode |
diff --git a/spec/components/work_package_relations_tab/relation_component_spec.rb b/spec/components/work_package_relations_tab/relation_component_spec.rb
index 5b5942cce91..35beb0eba81 100644
--- a/spec/components/work_package_relations_tab/relation_component_spec.rb
+++ b/spec/components/work_package_relations_tab/relation_component_spec.rb
@@ -31,16 +31,19 @@
require "rails_helper"
RSpec.describe WorkPackageRelationsTab::RelationComponent, type: :component do
+ include Rails.application.routes.url_helpers
+
shared_let(:user) { create(:admin) }
shared_let(:work_package) { create(:work_package) }
current_user { user }
shared_let_work_packages(<<~TABLE)
- hierarchy | MTWTFSS | scheduling mode | predecessors
- predecessor | XXX | manual |
- work_package | X | automatic | predecessor with lag 2
- child | | automatic |
+ hierarchy | MTWTFSS | scheduling mode | predecessors
+ predecessor | XXX | manual |
+ parent | X | automatic |
+ work_package | X | automatic | predecessor with lag 2
+ child | | automatic |
TABLE
def build_relation_item(type:, relation:, visibility: :visible, closest: false)
@@ -57,7 +60,7 @@ RSpec.describe WorkPackageRelationsTab::RelationComponent, type: :component do
it "renders a title link" do
expect(render_component(relation_item:))
- .to have_link "child"
+ .to have_link child.subject, href: work_package_path(child)
end
context "when editable" do
@@ -97,6 +100,52 @@ RSpec.describe WorkPackageRelationsTab::RelationComponent, type: :component do
end
end
+ context "with parent relation item" do
+ context "when visible" do
+ let(:relation_item) { build_relation_item(type: Relation::TYPE_PARENT, relation: parent, visibility: :visible) }
+
+ it "renders a title link" do
+ expect(render_component(relation_item:))
+ .to have_link parent.subject, href: work_package_path(parent)
+ end
+
+ context "when editable" do
+ it "renders an action menu", skip: "TODO: Add action menu to parent relations" do
+ component = render_component(relation_item:, editable: true)
+ expect(component).to have_menu
+ expect(component).to have_selector :menuitem, "Delete relation"
+ end
+ end
+
+ context "when not editable" do
+ it "does not render an action menu" do
+ component = render_component(relation_item:, editable: false)
+ expect(component).not_to have_menu
+ end
+ end
+ end
+
+ context "when ghost" do
+ let(:relation_item) { build_relation_item(type: Relation::TYPE_PARENT, relation: parent, visibility: :ghost) }
+
+ it "does not render a title link" do
+ expect(render_component(relation_item:))
+ .to have_no_link "child"
+ end
+
+ it "renders a title and message without details" do
+ rendered_component = render_component(relation_item:)
+ expect(rendered_component).to have_text "Related work package"
+ expect(rendered_component).to have_text "This is not visible to you due to permissions."
+ end
+
+ it "does not render an action menu" do
+ expect(render_component(relation_item:))
+ .to have_no_menu
+ end
+ end
+ end
+
context "with follows relations" do
let(:relation) { _table.relation(predecessor: predecessor) }
let(:relation_item) { build_relation_item(type: Relation::TYPE_FOLLOWS, relation:, visibility: :visible) }
@@ -104,7 +153,7 @@ RSpec.describe WorkPackageRelationsTab::RelationComponent, type: :component do
context "when visible" do
it "renders a title link" do
rendered_component = render_component(relation_item:)
- expect(rendered_component).to have_link "predecessor"
+ expect(rendered_component).to have_link predecessor.subject, href: work_package_path(predecessor)
end
it "renders the lag" do
diff --git a/spec/components/work_package_relations_tab/relations_mediator_spec.rb b/spec/components/work_package_relations_tab/relations_mediator_spec.rb
index b8e62d9b2ee..868882d5d72 100644
--- a/spec/components/work_package_relations_tab/relations_mediator_spec.rb
+++ b/spec/components/work_package_relations_tab/relations_mediator_spec.rb
@@ -40,17 +40,21 @@ RSpec.describe WorkPackageRelationsTab::RelationsMediator do
subject(:mediator) { described_class.new(work_package:) }
context "with a work package having multiple kind of relations" do
- let_work_packages(<<~TABLE)
+ shared_let_work_packages(<<~TABLE)
hierarchy | predecessors | relates to
predecessor | |
related 1 | |
related 2 | | work package
successor | work package |
- work package | predecessor | related 1
- child | |
+ parent | |
+ work package | predecessor | related 1
+ child | |
TABLE
it "returns all relations of the work package" do
+ expect(mediator.relation_group(Relation::TYPE_PARENT).all_relation_items).to contain_exactly(
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_PARENT, related: parent, visibility: :visible)
+ )
expect(mediator.relation_group(Relation::TYPE_CHILD).all_relation_items).to contain_exactly(
have_attributes(class: described_class::RelationItem, type: Relation::TYPE_CHILD, related: child, visibility: :visible)
)
@@ -69,6 +73,55 @@ RSpec.describe WorkPackageRelationsTab::RelationsMediator do
visibility: :visible)
)
end
+
+ describe "#all_relations_count" do
+ it "returns the total number of relations" do
+ expect(mediator.all_relations_count).to eq 6
+ end
+ end
+
+ context "when the related work packages are not visible by the user " \
+ "because they are in a project the user does not have access to" do
+ let(:other_project) { create(:project) }
+ let(:user) { create(:user, member_with_permissions: { other_project => %i[view_work_packages] }) }
+
+ current_user { user }
+
+ before do
+ work_package.project = other_project
+ work_package.save!
+ end
+
+ it "returns all relations of the work package as ghost relations (not visible)" do
+ expect(mediator.relation_group(Relation::TYPE_PARENT).all_relation_items).to contain_exactly(
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_PARENT, related: parent,
+ visibility: :ghost)
+ )
+ expect(mediator.relation_group(Relation::TYPE_CHILD).all_relation_items).to contain_exactly(
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_CHILD, related: child, visibility: :ghost)
+ )
+ expect(mediator.relation_group(Relation::TYPE_PRECEDES).all_relation_items).to contain_exactly(
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_PRECEDES, related: successor,
+ visibility: :ghost)
+ )
+ expect(mediator.relation_group(Relation::TYPE_FOLLOWS).all_relation_items).to contain_exactly(
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_FOLLOWS, related: predecessor,
+ visibility: :ghost)
+ )
+ expect(mediator.relation_group(Relation::TYPE_RELATES).all_relation_items).to contain_exactly(
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_RELATES, related: related1,
+ visibility: :ghost),
+ have_attributes(class: described_class::RelationItem, type: Relation::TYPE_RELATES, related: related2,
+ visibility: :ghost)
+ )
+ end
+
+ describe "#all_relations_count" do
+ it "returns the total number of relations" do
+ expect(mediator.all_relations_count).to eq 6
+ end
+ end
+ end
end
describe "RelationGroup" do