Files

43 lines
1.5 KiB
Ruby
Raw Permalink 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-08-14 09:25:35 +03:00
return head :unprocessable_content if params[:files].blank?
2025-04-25 21:19:28 +03:00
ActiveRecord::Associations::Preloader.new(
records: [@template],
2026-04-02 12:48:28 +03:00
associations: [{ schema_documents: :preview_images_attachments }]
2025-04-25 21:19:28 +03:00
).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, '.*')
2026-05-15 18:55:03 +03:00
authorize!(:create, cloned_template)
2025-04-25 21:19:28 +03:00
cloned_template.save!
documents = Templates::ReplaceAttachments.call(cloned_template, params, extract_fields: true)
2025-08-26 12:29:51 +03:00
Templates.maybe_assign_access(cloned_template)
2025-04-25 21:19:28 +03:00
cloned_template.save!
Templates::CloneAttachments.call(template: cloned_template, original_template: @template,
excluded_attachment_uuids: documents.map(&:uuid))
2025-06-05 13:16:55 +03:00
SearchEntries.enqueue_reindex(cloned_template)
2025-04-25 21:19:28 +03:00
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) }
2025-08-14 09:25:35 +03:00
f.json { render json: { error: 'PDF encrypted', status: 'pdf_encrypted' }, status: :unprocessable_content }
2025-05-09 00:01:50 +03:00
end
2025-04-25 21:19:28 +03:00
end
end