Files
docuseal/lib/templates/clone_attachments.rb
T

62 lines
1.8 KiB
Ruby
Raw Normal View History

2023-06-18 16:26:51 +00:00
# frozen_string_literal: true
module Templates
module CloneAttachments
module_function
2025-04-25 21:19:28 +03:00
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [])
2023-12-18 13:38:01 +02:00
schema_uuids_replacements = {}
2024-12-10 22:22:17 +02:00
template.schema.each_with_index do |schema_item, index|
2025-04-25 21:19:28 +03:00
next if excluded_attachment_uuids.include?(schema_item['attachment_uuid'])
2023-12-18 13:38:01 +02:00
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
2024-12-10 17:41:27 +02:00
new_name = documents&.dig(index, 'name')
schema_item['name'] = new_name if new_name.present?
2023-12-18 13:38:01 +02:00
end
2024-12-10 22:22:17 +02:00
template.fields.each do |field|
2023-12-18 13:38:01 +02:00
next if field['areas'].blank?
field['areas'].each do |area|
2025-04-25 21:19:28 +03:00
new_attachment_uuid = schema_uuids_replacements[area['attachment_uuid']]
area['attachment_uuid'] = new_attachment_uuid if new_attachment_uuid
2023-12-18 13:38:01 +02:00
end
end
2024-12-10 22:22:17 +02:00
template.save!
2023-12-18 13:38:01 +02:00
2025-04-25 21:19:28 +03:00
original_template.schema_documents.filter_map do |document|
new_attachment_uuid = schema_uuids_replacements[document.uuid]
next unless new_attachment_uuid
2024-07-16 01:40:38 +03:00
new_document =
ApplicationRecord.no_touching do
template.documents_attachments.create!(
2025-04-25 21:19:28 +03:00
uuid: new_attachment_uuid,
2024-07-16 01:40:38 +03:00
blob_id: document.blob_id
)
end
2023-06-18 16:26:51 +00:00
2023-12-18 13:38:01 +02:00
clone_document_preview_images_attachments(document:, new_document:)
2024-07-16 01:40:38 +03:00
new_document
2023-12-18 13:38:01 +02:00
end
end
def clone_document_preview_images_attachments(document:, new_document:)
ApplicationRecord.no_touching do
document.preview_images_attachments.each do |preview_image|
2024-07-16 01:40:38 +03:00
new_document.preview_images_attachments.create!(blob_id: preview_image.blob_id)
2023-06-18 16:26:51 +00:00
end
end
end
end
end