Files
docuseal/app/controllers/templates_clone_and_replace_controller.rb
T

36 lines
1.3 KiB
Ruby
Raw Normal View History

2025-04-25 21:19:28 +03:00
# frozen_string_literal: true
2025-05-09 00:01:50 +03:00
class TemplatesCloneAndReplaceController < ApplicationController
2025-04-25 21:19:28 +03:00
load_and_authorize_resource :template
def create
2025-05-09 00:01:50 +03:00
return head :unprocessable_entity if params[:files].blank?
2025-04-25 21:19:28 +03:00
ActiveRecord::Associations::Preloader.new(
records: [@template],
associations: [schema_documents: :preview_images_attachments]
).call
cloned_template = Templates::Clone.call(@template, author: current_user)
2025-05-09 00:01:50 +03:00
cloned_template.name = File.basename(params[:files].first.original_filename, '.*')
2025-04-25 21:19:28 +03:00
cloned_template.save!
documents = Templates::ReplaceAttachments.call(cloned_template, params, extract_fields: true)
cloned_template.save!
Templates::CloneAttachments.call(template: cloned_template, original_template: @template,
excluded_attachment_uuids: documents.map(&:uuid))
respond_to do |f|
f.html { redirect_to edit_template_path(cloned_template) }
f.json { render json: { id: cloned_template.id } }
end
rescue Templates::CreateAttachments::PdfEncrypted
2025-05-09 00:01:50 +03:00
respond_to do |f|
f.html { render turbo_stream: turbo_stream.append(params[:form_id], html: helpers.tag.prompt_password) }
f.json { render json: { error: 'PDF encrypted', status: 'pdf_encrypted' }, status: :unprocessable_entity }
end
2025-04-25 21:19:28 +03:00
end
end