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

Create separate service/contract for instantiation of templates

Oliver Günthere165eb34e7c8
diff --git a/app/contracts/projects/instantiate_template_contract.rb b/app/contracts/projects/instantiate_template_contract.rb
index e12fcd355b8..07889193ab9 100644
--- a/app/contracts/projects/instantiate_template_contract.rb
+++ b/app/contracts/projects/instantiate_template_contract.rb
@@ -27,15 +27,23 @@
#++
module Projects
- class CreateContract < BaseContract
- private
+ class InstantiateTemplateContract < CreateContract
+ def validate
+ validate_user_allowed_to_instantiate_template
+
+ super
+ end
- def validate_user_allowed_to_manage
- unless user.allowed_to_globally?(:add_project) ||
- model.parent && user.allowed_to?(:add_subprojects, model.parent)
+ private
+ def validate_user_allowed_to_instantiate_template
+ unless template_project && user.allowed_to?(:copy_projects, template_project)
errors.add :base, :error_unauthorized
end
end
+
+ def template_project
+ options[:template_project]
+ end
end
end
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index ceda575ff6e..0d3a291dca3 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -80,10 +80,7 @@ class ProjectsController < ApplicationController
end
def create
- call_result = Projects::CreateService
- .new(user: current_user)
- .call(permitted_params.project)
-
+ call_result = create_project
@project = call_result.result
if call_result.success?
@@ -261,6 +258,17 @@ class ProjectsController < ApplicationController
protected
+ def create_project
+ service =
+ if params[:from_template]
+ Projects::InstantiateTemplateService.new(user: current_user, template_id: params[:from_template])
+ else
+ Projects::CreateService.new(user: current_user)
+ end
+
+ service.call(permitted_params.project)
+ end
+
def set_sorting(query)
orders = query.orders.select(&:valid?).map { |o| [o.attribute.to_s, o.direction.to_s] }
diff --git a/app/services/projects/instantiate_template_service.rb b/app/services/projects/instantiate_template_service.rb
index 6042d8bcb74..23f51e7c9f0 100644
--- a/app/services/projects/instantiate_template_service.rb
+++ b/app/services/projects/instantiate_template_service.rb
@@ -29,15 +29,26 @@
#++
module Projects
- class CreateFromTemplateService < ::BaseServices::BaseContracted
- attr_reader :template_id
+ class InstantiateTemplateService < ::BaseServices::Create
+ attr_reader :template_project
- def initialize(user:, template_id: )
- super user: user, contract_class:
- @template_id = template_id
+ def initialize(user:, template_id:)
+ @template_project = Project.find_by(id: template_id)
+
+ super user: user,
+ contract_class: Projects::InstantiateTemplateContract,
+ contract_options: { template_project: template_project }
+ end
+
+ def after_validate(params, call)
+ # TODO
+ warn "Scheduling job for #{params.inspect} to create template copy from #{template_project.inspect}"
end
- def after_validate(params)
+ # Do not actually try to save the project here
+ # but simply pass the previous call
+ def persist(call)
+ call
end
end
end