move validation in model for dossier
This commit is contained in:
parent
bab43c5cd3
commit
e5e2fef8ae
5 changed files with 82 additions and 28 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
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'
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -32,9 +32,31 @@ describe Dossier do
|
|||
end
|
||||
|
||||
describe 'validation' do
|
||||
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
|
||||
let(:dossier) { create(:dossier, :with_entreprise) }
|
||||
|
|
Loading…
Reference in a new issue