Refactor clone_attachments

This commit is contained in:
Paul Chavard 2021-07-01 13:33:22 +02:00
parent d6be37fb40
commit ce27999bc3
4 changed files with 30 additions and 44 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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