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 @@
-

- <%= @template.name %> - <%= link_to 'Edit', template_path(@template), class: 'btn btn-outline btn-sm' %> -

+
+

+ <%= @template.name %> +

+
+ <%= link_to new_template_path(base_template_id: @template.id), class: 'btn btn-outline btn-sm', data: { turbo_frame: :modal } do %> + <%= svg_icon('copy', class: 'w-6 h-6') %> + Clone + <% end %> + <%= link_to template_path(@template), class: 'btn btn-outline btn-sm' do %> + + <%= svg_icon('pencil', class: 'w-6 h-6') %> + Edit + + <% end %> +
+
<% if @template.submitters.size == 1 %>
diff --git a/app/views/templates/new.html.erb b/app/views/templates/new.html.erb index efc1ab96..1b64d2ae 100644 --- a/app/views/templates/new.html.erb +++ b/app/views/templates/new.html.erb @@ -1,5 +1,8 @@ <%= render 'shared/turbo_modal', title: 'New Template' do %> <%= form_for @template, data: { turbo_frame: :_top } do |f| %> + <% if @base_template %> + <%= hidden_field_tag :base_template_id, @base_template.id %> + <% end %>
<%= f.text_field :name, required: true, placeholder: 'Template Name', class: 'base-input' %>
diff --git a/lib/templates/clone_attachments.rb b/lib/templates/clone_attachments.rb new file mode 100644 index 00000000..81948e57 --- /dev/null +++ b/lib/templates/clone_attachments.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Templates + module CloneAttachments + module_function + + def call(template:, original_template:) + original_template.documents.each do |document| + new_document = ActiveStorage::Attachment.create!( + uuid: document.uuid, + blob_id: document.blob_id, + name: 'documents', + record: template + ) + + document.preview_images_attachments.each do |preview_image| + ActiveStorage::Attachment.create!( + uuid: preview_image.uuid, + blob_id: preview_image.blob_id, + name: 'preview_images', + record: new_document + ) + end + end + end + end +end