DescriptionController: use common system to redirect with errors

This commit is contained in:
Simon Lehericey 2017-03-30 09:50:30 +02:00
parent 591ed2037e
commit 367b7c6ae1
6 changed files with 22 additions and 31 deletions

View file

@ -10,7 +10,7 @@ class Backoffice::PrivateFormulairesController < ApplicationController
if champs_service_errors.empty? if champs_service_errors.empty?
flash[:notice] = "Formulaire enregistré" flash[:notice] = "Formulaire enregistré"
else else
flash[:alert] = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'<br>' }).html_safe flash[:alert] = champs_service_errors.join('<br>').html_safe
end end
end end

View file

@ -38,25 +38,16 @@ class Users::DescriptionController < UsersController
params, params,
check_mandatory_fields check_mandatory_fields
unless champs_service_errors.empty? return redirect_to_description_with_errors(@dossier, champs_service_errors) if champs_service_errors.any?
flash.alert = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'<br>' }).html_safe
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
end
end end
if @procedure.cerfa_flag? && params[:cerfa_pdf] if @procedure.cerfa_flag? && params[:cerfa_pdf]
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user) cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user)
unless cerfa.save return redirect_to_description_with_errors(@dossier, cerfa.errors.full_messages) unless cerfa.save
flash.alert = cerfa.errors.full_messages.join('<br />').html_safe
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
end
end end
errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params) errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)
unless errors_upload.empty? return redirect_to_description_with_errors(@dossier, errors_upload) if errors_upload.any?
flash.alert = errors_upload.html_safe
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
end
if draft_submission? if draft_submission?
flash.notice = 'Votre brouillon a bien été sauvegardé.' 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 !((errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).empty?)
if flash.alert.nil? if flash.alert.nil?
flash.alert = errors_upload.html_safe flash.alert = errors_upload.join('<br>').html_safe
else else
flash.alert = (flash.alert + '<br />' + errors_upload.html_safe).html_safe flash.alert = (flash.alert + '<br />' + errors_upload.join('<br>').html_safe).html_safe
end end
else else
@ -111,6 +102,11 @@ class Users::DescriptionController < UsersController
private private
def redirect_to_description_with_errors(dossier, errors)
flash.alert = errors.join('<br>').html_safe
redirect_to users_dossier_description_path(dossier_id: dossier.id)
end
def draft_submission? def draft_submission?
params[:submit] && params[:submit].keys.first == 'brouillon' params[:submit] && params[:submit].keys.first == 'brouillon'
end end

View file

@ -31,11 +31,7 @@ class ChampsService
def build_error_messages(champs) def build_error_messages(champs)
champs.select(&:mandatory_and_blank?) champs.select(&:mandatory_and_blank?)
.map { |c| build_champ_error_message(c) } .map { |c| "Le champ #{c.libelle} doit être rempli." }
end
def build_champ_error_message(champ)
{ message: "Le champ #{champ.libelle} doit être rempli." }
end end
end end
end end

View file

@ -8,14 +8,13 @@ class PiecesJustificativesService
.partition { |_, content| ClamavService.safe_file?(content.path) } .partition { |_, content| ClamavService.safe_file?(content.path) }
errors = with_virus errors = with_virus
.map { |_, content| content.original_filename + ': <b>Virus détecté !!</b><br>' } .map { |_, content| content.original_filename + ': <b>Virus détecté !!</b>' }
errors += without_virus errors += without_virus
.map { |tpj, content| save_pj(content, dossier, tpj, user) } .map { |tpj, content| save_pj(content, dossier, tpj, user) }
.reject(&:empty?)
errors += missing_pj_error_messages(dossier) errors += missing_pj_error_messages(dossier)
errors.join
end end
def self.upload_one! dossier, user, params def self.upload_one! dossier, user, params
@ -41,7 +40,7 @@ class PiecesJustificativesService
type_de_piece_justificative: tpj, type_de_piece_justificative: tpj,
user: user) user: user)
pj.save ? '' : "le fichier #{pj.libelle} n'a pas pu être sauvegardé<br>" pj.save ? '' : "le fichier #{pj.libelle} n'a pas pu être sauvegardé"
end end
def self.missing_pj_error_messages(dossier) def self.missing_pj_error_messages(dossier)
@ -49,6 +48,6 @@ class PiecesJustificativesService
present_pjs = dossier.pieces_justificatives.map(&:type_de_piece_justificative) present_pjs = dossier.pieces_justificatives.map(&:type_de_piece_justificative)
missing_pjs = mandatory_pjs - present_pjs missing_pjs = mandatory_pjs - present_pjs
missing_pjs.map { |pj| "La pièce jointe #{pj.libelle} doit être fournie.<br>" } missing_pjs.map { |pj| "La pièce jointe #{pj.libelle} doit être fournie." }
end end
end end

View file

@ -34,7 +34,7 @@ describe ChampsService do
end end
it 'adds error for the missing mandatory champ' do 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
end end

View file

@ -24,7 +24,7 @@ describe PiecesJustificativesService do
let(:tpjs) { [tpj_not_mandatory] } let(:tpjs) { [tpj_not_mandatory] }
context 'when no params are given' do context 'when no params are given' do
it { expect(errors).to eq('') } it { expect(errors).to eq([]) }
end end
context 'when sometihing wrong with file save' do context 'when sometihing wrong with file save' do
@ -35,7 +35,7 @@ describe PiecesJustificativesService do
} }
end end
it { expect(errors).to eq("le fichier not mandatory n'a pas pu être sauvegardé<br>") } it { expect(errors).to match(["le fichier not mandatory n'a pas pu être sauvegardé"]) }
end end
context 'when a virus is provided' do context 'when a virus is provided' do
@ -47,7 +47,7 @@ describe PiecesJustificativesService do
} }
end end
it { expect(errors).to eq('bad_file: <b>Virus détecté !!</b><br>') } it { expect(errors).to match(['bad_file: <b>Virus détecté !!</b>']) }
end end
end end
@ -55,7 +55,7 @@ describe PiecesJustificativesService do
let(:tpjs) { [tpj_mandatory] } let(:tpjs) { [tpj_mandatory] }
context 'when no params are given' do context 'when no params are given' do
it { expect(errors).to eq('La pièce jointe justificatif doit être fournie.<br>') } it { expect(errors).to match(['La pièce jointe justificatif doit être fournie.']) }
end end
context 'when the piece justificative is provided' do context 'when the piece justificative is provided' do
@ -74,7 +74,7 @@ describe PiecesJustificativesService do
} }
end end
it { expect(errors).to eq('') } it { expect(errors).to match([]) }
end end
end end
end end