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
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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')
|
||||||
|
|
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
|
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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -31,52 +31,59 @@ 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
|
||||||
|
|
||||||
let(:dossier) { create(:dossier, :with_entreprise) }
|
describe 'validation' do
|
||||||
|
it { is_expected.to allow_value('tanguy@plop.com').for(:mail_contact) }
|
||||||
let(:entreprise) { dossier.entreprise }
|
it { is_expected.not_to allow_value('tanguyplop.com').for(:mail_contact) }
|
||||||
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
|
end
|
||||||
|
|
||||||
describe 'creation' do
|
describe 'methods' do
|
||||||
it 'create default cerfa' do
|
let(:dossier) { create(:dossier, :with_entreprise) }
|
||||||
expect { described_class.create }.to change { Cerfa.count }.by(1)
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
it 'link cerfa to dossier' do
|
describe 'creation' do
|
||||||
dossier = described_class.create
|
it 'create default cerfa' do
|
||||||
expect(dossier.cerfa).to eq(Cerfa.last)
|
expect { described_class.create }.to change { Cerfa.count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe '#retrieve_piece_jointe_by_type' do
|
it 'link cerfa to dossier' do
|
||||||
let(:type) { 93 }
|
dossier = described_class.create
|
||||||
subject { dossier.retrieve_piece_jointe_by_type type }
|
expect(dossier.cerfa).to eq(Cerfa.last)
|
||||||
before do
|
end
|
||||||
dossier.build_default_pieces_jointes
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns piece jointe with given type' do
|
describe '#retrieve_piece_jointe_by_type' do
|
||||||
expect(subject.type).to eq(93)
|
let(:type) { 93 }
|
||||||
end
|
subject { dossier.retrieve_piece_jointe_by_type type }
|
||||||
end
|
|
||||||
|
|
||||||
describe '#build_default_pieces_jointes' do
|
|
||||||
context 'when dossier is linked to a formualire' do
|
|
||||||
let(:dossier) { create(:dossier) }
|
|
||||||
before do
|
before do
|
||||||
dossier.build_default_pieces_jointes
|
dossier.build_default_pieces_jointes
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue