generate unique uuids for cloned templates
This commit is contained in:
committed by
Pete Matsyburka
parent
8898dcc054
commit
baaf1f510f
@@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Templates
|
||||
module Clone
|
||||
module_function
|
||||
|
||||
def call(original_template, author:, application_key: nil, name: nil, folder_name: nil)
|
||||
original_template_account = original_template.account
|
||||
|
||||
template = original_template_account.templates.new
|
||||
|
||||
template.application_key = application_key
|
||||
template.author = author
|
||||
template.name = name || "#{original_template.name} (Clone)"
|
||||
|
||||
template.assign_attributes(original_template.slice(:folder_id, :schema))
|
||||
|
||||
template.folder = TemplateFolders.find_or_create_by_name(author, folder_name) if folder_name.present?
|
||||
|
||||
submitter_uuids_replacements = {}
|
||||
|
||||
cloned_submitters = original_template['submitters'].deep_dup
|
||||
cloned_fields = original_template['fields'].deep_dup
|
||||
|
||||
cloned_submitters.each do |submitter|
|
||||
new_submitter_uuid = SecureRandom.uuid
|
||||
|
||||
submitter_uuids_replacements[submitter['uuid']] = new_submitter_uuid
|
||||
submitter['uuid'] = new_submitter_uuid
|
||||
end
|
||||
|
||||
cloned_fields.each do |field|
|
||||
field['uuid'] = SecureRandom.uuid
|
||||
field['submitter_uuid'] = submitter_uuids_replacements[field['submitter_uuid']]
|
||||
end
|
||||
|
||||
template.assign_attributes(fields: cloned_fields, submitters: cloned_submitters)
|
||||
|
||||
template
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,23 +5,48 @@ module Templates
|
||||
module_function
|
||||
|
||||
def call(template:, original_template:)
|
||||
schema_uuids_replacements = {}
|
||||
|
||||
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
|
||||
|
||||
schema_uuids_replacements[schema_item['attachment_uuid']] = new_schema_item_uuid
|
||||
schema_item['attachment_uuid'] = new_schema_item_uuid
|
||||
end
|
||||
|
||||
cloned_fields.each do |field|
|
||||
next if field['areas'].blank?
|
||||
|
||||
field['areas'].each do |area|
|
||||
area['attachment_uuid'] = schema_uuids_replacements[area['attachment_uuid']]
|
||||
end
|
||||
end
|
||||
|
||||
template.update!(schema: cloned_schema, fields: cloned_fields)
|
||||
|
||||
original_template.documents.preload(:preview_images_attachments).each do |document|
|
||||
new_document = ActiveStorage::Attachment.create!(
|
||||
uuid: document.uuid,
|
||||
uuid: schema_uuids_replacements[document.uuid],
|
||||
blob_id: document.blob_id,
|
||||
name: 'documents',
|
||||
record: template
|
||||
)
|
||||
|
||||
ApplicationRecord.no_touching do
|
||||
document.preview_images_attachments.each do |preview_image|
|
||||
ActiveStorage::Attachment.create!(
|
||||
uuid: preview_image.uuid,
|
||||
blob_id: preview_image.blob_id,
|
||||
name: 'preview_images',
|
||||
record: new_document
|
||||
)
|
||||
end
|
||||
clone_document_preview_images_attachments(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
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user