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