Merge pull request #117 from sgmap/pieces_justificatives_improvements

Pieces justificatives improvements
This commit is contained in:
gregoirenovel 2017-04-19 10:30:40 +02:00 committed by GitHub
commit b36252b916
2 changed files with 8 additions and 8 deletions

View file

@ -2,17 +2,17 @@ class PiecesJustificativesService
def self.upload!(dossier, user, params)
tpj_contents = dossier.types_de_piece_justificative
.map { |tpj| [tpj, params["piece_justificative_#{tpj.id}"]] }
.select { |_, content| content }
.select { |_, content| content.present? }
without_virus, with_virus = tpj_contents
.partition { |_, content| ClamavService.safe_file?(content.path) }
errors = with_virus
.map { |_, content| content.original_filename + ': <b>Virus détecté !!</b>' }
.map { |_, content| content.original_filename + ' : virus détecté' }
errors += without_virus
.map { |tpj, content| save_pj(content, dossier, tpj, user) }
.reject(&:empty?)
.compact()
errors += missing_pj_error_messages(dossier)
end
@ -40,7 +40,7 @@ class PiecesJustificativesService
type_de_piece_justificative: tpj,
user: user)
pj.save ? '' : "le fichier #{pj.libelle} n'a pas pu être sauvegardé"
pj.save ? nil : "le fichier #{content.original_filename} (#{pj.libelle}) n'a pas pu être sauvegardé"
end
def self.missing_pj_error_messages(dossier)

View file

@ -31,11 +31,11 @@ describe PiecesJustificativesService do
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'file')
double(path: '', original_filename: 'filename')
}
end
it { expect(errors).to match(["le fichier not mandatory n'a pas pu être sauvegardé"]) }
it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) }
end
context 'when a virus is provided' do
@ -47,7 +47,7 @@ describe PiecesJustificativesService do
}
end
it { expect(errors).to match(['bad_file: <b>Virus détecté !!</b>']) }
it { expect(errors).to match(['bad_file : virus détecté']) }
end
end
@ -63,7 +63,7 @@ describe PiecesJustificativesService do
# we are messing around piece_justificative
# because directly doubling carrierwave params seems complicated
allow(PiecesJustificativesService).to receive(:save_pj).and_return('')
allow(PiecesJustificativesService).to receive(:save_pj).and_return(nil)
piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory)
expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double])
end