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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
refactor: introduce relations mediator
Christophe Bliard893797843290
diff --git a/app/components/work_package_relations_tab/index_component.rb b/app/components/work_package_relations_tab/index_component.rbindex cbf0e528134..bcb853980ea 100644--- a/app/components/work_package_relations_tab/index_component.rb+++ b/app/components/work_package_relations_tab/index_component.rb@@ -16,20 +16,18 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentinclude Turbo::FramesHelperinclude 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_toend@@ -41,13 +39,13 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentprivatedef 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)enddef 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)enddef should_render_create_button?@@ -60,8 +58,6 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentendend- def any_relations? = relations.any? || children.any?-def render_relation_group(title:, relation_type:, items:, &_block)render(border_box_container(padding: :condensed,@@ -109,7 +105,7 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponentdef render_child_menu_items(menu) # rubocop:disable Metrics/AbcSizereturn unless should_render_add_child?- if helpers.current_user.allowed_in_project?(:add_work_packages, @work_package.project)+ if helpers.current_user.allowed_in_project?(:add_work_packages, work_package.project)menu.with_item(label: t("work_package_relations_tab.relations.new_child"),href: new_project_work_packages_dialog_path(work_package.project, parent_id: work_package.id),diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rbnew file mode 100644index 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+enddiff --git a/app/controllers/work_package_children_relations_controller.rb b/app/controllers/work_package_children_relations_controller.rbindex 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 < ApplicationControllerdef 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.rbindex 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 < ApplicationControllerif service_result.success?@work_package.reloadcomponent = 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 < ApplicationControllerif 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_streamselse@@ -94,16 +90,7 @@ class WorkPackageRelationsController < ApplicationControllerservice_result = Relations::DeleteService.new(user: current_user, model: @relation).callif 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_streamselsediff --git a/app/controllers/work_package_relations_tab_controller.rb b/app/controllers/work_package_relations_tab_controller.rbindex 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 < ApplicationControllerbefore_action :authorize_globaldef 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 dodiff --git a/app/views/work_package_relations_tab/_index.html.erb b/app/views/work_package_relations_tab/_index.html.erbdeleted file mode 100644index 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.rbindex 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 dosend_delete_requestexpect(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))enddiff --git a/spec/controllers/work_package_relations_controller_spec.rb b/spec/controllers/work_package_relations_controller_spec.rbindex 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 donew_relation = Relation.lastexpect(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 doexpect(response).to be_successfulexpect(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 doexpect(response).to be_successfulexpect(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.rbindex 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 doit "renders the relations tab" doget("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_successfulenddiff --git a/spec/features/work_packages/details/relations/hierarchy_spec.rb b/spec/features/work_packages/details/relations/hierarchy_spec.rbindex 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 doinclude_context "ng-select-autocomplete helpers"let(:user) { create(:admin) }