From ce27999bc3cc68e7e42cf0c7f11529a96a27b389 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 1 Jul 2021 13:33:22 +0200 Subject: [PATCH] Refactor clone_attachments --- app/models/procedure.rb | 27 +++---------------- app/models/procedure_revision.rb | 4 ++- app/models/type_de_champ.rb | 19 ------------- app/services/pieces_justificatives_service.rb | 24 +++++++++++++++++ 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 7601f8269..e02100f4b 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -424,7 +424,9 @@ class Procedure < ApplicationRecord } } include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin - procedure = self.deep_clone(include: include_list, &method(:clone_attachments)) + procedure = self.deep_clone(include: include_list) do |original, kopy| + PiecesJustificativesService.clone_attachments(original, kopy) + end procedure.path = SecureRandom.uuid procedure.aasm_state = :brouillon procedure.closed_at = nil @@ -470,29 +472,6 @@ class Procedure < ApplicationRecord procedure end - def clone_attachments(original, kopy) - if original.is_a?(TypeDeChamp) - clone_attachment(:piece_justificative_template, original, kopy) - elsif original.is_a?(Procedure) - clone_attachment(:logo, original, kopy) - clone_attachment(:notice, original, kopy) - clone_attachment(:deliberation, original, kopy) - end - end - - def clone_attachment(attribute, original, kopy) - original_attachment = original.send(attribute) - if original_attachment.attached? - kopy.send(attribute).attach({ - io: StringIO.new(original_attachment.download), - filename: original_attachment.filename, - content_type: original_attachment.content_type, - # we don't want to run virus scanner on cloned file - metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } - }) - end - end - def whitelisted? whitelisted_at.present? end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 13c60c8d4..75513d724 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -249,7 +249,9 @@ class ProcedureRevision < ApplicationRecord def revise_type_de_champ(type_de_champ) types_de_champ_association = type_de_champ.private? ? :revision_types_de_champ_private : :revision_types_de_champ association = send(types_de_champ_association).find_by!(type_de_champ: type_de_champ) - cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ], &type_de_champ.method(:clone_attachments)) + cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy| + PiecesJustificativesService.clone_attachments(original, kopy) + end cloned_type_de_champ.revision = self association.update!(type_de_champ: cloned_type_de_champ) cloned_type_de_champ diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 8874766fc..999d47f23 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -369,23 +369,4 @@ class TypeDeChamp < ApplicationRecord types_de_champ.destroy_all end end - - def clone_attachments(original, kopy) - if original.is_a?(TypeDeChamp) - clone_attachment(:piece_justificative_template, original, kopy) - end - end - - def clone_attachment(attribute, original, kopy) - original_attachment = original.send(attribute) - if original_attachment.attached? - kopy.send(attribute).attach({ - io: StringIO.new(original_attachment.download), - filename: original_attachment.filename, - content_type: original_attachment.content_type, - # we don't want to run virus scanner on cloned file - metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } - }) - end - end end diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index c70ace4dc..974e774b8 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -49,6 +49,30 @@ class PiecesJustificativesService end end + def self.clone_attachments(original, kopy) + if original.is_a?(TypeDeChamp) + clone_attachment(original.piece_justificative_template, kopy.piece_justificative_template) + elsif original.is_a?(Procedure) + clone_attachment(original.logo, kopy.logo) + clone_attachment(original.notice, kopy.notice) + clone_attachment(original.deliberation, kopy.deliberation) + end + end + + def self.clone_attachment(original_attachment, copy_attachment) + if original_attachment.attached? + original_attachment.open do |tempfile| + copy_attachment.attach({ + io: File.open(tempfile.path), + filename: original_attachment.filename, + content_type: original_attachment.content_type, + # we don't want to run virus scanner on cloned file + metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE } + }) + end + end + end + class FakeAttachment < Hashie::Dash property :filename property :name