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
improves the performance of Relations#visible
ulferts8ff03e750fc9
diff --git a/app/models/relation.rb b/app/models/relation.rbindex 861320abd6c..642a89f2afb 100644--- a/app/models/relation.rb+++ b/app/models/relation.rb@@ -33,7 +33,8 @@ class Relation < ApplicationRecordinclude ::Scopes::Scoped- scopes :follows_non_manual_ancestors+ scopes :follows_non_manual_ancestors,+ :visiblescope :of_work_package,->(work_package) { where('from_id = ? OR to_id = ?', work_package, work_package) }@@ -141,12 +142,6 @@ class Relation < ApplicationRecordendend- def self.visible(user = User.current)- direct- .where(from_id: WorkPackage.visible(user))- .where(to_id: WorkPackage.visible(user))- end-def self.from_work_package_or_ancestors(work_package)ancestor_or_self_ids = work_package.ancestors_relationsdiff --git a/app/models/relations/scopes/visible.rb b/app/models/relations/scopes/visible.rbnew file mode 100644index 00000000000..d1b30612d8a--- /dev/null+++ b/app/models/relations/scopes/visible.rb@@ -0,0 +1,63 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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.+#++++module Relations::Scopes+ module Visible+ extend ActiveSupport::Concern++ class_methods do+ # Returns all relationships visible to the user. The relationships have to be:+ # * Direct (not transitive and not reflexive) so they have to be user generated and not a relationship+ # generated by the system.+ # * Start (from_id) on a work package visible to the user (view_work_packages in the work package's project)+ # * End (to_id) on a work package visible to the user (view_work_packages in the work package's project)+ # @param [User] user+ def visible(user = User.current)+ # Initially, a where(from_id: WorkPackage.visible).where(to_id: WorkPackage.visible)+ # was used. That approach lead to the partial index+ # '(hierarchy + relates + duplicates + follows + blocks + includes + requires = 1) AND relations.hierarchy = 0'+ # not being used, however.+ direct+ .joins(visible_join(user, :from_id, 'visible_from'))+ .joins(visible_join(user, :to_id, 'visible_to'))+ end++ def visible_join(user, relation_column, alias_name)+ relations_table = Relation.arel_table+ visible_table = Arel::Nodes::TableAlias.new(WorkPackage.visible(user).select(:id).arel, alias_name)++ relations_table+ .join(visible_table)+ .on(visible_table[:id].eq(relations_table[relation_column]))+ .join_sources+ end+ end+ end+enddiff --git a/spec/models/relation_spec.rb b/spec/models/relation_spec.rbindex 528ea1108bd..cfed693877f 100644--- a/spec/models/relation_spec.rb+++ b/spec/models/relation_spec.rb@@ -173,62 +173,6 @@ describe Relation, type: :model doendend- describe '.visible' do- let(:user) { FactoryBot.create(:user) }- let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }- let(:member_project_to) do- FactoryBot.create(:member,- project: to.project,- user: user,- roles: [role])- end-- let(:member_project_from) do- FactoryBot.create(:member,- project: from.project,- user: user,- roles: [role])- end-- before do- relation.save!- end-- context 'user can see both work packages' do- before do- member_project_to- member_project_from- end-- it 'returns the relation' do- expect(Relation.visible(user))- .to match_array([relation])- end- end-- context 'user can see only the from work packages' do- before do- member_project_from- end-- it 'does not return the relation' do- expect(Relation.visible(user))- .to be_empty- end- end-- context 'user can see only the to work packages' do- before do- member_project_to- end-- it 'does not return the relation' do- expect(Relation.visible(user))- .to be_empty- end- end- end-describe 'it should validate circular dependency' dolet(:otherwp) { FactoryBot.create(:work_package) }let(:relation) dodiff --git a/spec/models/relations/scopes/visible_spec.rb b/spec/models/relations/scopes/visible_spec.rbnew file mode 100644index 00000000000..b45763bf9cb--- /dev/null+++ b/spec/models/relations/scopes/visible_spec.rb@@ -0,0 +1,93 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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.+#++++require 'spec_helper'++describe Relations::Scopes::Visible, type: :model do+ let(:from) { FactoryBot.create(:work_package, project: project1) }+ let(:intermediary) { FactoryBot.create(:work_package, project: project1) }+ let(:to) { FactoryBot.create(:work_package, project: project2) }+ let(:project1) { FactoryBot.create(:project) }+ let(:project2) { FactoryBot.create(:project) }+ let(:type) { 'relates' }+ let!(:relation1) { FactoryBot.create(:relation, from: from, to: intermediary, relation_type: type) }+ let!(:relation2) { FactoryBot.create(:relation, from: intermediary, to: to, relation_type: type) }+ let(:user) { FactoryBot.create(:user) }+ let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }+ let(:member_project1) do+ FactoryBot.create(:member,+ project: project1,+ user: user,+ roles: [role])+ end++ let(:member_project2) do+ FactoryBot.create(:member,+ project: project2,+ user: user,+ roles: [role])+ end++ describe '.visible' do+ context 'when the user can see all work packages' do+ before do+ member_project1+ member_project2+ end++ it 'returns the relations' do+ expect(Relation.visible(user))+ .to match_array([relation1, relation2])+ end+ end++ context 'when the user can see only the work packages in one project' do+ before do+ member_project1+ end++ it 'returns the relation within the one project' do+ expect(Relation.visible(user))+ .to match_array([relation1])+ end+ end++ context 'when the user can see only the to work packages' do+ before do+ member_project2+ end++ it 'does not return any relation (as the relation points outside the project)' do+ expect(Relation.visible(user))+ .to be_empty+ end+ end+ end+end