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 def create
@dossier = Dossier.find(params[:dossier_id]) @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? unless params[:cerfa_pdf].nil?
cerfa = @dossier.cerfa cerfa = @dossier.cerfa
cerfa.content = params[:cerfa_pdf] cerfa.content = params[:cerfa_pdf]
@ -31,15 +37,15 @@ class DescriptionController < ApplicationController
end end
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) redirect_to url_for(controller: :description, action: :error)
else else
if params[:back_url] == 'recapitulatif' if params[:back_url] == 'recapitulatif'
@commentaire = Commentaire.create commentaire = Commentaire.create
@commentaire.email = 'Modification détails' 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.body = 'Les informations détaillées de la demande ont été modifiées. Merci de le prendre en compte.'
@commentaire.dossier = @dossier commentaire.dossier = @dossier
@commentaire.save commentaire.save
end end
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id) redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id)
@ -56,9 +62,4 @@ class DescriptionController < ApplicationController
def check_missing_attributes(params) 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 == '' 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
# 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 end

View file

@ -12,6 +12,8 @@ class Dossier < ActiveRecord::Base
before_create :build_default_cerfa 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) def retrieve_piece_jointe_by_type(type)
pieces_jointes.where(type_piece_jointe_id: type).last pieces_jointes.where(type_piece_jointe_id: type).last
end 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. # 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.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.autoload_paths += %W(#{config.root}/lib)
config.assets.paths << Rails.root.join('app', 'assets', 'javascript') 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 context 'Mauvais format(s)' do
it 'mail_contact n\'est un format d\'email' 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' 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
end end

View file

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

View file

@ -31,6 +31,12 @@ describe Dossier do
it { is_expected.to delegate_method(:types_piece_jointe).to(:formulaire) } it { is_expected.to delegate_method(:types_piece_jointe).to(:formulaire) }
end 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 'methods' do
let(:dossier) { create(:dossier, :with_entreprise) } let(:dossier) { create(:dossier, :with_entreprise) }
let(:entreprise) { dossier.entreprise } let(:entreprise) { dossier.entreprise }
@ -80,4 +86,5 @@ describe Dossier do
end end
end end
end end
end
end end