diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 9a511c73..3beda4fe 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -3,6 +3,8 @@ class TemplatesController < ApplicationController layout false + before_action :load_base_template, only: %i[new create] + def show @template = current_account.templates.preload(documents_attachments: { preview_images_attachments: :blob }) .find(params[:id]) @@ -10,13 +12,22 @@ class TemplatesController < ApplicationController def new @template = current_account.templates.new + @template.name = "#{@base_template.name} (Clone)" if @base_template end def create - @template = current_account.templates.new(template_params) + @template = + if @base_template + current_account.templates.new(**@base_template.slice(:fields, :schema, :submitters), **template_params) + else + current_account.templates.new(template_params) + end + @template.author = current_user if @template.save + Templates::CloneAttachments.call(template: @template, original_template: @base_template) if @base_template + redirect_to template_path(@template) else render turbo_stream: turbo_stream.replace(:modal, template: 'templates/new'), status: :unprocessable_entity @@ -35,4 +46,12 @@ class TemplatesController < ApplicationController def template_params params.require(:template).permit(:name) end + + def load_base_template + return if params[:base_template_id].blank? + + @base_template = current_account.templates + .preload(documents_attachments: :preview_images_attachments) + .find_by(id: params[:base_template_id]) + end end diff --git a/app/views/icons/_copy.html.erb b/app/views/icons/_copy.html.erb new file mode 100644 index 00000000..432d5f26 --- /dev/null +++ b/app/views/icons/_copy.html.erb @@ -0,0 +1,5 @@ + diff --git a/app/views/icons/_pencil.html.erb b/app/views/icons/_pencil.html.erb new file mode 100644 index 00000000..6c16e35c --- /dev/null +++ b/app/views/icons/_pencil.html.erb @@ -0,0 +1,5 @@ + diff --git a/app/views/submissions/index.html.erb b/app/views/submissions/index.html.erb index fd20aabc..c637323f 100644 --- a/app/views/submissions/index.html.erb +++ b/app/views/submissions/index.html.erb @@ -1,9 +1,22 @@