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
|
||||
Reference in New Issue
Block a user