Merge pull request #6314 from tchak/clone-attachments
Refactor clone_attachments
This commit is contained in:
commit
744b140c4c
4 changed files with 30 additions and 44 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue