move check email params in validator
This commit is contained in:
parent
d557cee249
commit
88b663a514
7 changed files with 81 additions and 50 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
|
13
config/locales/models/dossier/fr.yml
Normal file
13
config/locales/models/dossier/fr.yml
Normal 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'
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -31,6 +31,12 @@ describe Dossier do
|
|||
it { is_expected.to delegate_method(:types_piece_jointe).to(:formulaire) }
|
||||
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(:entreprise) { dossier.entreprise }
|
||||
|
@ -80,4 +86,5 @@ describe Dossier do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue