add the ability to clone a template
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="<%= local_assigns[:class] %>" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||||
<path d="M8 8m0 2a2 2 0 0 1 2 -2h8a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-8a2 2 0 0 1 -2 -2z" />
|
||||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 450 B |
@@ -0,0 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="<%= local_assigns[:class] %>" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||||
<path d="M4 20h4l10.5 -10.5a1.5 1.5 0 0 0 -4 -4l-10.5 10.5v4" />
|
||||
<path d="M13.5 6.5l4 4" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 378 B |
@@ -1,9 +1,22 @@
|
||||
<div class="card card-compact bg-primary mb-12 md:card-normal">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-4xl flex justify-between">
|
||||
<%= @template.name %>
|
||||
<%= link_to 'Edit', template_path(@template), class: 'btn btn-outline btn-sm' %>
|
||||
</h2>
|
||||
<div class="grid md:grid-cols-2 gap-4 md:flex md:justify-between">
|
||||
<h2 class="card-title text-4xl ">
|
||||
<%= @template.name %>
|
||||
</h2>
|
||||
<div class="flex md:justify-between space-x-2">
|
||||
<%= 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') %>
|
||||
<span>Clone</span>
|
||||
<% end %>
|
||||
<%= link_to template_path(@template), class: 'btn btn-outline btn-sm' do %>
|
||||
<span class="flex items-center justify-center space-x-2">
|
||||
<%= svg_icon('pencil', class: 'w-6 h-6') %>
|
||||
<span>Edit</span>
|
||||
</span>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% if @template.submitters.size == 1 %>
|
||||
<div class="join w-full">
|
||||
<buttun class="btn bg-neutral btn-disabled text-white join-item">
|
||||
|
||||
@@ -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 %>
|
||||
<div class="form-control my-6">
|
||||
<%= f.text_field :name, required: true, placeholder: 'Template Name', class: 'base-input' %>
|
||||
</div>
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user