refactor(active_storage): PiecesJustificativesService::FakeAttachment -> ActiveStorage::FakeAttachment

This commit is contained in:
Paul Chavard 2022-11-18 11:30:21 +01:00
parent e16a33a316
commit d306583a7b
6 changed files with 32 additions and 36 deletions

View file

@ -10,7 +10,7 @@ class ActiveStorage::DownloadableFile
end
files.filter do |file, _filename|
if file.is_a?(PiecesJustificativesService::FakeAttachment)
if file.is_a?(ActiveStorage::FakeAttachment)
true
else
service = file.blob.service

View file

@ -0,0 +1,24 @@
class ActiveStorage::FakeAttachment < Hashie::Dash
property :filename
property :name
property :file
property :id
property :created_at
property :record_type, default: 'Fake'
def download
file.read
end
def read(*args)
file.read(*args)
end
def close
file.close
end
def attached?
true
end
end

View file

@ -32,7 +32,7 @@ module DownloadManager
attachment_dir = File.dirname(attachment_path)
FileUtils.mkdir_p(attachment_dir) if !Dir.exist?(attachment_dir) # defensive, do not write in undefined dir
if attachment.is_a?(PiecesJustificativesService::FakeAttachment)
if attachment.is_a?(ActiveStorage::FakeAttachment)
File.write(attachment_path, attachment.file.read, mode: 'wb')
else
request = Typhoeus::Request.new(attachment.url)

View file

@ -77,34 +77,6 @@ class PiecesJustificativesService
end
end
class FakeAttachment < Hashie::Dash
property :filename
property :name
property :file
property :id
property :created_at
def download
file.read
end
def read(*args)
file.read(*args)
end
def close
file.close
end
def attached?
true
end
def record_type
'Fake'
end
end
def self.generate_dossier_export(dossiers)
return [] if dossiers.empty?
@ -123,7 +95,7 @@ class PiecesJustificativesService
dossier: dossier
})
a = FakeAttachment.new(
a = ActiveStorage::FakeAttachment.new(
file: StringIO.new(pdf),
filename: "export-#{dossier.id}.pdf",
name: 'pdf_export_for_instructeur',

View file

@ -13,7 +13,7 @@ describe DownloadManager::ParallelDownloadQueue do
let(:destination) { 'lol.png' }
let(:attachment) do
PiecesJustificativesService::FakeAttachment.new(
ActiveStorage::FakeAttachment.new(
file: StringIO.new('coucou'),
filename: "export-dossier.pdf",
name: 'pdf_export_for_instructeur',
@ -22,7 +22,7 @@ describe DownloadManager::ParallelDownloadQueue do
)
end
context 'with a PiecesJustificativesService::FakeAttachment and it works' do
context 'with a ActiveStorage::FakeAttachment and it works' do
it 'write attachment.file to disk' do
target = File.join(download_to_dir, destination)
expect { subject }.to change { File.exist?(target) }
@ -31,7 +31,7 @@ describe DownloadManager::ParallelDownloadQueue do
end
end
context 'with a PiecesJustificativesService::FakeAttachment and it fails' do
context 'with a ActiveStorage::FakeAttachment and it fails' do
it 'write attachment.file to disk' do
expect(attachment.file).to receive(:read).and_raise("boom")
target = File.join(download_to_dir, destination)

View file

@ -65,7 +65,7 @@ describe ProcedureArchiveService do
context 'with a missing file' do
let(:pj) do
PiecesJustificativesService::FakeAttachment.new(
ActiveStorage::FakeAttachment.new(
file: StringIO.new('coucou'),
filename: "export-dossier.pdf",
name: 'pdf_export_for_instructeur',
@ -75,7 +75,7 @@ describe ProcedureArchiveService do
end
let(:bad_pj) do
PiecesJustificativesService::FakeAttachment.new(
ActiveStorage::FakeAttachment.new(
file: nil,
filename: "cni.png",
name: 'cni.png',