refactor clone
This commit is contained in:
@@ -7,6 +7,11 @@ module Api
|
||||
def create
|
||||
authorize!(:manage, @template)
|
||||
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [@template],
|
||||
associations: [schema_documents: :preview_images_attachments]
|
||||
).call
|
||||
|
||||
cloned_template = Templates::Clone.call(
|
||||
@template,
|
||||
author: current_user,
|
||||
@@ -18,11 +23,11 @@ module Api
|
||||
cloned_template.source = :api
|
||||
cloned_template.save!
|
||||
|
||||
Templates::CloneAttachments.call(template: cloned_template, original_template: @template)
|
||||
schema_documents = Templates::CloneAttachments.call(template: cloned_template, original_template: @template)
|
||||
|
||||
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => cloned_template.id)
|
||||
|
||||
render json: Templates::SerializeForApi.call(cloned_template)
|
||||
render json: Templates::SerializeForApi.call(cloned_template, schema_documents)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,7 +27,7 @@ class TemplatesController < ApplicationController
|
||||
def edit
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [@template],
|
||||
associations: [schema_documents: { preview_images_attachments: :blob }]
|
||||
associations: [schema_documents: [:blob, { preview_images_attachments: :blob }]]
|
||||
).call
|
||||
|
||||
@template_data =
|
||||
@@ -43,6 +43,11 @@ class TemplatesController < ApplicationController
|
||||
|
||||
def create
|
||||
if @base_template
|
||||
ActiveRecord::Associations::Preloader.new(
|
||||
records: [@base_template],
|
||||
associations: [schema_documents: :preview_images_attachments]
|
||||
).call
|
||||
|
||||
@template = Templates::Clone.call(@base_template, author: current_user,
|
||||
name: params.dig(:template, :name),
|
||||
folder_name: params[:folder_name])
|
||||
|
||||
@@ -4,7 +4,6 @@ module Templates
|
||||
module Clone
|
||||
module_function
|
||||
|
||||
# rubocop:disable Metrics
|
||||
def call(original_template, author:, external_id: nil, name: nil, folder_name: nil)
|
||||
template = original_template.account.templates.new
|
||||
|
||||
@@ -17,6 +16,12 @@ module Templates
|
||||
|
||||
template.folder = TemplateFolders.find_or_create_by_name(author, folder_name) if folder_name.present?
|
||||
|
||||
template.submitters, template.fields = clone_submitters_and_fields(original_template)
|
||||
|
||||
template
|
||||
end
|
||||
|
||||
def clone_submitters_and_fields(original_template)
|
||||
submitter_uuids_replacements = {}
|
||||
field_uuids_replacements = {}
|
||||
|
||||
@@ -52,10 +57,7 @@ module Templates
|
||||
end
|
||||
end
|
||||
|
||||
template.assign_attributes(fields: cloned_fields, submitters: cloned_submitters)
|
||||
|
||||
template
|
||||
[cloned_submitters, cloned_fields]
|
||||
end
|
||||
# rubocop:enable Metrics
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,8 +7,8 @@ module Templates
|
||||
def call(template:, original_template:)
|
||||
schema_uuids_replacements = {}
|
||||
|
||||
cloned_schema = original_template['schema'].deep_dup
|
||||
cloned_fields = template['fields'].deep_dup
|
||||
cloned_schema = original_template.schema.deep_dup
|
||||
cloned_fields = template.fields.deep_dup
|
||||
|
||||
cloned_schema.each do |schema_item|
|
||||
new_schema_item_uuid = SecureRandom.uuid
|
||||
@@ -27,26 +27,25 @@ module Templates
|
||||
|
||||
template.update!(schema: cloned_schema, fields: cloned_fields)
|
||||
|
||||
original_template.schema_documents.preload(:preview_images_attachments).each do |document|
|
||||
new_document = ActiveStorage::Attachment.create!(
|
||||
uuid: schema_uuids_replacements[document.uuid],
|
||||
blob_id: document.blob_id,
|
||||
name: 'documents',
|
||||
record: template
|
||||
)
|
||||
original_template.schema_documents.map do |document|
|
||||
new_document =
|
||||
ApplicationRecord.no_touching do
|
||||
template.documents_attachments.create!(
|
||||
uuid: schema_uuids_replacements[document.uuid],
|
||||
blob_id: document.blob_id
|
||||
)
|
||||
end
|
||||
|
||||
clone_document_preview_images_attachments(document:, new_document:)
|
||||
|
||||
new_document
|
||||
end
|
||||
end
|
||||
|
||||
def clone_document_preview_images_attachments(document:, new_document:)
|
||||
ApplicationRecord.no_touching do
|
||||
document.preview_images_attachments.each do |preview_image|
|
||||
ActiveStorage::Attachment.create!(
|
||||
blob_id: preview_image.blob_id,
|
||||
name: 'preview_images',
|
||||
record: new_document
|
||||
)
|
||||
new_document.preview_images_attachments.create!(blob_id: preview_image.blob_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user