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

refactor: introduce relations mediator

Christophe Bliarddb8ee1ce9d99
diff --git a/app/components/work_package_relations_tab/index_component.html.erb b/app/components/work_package_relations_tab/index_component.html.erb
index c12694bc30e..fcedb75f462 100644
--- a/app/components/work_package_relations_tab/index_component.html.erb
+++ b/app/components/work_package_relations_tab/index_component.html.erb
@@ -37,7 +37,7 @@
if should_render_add_child?
menu.with_item(
label: t("#{I18N_NAMESPACE}.relations.label_child_singular").capitalize,
- href: new_work_package_children_relation_path(@work_package),
+ href: new_work_package_children_relation_path(work_package),
test_selector: new_button_test_selector(relation_type: :child),
content_arguments: {
data: { turbo_stream: true }
diff --git a/app/components/work_package_relations_tab/index_component.rb b/app/components/work_package_relations_tab/index_component.rb
index 247cc519594..63a4c316360 100644
--- a/app/components/work_package_relations_tab/index_component.rb
+++ b/app/components/work_package_relations_tab/index_component.rb
@@ -15,20 +15,18 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
include Turbo::FramesHelper
include OpTurbo::Streamable
- attr_reader :work_package, :relations, :children, :directionally_aware_grouped_relations, :relation_to_scroll_to
+ attr_reader :relations_mediator, :directionally_aware_grouped_relations, :relation_to_scroll_to
+
+ delegate :work_package, :relations, :children, :any_relations?, to: :relations_mediator
# Initialize the component with required data
#
# @param work_package [WorkPackage] The work package whose relations are being displayed
- # @param relations [Array<Relation>] The relations associated with this work package
- # @param children [Array<WorkPackage>] Child work packages
# @param relation_to_scroll_to [Relation, WorkPackage, nil] Optional relation or child to scroll to when rendering
- def initialize(work_package:, relations:, children:, relation_to_scroll_to: nil)
+ def initialize(work_package: nil, relation_to_scroll_to: nil)
super()
- @work_package = work_package
- @relations = relations
- @children = children
+ @relations_mediator = WorkPackageRelationsTab::RelationsMediator.new(work_package:)
@directionally_aware_grouped_relations = group_relations_by_directional_context
@relation_to_scroll_to = relation_to_scroll_to
end
@@ -40,13 +38,13 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
private
def should_render_add_child?
- return false if @work_package.milestone?
+ return false if work_package.milestone?
- helpers.current_user.allowed_in_project?(:manage_subtasks, @work_package.project)
+ helpers.current_user.allowed_in_project?(:manage_subtasks, work_package.project)
end
def should_render_add_relations?
- helpers.current_user.allowed_in_project?(:manage_work_package_relations, @work_package.project)
+ helpers.current_user.allowed_in_project?(:manage_work_package_relations, work_package.project)
end
def should_render_create_button?
@@ -59,8 +57,6 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
end
end
- def any_relations? = relations.any? || children.any?
-
def render_relation_group(title:, relation_type:, items:, &_block)
render(border_box_container(
padding: :condensed,
diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rb
new file mode 100644
index 00000000000..8c21e51e730
--- /dev/null
+++ b/app/components/work_package_relations_tab/relations_mediator.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+class WorkPackageRelationsTab::RelationsMediator
+ attr_reader :work_package
+
+ def initialize(work_package:)
+ @work_package = work_package
+ end
+
+ def relations
+ @relations ||= work_package.relations.includes(:to, :from).visible
+ end
+
+ def children
+ @children ||= work_package.children.visible
+ end
+
+ def any_relations?
+ relations.any? || children.any?
+ end
+end
diff --git a/app/controllers/work_package_children_relations_controller.rb b/app/controllers/work_package_children_relations_controller.rb
index 6fecc9b2a93..7e356ccc237 100644
--- a/app/controllers/work_package_children_relations_controller.rb
+++ b/app/controllers/work_package_children_relations_controller.rb
@@ -66,12 +66,7 @@ class WorkPackageChildrenRelationsController < ApplicationController
def respond_with_relations_tab_update(service_result, **)
if service_result.success?
@work_package.reload
- component = WorkPackageRelationsTab::IndexComponent.new(
- work_package: @work_package,
- relations: @work_package.relations.visible,
- children: @work_package.children.visible,
- **
- )
+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package, **)
replace_via_turbo_stream(component:)
render_success_flash_message_via_turbo_stream(message: I18n.t(:notice_successful_update))
diff --git a/app/controllers/work_package_relations_controller.rb b/app/controllers/work_package_relations_controller.rb
index 02e7f68b013..61fab59c7e1 100644
--- a/app/controllers/work_package_relations_controller.rb
+++ b/app/controllers/work_package_relations_controller.rb
@@ -62,8 +62,6 @@ class WorkPackageRelationsController < ApplicationController
if service_result.success?
@work_package.reload
component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package,
- relations: @work_package.relations.visible,
- children: @work_package.children.visible,
relation_to_scroll_to: service_result.result)
replace_via_turbo_stream(component:)
respond_with_turbo_streams
@@ -80,9 +78,7 @@ class WorkPackageRelationsController < ApplicationController
if service_result.success?
@work_package.reload
- component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package,
- relations: @work_package.relations.visible,
- children: @work_package.children.visible)
+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package)
replace_via_turbo_stream(component:)
respond_with_turbo_streams
else
@@ -94,16 +90,7 @@ class WorkPackageRelationsController < ApplicationController
service_result = Relations::DeleteService.new(user: current_user, model: @relation).call
if service_result.success?
- @children = WorkPackage.where(parent_id: @work_package.id).visible
- @relations = @work_package
- .relations
- .reload
- .includes(:to, :from)
- .visible
-
- component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package,
- relations: @relations,
- children: @children)
+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package.reload)
replace_via_turbo_stream(component:)
respond_with_turbo_streams
else
diff --git a/app/controllers/work_package_relations_tab_controller.rb b/app/controllers/work_package_relations_tab_controller.rb
index f457a67fd93..097b4b8296e 100644
--- a/app/controllers/work_package_relations_tab_controller.rb
+++ b/app/controllers/work_package_relations_tab_controller.rb
@@ -33,17 +33,7 @@ class WorkPackageRelationsTabController < ApplicationController
before_action :authorize_global
def index
- @children = WorkPackage.where(parent_id: @work_package.id).visible
- @relations = @work_package
- .relations
- .visible
- .includes(:to, :from)
-
- component = WorkPackageRelationsTab::IndexComponent.new(
- work_package: @work_package,
- relations: @relations,
- children: @children
- )
+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package)
respond_to do |format|
format.html do
diff --git a/app/views/work_package_relations_tab/_index.html.erb b/app/views/work_package_relations_tab/_index.html.erb
deleted file mode 100644
index 43c920b1591..00000000000
--- a/app/views/work_package_relations_tab/_index.html.erb
+++ /dev/null
@@ -1,6 +0,0 @@
-<%= render(
- WorkPackageRelationsTab::IndexComponent.new(
- work_package: @work_package,
- relations: @relations
- )
-) %>
diff --git a/spec/controllers/work_package_children_relations_controller_spec.rb b/spec/controllers/work_package_children_relations_controller_spec.rb
index e4e0388beea..8c820dbb1f8 100644
--- a/spec/controllers/work_package_children_relations_controller_spec.rb
+++ b/spec/controllers/work_package_children_relations_controller_spec.rb
@@ -100,7 +100,7 @@ RSpec.describe WorkPackageChildrenRelationsController do
send_delete_request
expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)
- .with(work_package:, relations: [], children: [])
+ .with(work_package:)
expect(controller).to have_received(:replace_via_turbo_stream)
.with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))
end
diff --git a/spec/controllers/work_package_relations_controller_spec.rb b/spec/controllers/work_package_relations_controller_spec.rb
index ba571d56983..0503650f5ac 100644
--- a/spec/controllers/work_package_relations_controller_spec.rb
+++ b/spec/controllers/work_package_relations_controller_spec.rb
@@ -117,7 +117,7 @@ RSpec.describe WorkPackageRelationsController do
new_relation = Relation.last
expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)
- .with(work_package:, relations: [relation, new_relation], children:, relation_to_scroll_to: new_relation)
+ .with(work_package:, relation_to_scroll_to: new_relation)
expect(controller).to have_received(:replace_via_turbo_stream)
.with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))
end
@@ -142,7 +142,7 @@ RSpec.describe WorkPackageRelationsController do
expect(response).to be_successful
expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)
- .with(work_package:, relations: [relation], children:)
+ .with(work_package:)
expect(controller).to have_received(:replace_via_turbo_stream)
.with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))
end
@@ -160,7 +160,7 @@ RSpec.describe WorkPackageRelationsController do
expect(response).to be_successful
expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)
- .with(work_package:, relations: [], children:)
+ .with(work_package:)
expect(controller).to have_received(:replace_via_turbo_stream)
.with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))
expect { relation.reload }.to raise_error(ActiveRecord::RecordNotFound)
diff --git a/spec/controllers/work_package_relations_tab_controller_spec.rb b/spec/controllers/work_package_relations_tab_controller_spec.rb
index e9b3f8b4b79..8992bf6d467 100644
--- a/spec/controllers/work_package_relations_tab_controller_spec.rb
+++ b/spec/controllers/work_package_relations_tab_controller_spec.rb
@@ -55,11 +55,8 @@ RSpec.describe WorkPackageRelationsTabController do
it "renders the relations tab" do
get("index", params: { work_package_id: work_package.id }, as: :turbo_stream)
- expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new).with(
- work_package:,
- relations:,
- children:
- )
+ expect(WorkPackageRelationsTab::IndexComponent)
+ .to have_received(:new).with(work_package:)
expect(response).to be_successful
end
diff --git a/spec/features/work_packages/details/relations/hierarchy_spec.rb b/spec/features/work_packages/details/relations/hierarchy_spec.rb
index ca29971fc18..3b73a27b63e 100644
--- a/spec/features/work_packages/details/relations/hierarchy_spec.rb
+++ b/spec/features/work_packages/details/relations/hierarchy_spec.rb
@@ -28,7 +28,7 @@
require "spec_helper"
-RSpec.shared_examples "work package relations tab", :js, :selenium do
+RSpec.shared_examples "work package relations tab", :js, :with_cuprite do
include_context "ng-select-autocomplete helpers"
let(:user) { create(:admin) }