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

move assignable_parents loging into scope

ulferts5104117b52b5
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index a74d8a4e5b5..4621966546e 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -62,9 +62,7 @@ module Projects
def valid?(context = :saving_custom_fields) = super
def assignable_parents
- Project
- .allowed_to(user, :add_subprojects)
- .where.not(id: model.self_and_descendants)
+ Project.assignable_parents(user, model)
end
def available_custom_fields
@@ -100,7 +98,7 @@ module Projects
def validate_parent_assignable
if model.parent &&
model.parent_id_changed? &&
- !assignable_parents.where(id: parent.id).exists?
+ !assignable_parents.exists?(id: parent.id)
errors.add(:parent, :does_not_exist)
end
end
diff --git a/app/models/project.rb b/app/models/project.rb
index d77b82a8122..786c3c686cc 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -198,7 +198,8 @@ class Project < ApplicationRecord
scopes :activated_in_storage,
:allowed_to,
:available_custom_fields,
- :visible
+ :visible,
+ :assignable_parents
scope :has_module, ->(mod) {
where(["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s])
diff --git a/app/models/projects/scopes/assignable_parents.rb b/app/models/projects/scopes/assignable_parents.rb
new file mode 100644
index 00000000000..f454e48137f
--- /dev/null
+++ b/app/models/projects/scopes/assignable_parents.rb
@@ -0,0 +1,43 @@
+# 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.
+# ++
+
+module Projects::Scopes
+ module AssignableParents
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def assignable_parents(user, project)
+ Project
+ .allowed_to(user, :add_subprojects)
+ .where.not(id: project.self_and_descendants)
+ end
+ end
+ end
+end
diff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rb
index 9439f4a8470..50eb2dd2f89 100644
--- a/spec/contracts/projects/shared_contract_examples.rb
+++ b/spec/contracts/projects/shared_contract_examples.rb
@@ -41,6 +41,19 @@ RSpec.shared_examples_for "project contract" do
mock.allow_in_project(*project_permissions, project:)
mock.allow_globally(*global_permissions)
end
+
+ assignable_parents_scope = instance_double(ActiveRecord::Relation,
+ empty?: assignable_parents.empty?,
+ to_a: assignable_parents)
+
+ allow(Project)
+ .to receive(:assignable_parents)
+ .and_return(assignable_parents_scope)
+
+ allow(assignable_parents_scope)
+ .to receive(:exists?) do |id:|
+ assignable_parents.map(&:id).include?(id)
+ end
end
let(:project_permissions) { [] }
@@ -57,42 +70,7 @@ RSpec.shared_examples_for "project contract" do
let(:project_parent) do
build_stubbed(:project)
end
- let(:parent_assignable) { true }
- let!(:assignable_parents) do
- assignable_parents_scope = double("assignable parents scope")
- assignable_parents = double("assignable parents")
-
- allow(Project)
- .to receive(:allowed_to)
- .and_call_original
-
- allow(Project)
- .to receive(:allowed_to)
- .with(current_user, :add_subprojects)
- .and_return assignable_parents_scope
-
- allow(assignable_parents_scope)
- .to receive(:where)
- .and_return(assignable_parents_scope)
-
- allow(assignable_parents_scope)
- .to receive(:not)
- .with(id: project.self_and_descendants)
- .and_return(assignable_parents)
-
- if project_parent
- allow(assignable_parents)
- .to receive(:where)
- .with(id: project_parent.id)
- .and_return(assignable_parents_scope)
-
- allow(assignable_parents_scope)
- .to receive(:exists?)
- .and_return(parent_assignable)
- end
-
- assignable_parents
- end
+ let(:assignable_parents) { [project_parent] }
it_behaves_like "contract is valid"
@@ -115,7 +93,7 @@ RSpec.shared_examples_for "project contract" do
end
context "if the parent is not in the set of assignable_parents" do
- let(:parent_assignable) { false }
+ let(:assignable_parents) { [] }
it_behaves_like "contract is invalid", parent: %i(does_not_exist)
end
@@ -227,8 +205,8 @@ RSpec.shared_examples_for "project contract" do
assignable_parents
end
- it "returns all projects the user has the add_subprojects permissions for" do
- expect(contract.assignable_parents)
+ it "returns the projects Project.assignable_parents returns" do
+ expect(contract.assignable_parents.to_a)
.to eql assignable_parents
end
end
diff --git a/spec/models/projects/scopes/assignable_parents_spec.rb b/spec/models/projects/scopes/assignable_parents_spec.rb
new file mode 100644
index 00000000000..28b08e322cb
--- /dev/null
+++ b/spec/models/projects/scopes/assignable_parents_spec.rb
@@ -0,0 +1,47 @@
+# 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.
+# ++
+
+require "spec_helper"
+
+RSpec.describe Projects::Scopes::AssignableParents do
+ shared_let(:add_subprojects_role) { create(:project_role, permissions: %i[add_subprojects]) }
+ shared_let(:view_role) { create(:project_role, permissions: %i[]) }
+ shared_let(:user) { create(:user) }
+ shared_let(:project_with_permission) { create(:project, members: { user => add_subprojects_role }) }
+ shared_let(:parent_project) { create(:project, members: { user => add_subprojects_role }) }
+ shared_let(:subject_project) { create(:project, parent: parent_project, members: { user => add_subprojects_role }) }
+ shared_let(:child_project) { create(:project, parent: subject_project, members: { user => add_subprojects_role }) }
+ shared_let(:project_without_permission) { create(:project, members: { user => view_role }) }
+
+ it "returns all projects the user has the add_subprojects permission in but without self or descendants" do
+ expect(Project.assignable_parents(user, subject_project))
+ .to contain_exactly(project_with_permission, parent_project)
+ end
+end