mirror of https://github.com/docusealco/docuseal
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							37 lines
						
					
					
						
							1.3 KiB
						
					
					
				
			
		
		
	
	
							37 lines
						
					
					
						
							1.3 KiB
						
					
					
				| # 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
 |