diff --git a/app/controllers/backoffice/private_formulaires_controller.rb b/app/controllers/backoffice/private_formulaires_controller.rb
index 81a728085..3c62ad362 100644
--- a/app/controllers/backoffice/private_formulaires_controller.rb
+++ b/app/controllers/backoffice/private_formulaires_controller.rb
@@ -10,7 +10,7 @@ class Backoffice::PrivateFormulairesController < ApplicationController
if champs_service_errors.empty?
flash[:notice] = "Formulaire enregistré"
else
- flash[:alert] = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'
' }).html_safe
+ flash[:alert] = champs_service_errors.join('
').html_safe
end
end
diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb
index a45a402fb..49664c5b6 100644
--- a/app/controllers/users/description_controller.rb
+++ b/app/controllers/users/description_controller.rb
@@ -38,25 +38,16 @@ class Users::DescriptionController < UsersController
params,
check_mandatory_fields
- unless champs_service_errors.empty?
- flash.alert = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'
' }).html_safe
- return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
- end
+ return redirect_to_description_with_errors(@dossier, champs_service_errors) if champs_service_errors.any?
end
if @procedure.cerfa_flag? && params[:cerfa_pdf]
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user)
- unless cerfa.save
- flash.alert = cerfa.errors.full_messages.join('
').html_safe
- return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
- end
+ return redirect_to_description_with_errors(@dossier, cerfa.errors.full_messages) unless cerfa.save
end
errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)
- unless errors_upload.empty?
- flash.alert = errors_upload.html_safe
- return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
- end
+ return redirect_to_description_with_errors(@dossier, errors_upload) if errors_upload.any?
if draft_submission?
flash.notice = 'Votre brouillon a bien été sauvegardé.'
@@ -88,9 +79,9 @@ class Users::DescriptionController < UsersController
if !((errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).empty?)
if flash.alert.nil?
- flash.alert = errors_upload.html_safe
+ flash.alert = errors_upload.join('
').html_safe
else
- flash.alert = (flash.alert + '
' + errors_upload.html_safe).html_safe
+ flash.alert = (flash.alert + '
' + errors_upload.join('
').html_safe).html_safe
end
else
@@ -111,6 +102,11 @@ class Users::DescriptionController < UsersController
private
+ def redirect_to_description_with_errors(dossier, errors)
+ flash.alert = errors.join('
').html_safe
+ redirect_to users_dossier_description_path(dossier_id: dossier.id)
+ end
+
def draft_submission?
params[:submit] && params[:submit].keys.first == 'brouillon'
end
diff --git a/app/services/champs_service.rb b/app/services/champs_service.rb
index 31b53973f..b35ec4d7b 100644
--- a/app/services/champs_service.rb
+++ b/app/services/champs_service.rb
@@ -31,11 +31,7 @@ class ChampsService
def build_error_messages(champs)
champs.select(&:mandatory_and_blank?)
- .map { |c| build_champ_error_message(c) }
- end
-
- def build_champ_error_message(champ)
- { message: "Le champ #{champ.libelle} doit être rempli." }
+ .map { |c| "Le champ #{c.libelle} doit être rempli." }
end
end
end
diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb
index 8dc309df8..6b10ecbeb 100644
--- a/app/services/pieces_justificatives_service.rb
+++ b/app/services/pieces_justificatives_service.rb
@@ -8,14 +8,13 @@ class PiecesJustificativesService
.partition { |_, content| ClamavService.safe_file?(content.path) }
errors = with_virus
- .map { |_, content| content.original_filename + ': Virus détecté !!
' }
+ .map { |_, content| content.original_filename + ': Virus détecté !!' }
errors += without_virus
.map { |tpj, content| save_pj(content, dossier, tpj, user) }
+ .reject(&:empty?)
errors += missing_pj_error_messages(dossier)
-
- errors.join
end
def self.upload_one! dossier, user, params
@@ -41,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 ? '' : "le fichier #{pj.libelle} n'a pas pu être sauvegardé"
end
def self.missing_pj_error_messages(dossier)
@@ -49,6 +48,6 @@ class PiecesJustificativesService
present_pjs = dossier.pieces_justificatives.map(&:type_de_piece_justificative)
missing_pjs = mandatory_pjs - present_pjs
- missing_pjs.map { |pj| "La pièce jointe #{pj.libelle} doit être fournie.
" }
+ missing_pjs.map { |pj| "La pièce jointe #{pj.libelle} doit être fournie." }
end
end
diff --git a/spec/services/champs_service_spec.rb b/spec/services/champs_service_spec.rb
index 4e8ce53a1..ee0047ea2 100644
--- a/spec/services/champs_service_spec.rb
+++ b/spec/services/champs_service_spec.rb
@@ -34,7 +34,7 @@ describe ChampsService do
end
it 'adds error for the missing mandatory champ' do
- expect(@errors).to match([{ message: 'Le champ mandatory doit être rempli.' }])
+ expect(@errors).to match(['Le champ mandatory doit être rempli.'])
end
end
diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb
index 8f10f4eb8..922befc4e 100644
--- a/spec/services/pieces_justificatives_service_spec.rb
+++ b/spec/services/pieces_justificatives_service_spec.rb
@@ -24,7 +24,7 @@ describe PiecesJustificativesService do
let(:tpjs) { [tpj_not_mandatory] }
context 'when no params are given' do
- it { expect(errors).to eq('') }
+ it { expect(errors).to eq([]) }
end
context 'when sometihing wrong with file save' do
@@ -35,7 +35,7 @@ describe PiecesJustificativesService do
}
end
- it { expect(errors).to eq("le fichier not mandatory n'a pas pu être sauvegardé
") }
+ it { expect(errors).to match(["le fichier 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 eq('bad_file: Virus détecté !!
') }
+ it { expect(errors).to match(['bad_file: Virus détecté !!']) }
end
end
@@ -55,7 +55,7 @@ describe PiecesJustificativesService do
let(:tpjs) { [tpj_mandatory] }
context 'when no params are given' do
- it { expect(errors).to eq('La pièce jointe justificatif doit être fournie.
') }
+ it { expect(errors).to match(['La pièce jointe justificatif doit être fournie.']) }
end
context 'when the piece justificative is provided' do
@@ -74,7 +74,7 @@ describe PiecesJustificativesService do
}
end
- it { expect(errors).to eq('') }
+ it { expect(errors).to match([]) }
end
end
end