# frozen_string_literal: true class TemplateReplaceDocumentsController < ApplicationController load_and_authorize_resource :template def create if params[:blobs].blank? && params[:files].blank? return respond_to do |f| f.html { redirect_back fallback_location: template_path(@template), alert: I18n.t('file_is_missing') } f.json { render json: { error: I18n.t('file_is_missing') }, status: :unprocessable_entity } end end ActiveRecord::Associations::Preloader.new( records: [@template], associations: [schema_documents: :preview_images_attachments] ).call cloned_template = Templates::Clone.call(@template, author: current_user) 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 render json: { error: 'PDF encrypted', status: 'pdf_encrypted' }, status: :unprocessable_entity end end