move check email params in validator

This commit is contained in:
Tanguy PATTE 2015-08-21 11:37:13 +02:00
parent d557cee249
commit 88b663a514
7 changed files with 81 additions and 50 deletions

View file

@ -17,7 +17,13 @@ class DescriptionController < ApplicationController
def create
@dossier = Dossier.find(params[:dossier_id])
@dossier.update_attributes(create_params)
unless @dossier.update_attributes(create_params)
@dossier = @dossier.decorate
@formulaire = @dossier.formulaire
flash.now.alert = @dossier.errors.full_messages.join('<br />').html_safe
return render 'show'
end
unless params[:cerfa_pdf].nil?
cerfa = @dossier.cerfa
cerfa.content = params[:cerfa_pdf]
@ -31,15 +37,15 @@ class DescriptionController < ApplicationController
end
end
if check_missing_attributes(params) || check_format_email(@dossier.mail_contact).nil?
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'
@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
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
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id)
@ -56,9 +62,4 @@ class DescriptionController < ApplicationController
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
# TODO dans un validateur, dans le model
def check_format_email(email)
/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/.match(email)
end
end

View file

@ -12,6 +12,8 @@ class Dossier < ActiveRecord::Base
before_create :build_default_cerfa
validates :mail_contact, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/ }, unless: 'mail_contact.nil?'
def retrieve_piece_jointe_by_type(type)
pieces_jointes.where(type_piece_jointe_id: type).last
end

View file

@ -18,7 +18,8 @@ module AdmiFacile
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.default_locale = :fr
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.autoload_paths += %W(#{config.root}/lib)
config.assets.paths << Rails.root.join('app', 'assets', 'javascript')

View file

@ -0,0 +1,13 @@
fr:
activerecord:
models:
dossier: 'Dossier'
attributes:
dossier:
mail_contact: 'le mail de contact'
errors:
models:
dossier:
attributes:
mail_contact:
invalid: 'est incorrect'

View file

@ -104,7 +104,8 @@ describe DescriptionController, type: :controller do
context 'Mauvais format(s)' do
it 'mail_contact n\'est un format d\'email' 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: date_previsionnelle, mail_contact: 'test.com'
expect(response).to redirect_to("/dossiers/#{dossier_id}/description/error")
expect(response).to render_template('show')
expect(flash[:alert]).to be_present
end
end

View file

@ -10,5 +10,11 @@ FactoryGirl.define do
dossier.etablissement = etablissement
end
end
trait :with_pieces_jointes do
after(:build) do |dossier, _evaluator|
dossier.build_default_pieces_jointes
end
end
end
end

View file

@ -31,52 +31,59 @@ describe Dossier do
it { is_expected.to delegate_method(:types_piece_jointe).to(:formulaire) }
end
let(:dossier) { create(:dossier, :with_entreprise) }
let(:entreprise) { dossier.entreprise }
let(:etablissement) { dossier.etablissement }
subject { dossier }
describe '#types_piece_jointe' do
subject { dossier.types_piece_jointe }
it 'returns list of required piece justificative' do
expect(subject.size).to eq(7)
expect(subject).to include(TypePieceJointe.find(103))
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) }
end
describe 'creation' do
it 'create default cerfa' do
expect { described_class.create }.to change { Cerfa.count }.by(1)
describe 'methods' do
let(:dossier) { create(:dossier, :with_entreprise) }
let(:entreprise) { dossier.entreprise }
let(:etablissement) { dossier.etablissement }
subject { dossier }
describe '#types_piece_jointe' do
subject { dossier.types_piece_jointe }
it 'returns list of required piece justificative' do
expect(subject.size).to eq(7)
expect(subject).to include(TypePieceJointe.find(103))
end
end
it 'link cerfa to dossier' do
dossier = described_class.create
expect(dossier.cerfa).to eq(Cerfa.last)
end
end
describe 'creation' do
it 'create default cerfa' do
expect { described_class.create }.to change { Cerfa.count }.by(1)
end
describe '#retrieve_piece_jointe_by_type' do
let(:type) { 93 }
subject { dossier.retrieve_piece_jointe_by_type type }
before do
dossier.build_default_pieces_jointes
it 'link cerfa to dossier' do
dossier = described_class.create
expect(dossier.cerfa).to eq(Cerfa.last)
end
end
it 'returns piece jointe with given type' do
expect(subject.type).to eq(93)
end
end
describe '#build_default_pieces_jointes' do
context 'when dossier is linked to a formualire' do
let(:dossier) { create(:dossier) }
describe '#retrieve_piece_jointe_by_type' do
let(:type) { 93 }
subject { dossier.retrieve_piece_jointe_by_type type }
before do
dossier.build_default_pieces_jointes
end
it 'build all pieces jointes needed' do
expect(dossier.pieces_jointes.count).to eq(7)
it 'returns piece jointe with given type' do
expect(subject.type).to eq(93)
end
end
describe '#build_default_pieces_jointes' do
context 'when dossier is linked to a formualire' do
let(:dossier) { create(:dossier) }
before do
dossier.build_default_pieces_jointes
end
it 'build all pieces jointes needed' do
expect(dossier.pieces_jointes.count).to eq(7)
end
end
end
end