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 available templates method into contract

Oliver Günther9173cd82d76f
diff --git a/app/contracts/projects/instantiate_template_contract.rb b/app/contracts/projects/instantiate_template_contract.rb
index 07889193ab9..baef08a3fab 100644
--- a/app/contracts/projects/instantiate_template_contract.rb
+++ b/app/contracts/projects/instantiate_template_contract.rb
@@ -28,6 +28,12 @@
module Projects
class InstantiateTemplateContract < CreateContract
+ def self.visible_templates(user)
+ Project
+ .allowed_to(user, :copy_projects)
+ .where(templated: true)
+ end
+
def validate
validate_user_allowed_to_instantiate_template
@@ -37,13 +43,17 @@ module Projects
private
def validate_user_allowed_to_instantiate_template
- unless template_project && user.allowed_to?(:copy_projects, template_project)
- errors.add :base, :error_unauthorized
- end
+ errors.add(:base, :error_unauthorized) unless visible_template?
+ end
+
+ def visible_template?
+ return false if template_project_id.nil?
+
+ self.class.visible_templates(user).exists?(template_project_id)
end
- def template_project
- options[:template_project]
+ def template_project_id
+ options[:template_project_id]
end
end
end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index a982969d0c6..aaa11a03b2e 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -147,9 +147,8 @@ module ProjectsHelper
end
def project_options_for_templated
- Project
- .allowed_to(current_user, :copy_projects)
- .where(templated: true)
+ ::Projects::InstantiateTemplateContract
+ .visible_templates(current_user)
.pluck(:name, :id)
end
diff --git a/app/services/projects/instantiate_template_service.rb b/app/services/projects/instantiate_template_service.rb
index 5da4d9ad0b0..b8301e3597c 100644
--- a/app/services/projects/instantiate_template_service.rb
+++ b/app/services/projects/instantiate_template_service.rb
@@ -30,20 +30,20 @@
module Projects
class InstantiateTemplateService < ::BaseServices::Create
- attr_reader :template_project
+ attr_reader :template_id
def initialize(user:, template_id:)
- @template_project = Project.find_by(id: template_id)
+ @template_id = template_id
super user: user,
contract_class: Projects::InstantiateTemplateContract,
- contract_options: { template_project: template_project }
+ contract_options: { template_project_id: template_id }
end
def after_validate(params, call)
::CopyProjectJob.perform_later(
user_id: user.id,
- source_project_id: template_project.id,
+ source_project_id: template_id,
target_project_params: params,
# Copy all associations
associations_to_copy: nil,
diff --git a/spec/contracts/projects/instantiate_template_contract_spec.rb b/spec/contracts/projects/instantiate_template_contract_spec.rb
index d24f88086a1..344fe04c11f 100644
--- a/spec/contracts/projects/instantiate_template_contract_spec.rb
+++ b/spec/contracts/projects/instantiate_template_contract_spec.rb
@@ -33,7 +33,7 @@ describe Projects::InstantiateTemplateContract do
let(:user) { FactoryBot.build_stubbed :user }
let(:project) { Project.new name: 'Foo Bar', identifier: 'foo' }
let(:template) { FactoryBot.build_stubbed :project }
- let(:options) { { template_project: template } }
+ let(:options) { { template_project_id: template.id } }
subject { described_class.new(project, user, options: options) }
@@ -43,9 +43,8 @@ describe Projects::InstantiateTemplateContract do
.with(:add_project)
.and_return(allowed_to_add)
- allow(user)
- .to(receive(:allowed_to?))
- .with(:copy_projects, template)
+ allow(Project)
+ .to receive_message_chain(:allowed_to, :where, :exists?)
.and_return(allowed_to_copy)
end