demarches-normaliennes/app/controllers/users/description_controller.rb

92 lines
2.6 KiB
Ruby
Raw Normal View History

class Users::DescriptionController < UsersController
2016-01-26 15:52:05 +01:00
before_action only: [:show] do
authorized_routes? self.class
end
2015-08-10 11:05:06 +02:00
def show
@dossier = current_user_dossier.decorate
@procedure = @dossier.procedure
@champs = @dossier.ordered_champs
2015-08-18 10:43:36 +02:00
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to url_for(root_path)
2015-08-10 11:05:06 +02:00
end
def error
show
flash.now.alert = 'Un ou plusieurs attributs obligatoires sont manquants ou incorrects.'
render 'show'
end
def create
@dossier = current_user_dossier
2016-02-02 18:37:38 +01:00
@procedure = @dossier.procedure
@champs = @dossier.ordered_champs
unless @dossier.update_attributes(create_params)
2015-08-21 11:37:13 +02:00
@dossier = @dossier.decorate
flash.now.alert = @dossier.errors.full_messages.join('<br />').html_safe
return render 'show'
end
2016-02-04 14:08:35 +01:00
2016-02-02 18:37:38 +01:00
if @procedure.cerfa_flag?
unless params[:cerfa_pdf].nil?
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier)
2016-02-18 15:46:59 +01:00
unless cerfa.save
flash.now.alert = cerfa.errors.full_messages.join('<br />').html_safe
return render 'show'
end
2016-02-02 18:37:38 +01:00
end
end
2015-08-10 11:05:06 +02:00
unless params[:champs].nil?
@dossier.champs.each do |champ|
champ.value = params[:champs]["'#{champ.id}'"]
if champ.mandatory? && (champ.value.nil? || champ.value.blank?)
flash.now.alert = "Le champ #{champ.libelle} doit être rempli."
return render 'show'
end
champ.save
end
end
2016-03-17 14:50:10 +01:00
@dossier.types_de_piece_justificative.each do |type_de_pieces_justificatives|
unless params["piece_justificative_#{type_de_pieces_justificatives.id}"].nil?
piece_justificative = PieceJustificative.new(content: params["piece_justificative_#{type_de_pieces_justificatives.id}"],
dossier: @dossier,
type_de_piece_justificative: type_de_pieces_justificatives)
2016-02-18 15:46:59 +01:00
unless piece_justificative.save
flash.now.alert = piece_justificative.errors.full_messages.join('<br />').html_safe
return render 'show'
end
end
end
2015-08-10 11:05:06 +02:00
if @dossier.draft?
@dossier.initiated!
end
2015-08-10 11:05:06 +02:00
flash.notice = 'Félicitation, votre demande a bien été enregistrée.'
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id)
2015-08-10 11:05:06 +02:00
end
2016-01-26 15:52:05 +01:00
def self.route_authorization
{
states: [:draft, :initiated, :replied, :updated]
}
end
2015-08-10 11:05:06 +02:00
private
def create_params
params.permit(:nom_projet, :description)
end
2015-08-10 11:05:06 +02:00
end