2015-08-10 11:05:06 +02:00
|
|
|
class DescriptionController < ApplicationController
|
|
|
|
def show
|
|
|
|
@dossier = Dossier.find(params[:dossier_id])
|
|
|
|
@dossier = @dossier.decorate
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2015-08-17 11:42:41 +02:00
|
|
|
@formulaire = @dossier.formulaire
|
2015-08-20 12:20:54 +02:00
|
|
|
|
2015-08-18 10:43:36 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
redirect_to url_for(controller: :start, action: :error_dossier)
|
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 = Dossier.find(params[:dossier_id])
|
2015-08-11 15:39:16 +02:00
|
|
|
@dossier.update_attributes(create_params)
|
2015-08-20 16:37:03 +02:00
|
|
|
unless params[:cerfa_pdf].nil?
|
2015-08-20 15:15:20 +02:00
|
|
|
cerfa = @dossier.cerfa
|
|
|
|
cerfa.content = params[:cerfa_pdf]
|
|
|
|
cerfa.save
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-08-20 15:15:20 +02:00
|
|
|
@dossier.pieces_jointes.each do |piece_jointe|
|
2015-08-20 16:37:03 +02:00
|
|
|
unless params["piece_jointe_#{piece_jointe.type}"].nil?
|
2015-08-20 15:15:20 +02:00
|
|
|
piece_jointe.content = params["piece_jointe_#{piece_jointe.id}"]
|
|
|
|
piece_jointe.save
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-08-20 16:37:03 +02:00
|
|
|
if check_missing_attributes(params) || check_format_email(@dossier.mail_contact).nil?
|
|
|
|
redirect_to url_for(controller: :description, action: :error)
|
2015-08-10 11:05:06 +02:00
|
|
|
else
|
|
|
|
if params[:back_url] == 'recapitulatif'
|
|
|
|
@commentaire = Commentaire.create
|
|
|
|
@commentaire.email = 'Modification détails'
|
|
|
|
@commentaire.body = 'Les informations détaillées de la demande ont été modifiées. Merci de le prendre en compte.'
|
|
|
|
@commentaire.dossier = @dossier
|
|
|
|
@commentaire.save
|
|
|
|
end
|
|
|
|
|
2015-08-20 16:37:03 +02:00
|
|
|
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-08-11 15:39:16 +02:00
|
|
|
def create_params
|
|
|
|
params.permit(:nom_projet, :description, :montant_projet, :montant_aide_demande, :date_previsionnelle, :lien_plus_infos, :mail_contact)
|
|
|
|
end
|
|
|
|
|
2015-08-20 16:37:03 +02:00
|
|
|
# TODO dans un validateur, dans le model
|
|
|
|
def check_missing_attributes(params)
|
2015-08-10 11:05:06 +02:00
|
|
|
params[:nom_projet].strip == '' || params[:description].strip == '' || params[:montant_projet].strip == '' || params[:montant_aide_demande].strip == '' || params[:date_previsionnelle].strip == '' || params[:mail_contact].strip == ''
|
|
|
|
end
|
|
|
|
|
2015-08-20 16:37:03 +02:00
|
|
|
# TODO dans un validateur, dans le model
|
|
|
|
def check_format_email(email)
|
2015-08-10 11:05:06 +02:00
|
|
|
/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/.match(email)
|
|
|
|
end
|
|
|
|
end
|