From e5e2fef8aed0d23380229f657e81a1f43a6da46e Mon Sep 17 00:00:00 2001 From: Tanguy PATTE Date: Tue, 25 Aug 2015 10:19:39 +0200 Subject: [PATCH] move validation in model for dossier --- app/controllers/description_controller.rb | 10 +--- app/models/dossier.rb | 6 +++ config/locales/models/dossier/fr.yml | 21 ++++++++- .../description_controller_spec.rb | 47 +++++++++++++------ spec/models/dossier_spec.rb | 26 +++++++++- 5 files changed, 82 insertions(+), 28 deletions(-) diff --git a/app/controllers/description_controller.rb b/app/controllers/description_controller.rb index c4b3246d5..ffb766308 100644 --- a/app/controllers/description_controller.rb +++ b/app/controllers/description_controller.rb @@ -37,9 +37,6 @@ class DescriptionController < ApplicationController end end - if check_missing_attributes(params) - redirect_to url_for(controller: :description, action: :error) - else if params[:back_url] == 'recapitulatif' commentaire = Commentaire.create commentaire.email = 'Modification détails' @@ -49,7 +46,7 @@ class DescriptionController < ApplicationController end redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id) - end + end private @@ -57,9 +54,4 @@ class DescriptionController < ApplicationController def create_params params.permit(:nom_projet, :description, :montant_projet, :montant_aide_demande, :date_previsionnelle, :lien_plus_infos, :mail_contact) end - - # TODO dans un validateur, dans le model - def check_missing_attributes(params) - 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 end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index ee17ac949..cc7b1609c 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -15,6 +15,12 @@ class Dossier < ActiveRecord::Base after_save :build_default_pieces_jointes, if: Proc.new { formulaire_id_changed? } validates :mail_contact, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/ }, unless: 'mail_contact.nil?' + validates :nom_projet, presence: true, allow_blank: false, allow_nil: true + validates :description, presence: true, allow_blank: false, allow_nil: true + validates :montant_projet, presence: true, allow_blank: false, allow_nil: true + validates :montant_aide_demande, presence: true, allow_blank: false, allow_nil: true + validates :date_previsionnelle, presence: true, allow_blank: false, unless: Proc.new { description.nil? } + def retrieve_piece_jointe_by_type(type) pieces_jointes.where(type_piece_jointe_id: type).last diff --git a/config/locales/models/dossier/fr.yml b/config/locales/models/dossier/fr.yml index 0b18dc5da..383373dde 100644 --- a/config/locales/models/dossier/fr.yml +++ b/config/locales/models/dossier/fr.yml @@ -4,10 +4,27 @@ fr: dossier: 'Dossier' attributes: dossier: - mail_contact: 'le mail de contact' + mail_contact: 'Le mail de contact' + nom_projet: 'Le nom du projet' + description: 'La description' + montant_projet: 'Le montant du projet' + montant_aide_demande: "Le montant d'aide demandée" + date_previsionnelle: "La date de début prévisionnelle" errors: models: dossier: attributes: mail_contact: - invalid: 'est incorrect' \ No newline at end of file + blank: 'doit être rempli' + invalid: 'est incorrect' + nom_projet: + blank: 'doit être rempli' + description: + blank: 'doit être remplie' + montant_projet: + blank: 'doit être rempli' + montant_aide_demande: + blank: 'doit être rempli' + date_previsionnelle: + blank: 'doit être remplie' + diff --git a/spec/controllers/description_controller_spec.rb b/spec/controllers/description_controller_spec.rb index b5c385595..06d626860 100644 --- a/spec/controllers/description_controller_spec.rb +++ b/spec/controllers/description_controller_spec.rb @@ -70,29 +70,46 @@ describe DescriptionController, type: :controller do end context 'Attribut(s) manquant(s)' do - it 'nom_projet manquant' do - post :create, dossier_id: dossier_id, nom_projet: '', description: description, montant_projet: montant_projet, montant_aide_demande: montant_aide_demande, date_previsionnelle: date_previsionnelle, mail_contact: mail_contact - expect(response).to redirect_to("/dossiers/#{dossier_id}/description/error") + subject { + post :create, + dossier_id: dossier_id, + nom_projet: nom_projet, + description: description, + montant_projet: montant_projet, + montant_aide_demande: montant_aide_demande, + date_previsionnelle: date_previsionnelle, + mail_contact: mail_contact + } + before { subject } + + context 'nom_projet empty' do + let(:nom_projet) { '' } + it { is_expected.to render_template(:show) } + it { expect(flash[:alert]).to be_present } end - it 'description manquante' do - post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: '', montant_projet: montant_projet, montant_aide_demande: montant_aide_demande, date_previsionnelle: date_previsionnelle, mail_contact: mail_contact - expect(response).to redirect_to("/dossiers/#{dossier_id}/description/error") + context 'description empty' do + let(:description) { '' } + it { is_expected.to render_template(:show) } + it { expect(flash[:alert]).to be_present } end - it 'montant_projet manquant' do - post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description, montant_projet: '', montant_aide_demande: montant_aide_demande, date_previsionnelle: date_previsionnelle, mail_contact: mail_contact - expect(response).to redirect_to("/dossiers/#{dossier_id}/description/error") + context 'montant_projet empty' do + let(:montant_projet) { '' } + it { is_expected.to render_template(:show) } + it { expect(flash[:alert]).to be_present } end - it 'montant_aide_demande manquant' do - post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description, montant_projet: montant_projet, montant_aide_demande: '', date_previsionnelle: date_previsionnelle, mail_contact: mail_contact - expect(response).to redirect_to("/dossiers/#{dossier_id}/description/error") + context 'montant_aide_demande empty' do + let(:montant_aide_demande) { '' } + it { is_expected.to render_template(:show) } + it { expect(flash[:alert]).to be_present } end - it 'date_previsionnelle manquante' do - post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description, montant_projet: montant_projet, montant_aide_demande: montant_aide_demande, date_previsionnelle: '', mail_contact: mail_contact - expect(response).to redirect_to("/dossiers/#{dossier_id}/description/error") + context 'date_previsionnelle empty' do + let(:date_previsionnelle) { '' } + it { is_expected.to render_template(:show) } + it { expect(flash[:alert]).to be_present } end it 'mail_contact manquant' do diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 1cd5d8898..26568599e 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -32,8 +32,30 @@ describe Dossier do end describe 'validation' do - it { is_expected.to allow_value('tanguy@plop.com').for(:mail_contact) } - it { is_expected.not_to allow_value('tanguyplop.com').for(:mail_contact) } + context 'mail_contact' do + it { is_expected.to allow_value('tanguy@plop.com').for(:mail_contact) } + it { is_expected.not_to allow_value('tanguyplop.com').for(:mail_contact) } + end + context 'nom_projet' do + it { is_expected.to allow_value(nil).for(:nom_projet) } + it { is_expected.not_to allow_value('').for(:nom_projet) } + it { is_expected.to allow_value('mon super projet').for(:nom_projet) } + end + context 'description' do + it { is_expected.to allow_value(nil).for(:description) } + it { is_expected.not_to allow_value('').for(:description) } + it { is_expected.to allow_value('ma superbe description').for(:description) } + end + context 'montant_projet' do + it { is_expected.to allow_value(nil).for(:montant_projet) } + it { is_expected.not_to allow_value('').for(:montant_projet) } + it { is_expected.to allow_value(124324).for(:montant_projet) } + end + context 'montant_aide_demande' do + it { is_expected.to allow_value(nil).for(:montant_aide_demande) } + it { is_expected.not_to allow_value('').for(:montant_aide_demande) } + it { is_expected.to allow_value(124324).for(:montant_aide_demande) } + end end describe 'methods' do