fix template clone
This commit is contained in:
+22
-4
@@ -4,10 +4,9 @@ module Templates
|
||||
module Clone
|
||||
module_function
|
||||
|
||||
# rubocop:disable Metrics
|
||||
def call(original_template, author:, external_id: nil, name: nil, folder_name: nil)
|
||||
original_template_account = original_template.account
|
||||
|
||||
template = original_template_account.templates.new
|
||||
template = original_template.account.templates.new
|
||||
|
||||
template.external_id = external_id
|
||||
template.author = author
|
||||
@@ -19,6 +18,7 @@ module Templates
|
||||
template.folder = TemplateFolders.find_or_create_by_name(author, folder_name) if folder_name.present?
|
||||
|
||||
submitter_uuids_replacements = {}
|
||||
field_uuids_replacements = {}
|
||||
|
||||
cloned_submitters = original_template['submitters'].deep_dup
|
||||
cloned_fields = original_template['fields'].deep_dup
|
||||
@@ -31,13 +31,31 @@ module Templates
|
||||
end
|
||||
|
||||
cloned_fields.each do |field|
|
||||
field['uuid'] = SecureRandom.uuid
|
||||
new_field_uuid = SecureRandom.uuid
|
||||
|
||||
field_uuids_replacements[field['uuid']] = new_field_uuid
|
||||
field['uuid'] = new_field_uuid
|
||||
|
||||
field['submitter_uuid'] = submitter_uuids_replacements[field['submitter_uuid']]
|
||||
end
|
||||
|
||||
replace_fields_regexp = Regexp.union(field_uuids_replacements.keys)
|
||||
|
||||
cloned_fields.each do |field|
|
||||
Array.wrap(field['conditions']).each do |condition|
|
||||
condition['field_uuid'] = field_uuids_replacements[condition['field_uuid']]
|
||||
end
|
||||
|
||||
if field.dig('preferences', 'formula').present?
|
||||
field['preferences']['formula'] =
|
||||
field['preferences']['formula'].gsub(replace_fields_regexp, field_uuids_replacements)
|
||||
end
|
||||
end
|
||||
|
||||
template.assign_attributes(fields: cloned_fields, submitters: cloned_submitters)
|
||||
|
||||
template
|
||||
end
|
||||
# rubocop:enable Metrics
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user