diff --git a/lib/templates/clone.rb b/lib/templates/clone.rb index b4b68231..60739c2e 100644 --- a/lib/templates/clone.rb +++ b/lib/templates/clone.rb @@ -12,7 +12,13 @@ module Templates template.preferences = original_template.preferences.deep_dup template.name = name.presence || "#{original_template.name} (#{I18n.t('clone')})" - template.assign_attributes(original_template.slice(:folder_id, :schema)) + template.assign_attributes(original_template.slice(:folder_id, :schema).deep_dup) + + if name.present? && template.schema.size == 1 && + original_template.schema.first['name'] == original_template.name && + template.name != "#{original_template.name} (#{I18n.t('clone')})" + template.schema.first['name'] = template.name + end template.folder = TemplateFolders.find_or_create_by_name(author, folder_name) if folder_name.present? diff --git a/lib/templates/clone_attachments.rb b/lib/templates/clone_attachments.rb index 0b52a2fa..8b08735d 100644 --- a/lib/templates/clone_attachments.rb +++ b/lib/templates/clone_attachments.rb @@ -7,10 +7,7 @@ module Templates def call(template:, original_template:, documents: []) schema_uuids_replacements = {} - cloned_schema = original_template.schema.deep_dup - cloned_fields = template.fields.deep_dup - - cloned_schema.each_with_index do |schema_item, index| + template.schema.each_with_index do |schema_item, index| new_schema_item_uuid = SecureRandom.uuid schema_uuids_replacements[schema_item['attachment_uuid']] = new_schema_item_uuid @@ -21,7 +18,7 @@ module Templates schema_item['name'] = new_name if new_name.present? end - cloned_fields.each do |field| + template.fields.each do |field| next if field['areas'].blank? field['areas'].each do |area| @@ -29,7 +26,7 @@ module Templates end end - template.update!(schema: cloned_schema, fields: cloned_fields) + template.save! original_template.schema_documents.map do |document| new_document =