Merge pull request #116 from sgmap/description_controller_refactor

Description controller refactor
This commit is contained in:
LeSim 2017-04-19 10:57:03 +02:00 committed by GitHub
commit 42866fdcde
2 changed files with 17 additions and 22 deletions

View file

@ -24,41 +24,36 @@ class Users::DescriptionController < UsersController
end end
def update def update
@dossier = current_user_dossier dossier = current_user_dossier
@procedure = @dossier.procedure procedure = dossier.procedure
return head :forbidden unless @dossier.can_be_initiated?
@champs = @dossier.ordered_champs
return head :forbidden unless dossier.can_be_initiated?
check_mandatory_fields = !draft_submission? check_mandatory_fields = !draft_submission?
if params[:champs] if params[:champs]
champs_service_errors = ChampsService.save_champs @dossier.champs, champs_service_errors =
params, ChampsService.save_champs(dossier.champs, params, check_mandatory_fields)
check_mandatory_fields return redirect_to_description_with_errors(dossier, champs_service_errors) if champs_service_errors.any?
return redirect_to_description_with_errors(@dossier, champs_service_errors) if champs_service_errors.any?
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)
return redirect_to_description_with_errors(@dossier, cerfa.errors.full_messages) unless cerfa.save return redirect_to_description_with_errors(dossier, cerfa.errors.full_messages) unless cerfa.save
end end
errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params) errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params)
return redirect_to_description_with_errors(@dossier, errors_upload) if errors_upload.any? return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any?
if draft_submission? if draft_submission?
flash.notice = 'Votre brouillon a bien été sauvegardé.' flash.notice = 'Votre brouillon a bien été sauvegardé.'
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon) redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)
else else
if @dossier.draft? if dossier.draft?
@dossier.initiated! dossier.initiated!
NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail).deliver_now! NotificationMailer.send_notification(dossier, procedure.initiated_mail).deliver_now!
end end
flash.notice = 'Félicitations, votre demande a bien été enregistrée.' flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id) redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: dossier.id)
end end
end end
@ -103,7 +98,7 @@ class Users::DescriptionController < UsersController
private private
def redirect_to_description_with_errors(dossier, errors) def redirect_to_description_with_errors(dossier, errors)
flash.alert = errors.join('<br>').html_safe flash.alert = errors.join('<br>')
redirect_to users_dossier_description_path(dossier_id: dossier.id) redirect_to users_dossier_description_path(dossier_id: dossier.id)
end end

View file

@ -4,4 +4,4 @@
= flash.notice = flash.notice
- if flash.alert - if flash.alert
.alert.alert-danger .alert.alert-danger
= flash.alert = flash.alert.html_safe