Add gestionnaire management
This commit is contained in:
parent
2a2aef34db
commit
b58b0de8b0
4 changed files with 87 additions and 13 deletions
|
@ -12,10 +12,12 @@ class Admin::GestionnairesController < AdminController
|
|||
|
||||
|
||||
def create
|
||||
@gestionnaire = Gestionnaire.create(create_procedure_params)
|
||||
gestionnaire_params = create_gestionnaire_params
|
||||
@gestionnaire = Gestionnaire.create(gestionnaire_params)
|
||||
|
||||
if (@gestionnaire.errors.empty?)
|
||||
flash.notice = 'Gestionnaire ajouté'
|
||||
if @gestionnaire.errors.messages.empty?
|
||||
flash.notice = 'Gestionnaire ajouté'
|
||||
GestionnaireMailer.new_gestionnaire(gestionnaire_params[:email], gestionnaire_params[:password]).deliver_now!
|
||||
else
|
||||
flash.alert = @gestionnaire.errors.full_messages.join('<br />').html_safe
|
||||
end
|
||||
|
@ -23,7 +25,7 @@ class Admin::GestionnairesController < AdminController
|
|||
redirect_to admin_gestionnaires_path
|
||||
end
|
||||
|
||||
def create_procedure_params
|
||||
def create_gestionnaire_params
|
||||
params.require(:gestionnaire).permit(:email)
|
||||
.merge(administrateur_id: current_administrateur.id)
|
||||
.merge(password: SecureRandom.hex(5))
|
||||
|
|
20
app/mailers/gestionnaire_mailer.rb
Normal file
20
app/mailers/gestionnaire_mailer.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
class GestionnaireMailer < ApplicationMailer
|
||||
|
||||
def new_gestionnaire email, password
|
||||
send_mail email, password, "Vous avez été nommé accompagnateur sur la plateforme TPS"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def vars_mailer email, password
|
||||
@password = password
|
||||
@email = email
|
||||
end
|
||||
|
||||
def send_mail email, password, subject
|
||||
vars_mailer email, password
|
||||
|
||||
mail(from: "tps@apientreprise.fr", to: email,
|
||||
subject: subject)
|
||||
end
|
||||
end
|
12
app/views/gestionnaire_mailer/new_gestionnaire.text.erb
Normal file
12
app/views/gestionnaire_mailer/new_gestionnaire.text.erb
Normal file
|
@ -0,0 +1,12 @@
|
|||
Bienvenue sur la plateforme TPS
|
||||
|
||||
Vous venez d'être nommé accompagnateur sur la plateforme TPS. Pour mémoire, voici quelques informations utiles :
|
||||
|
||||
URL : https://tps.apientreprise.fr/gestionnaires/sign_in
|
||||
Login : <%= @email %>
|
||||
Mot de passe : <%= @password %>
|
||||
|
||||
Bonne journée,
|
||||
|
||||
---
|
||||
L'équipe TPS - tps@apientreprise.fr
|
|
@ -13,38 +13,78 @@ describe Admin::GestionnairesController, type: :controller do
|
|||
|
||||
describe 'POST #create' do
|
||||
let(:email) { 'test@plop.com' }
|
||||
before do
|
||||
post :create, gestionnaire: { email: email }
|
||||
end
|
||||
it { expect(response.status).to eq(302) }
|
||||
it { expect(response).to redirect_to admin_gestionnaires_path }
|
||||
subject { post :create, gestionnaire: { email: email } }
|
||||
|
||||
describe 'Gestionnaire attributs in database' do
|
||||
let(:gestionnaire) { Gestionnaire.last }
|
||||
it { expect(gestionnaire.email).to eq(email) }
|
||||
it { expect(gestionnaire.administrateur_id).to eq(admin.id) }
|
||||
context 'When email is valid' do
|
||||
before do
|
||||
subject
|
||||
end
|
||||
it { expect(response.status).to eq(302) }
|
||||
it { expect(response).to redirect_to admin_gestionnaires_path }
|
||||
|
||||
describe 'Gestionnaire attributs in database' do
|
||||
let(:gestionnaire) { Gestionnaire.last }
|
||||
it { expect(gestionnaire.email).to eq(email) }
|
||||
it { expect(gestionnaire.administrateur_id).to eq(admin.id) }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when email is not valid' do
|
||||
before do
|
||||
subject
|
||||
end
|
||||
let(:email) { 'piou' }
|
||||
it { expect(response.status).to eq(302) }
|
||||
it { expect{ response }.not_to change(Gestionnaire, :count) }
|
||||
end
|
||||
|
||||
context 'when email is empty' do
|
||||
before do
|
||||
subject
|
||||
end
|
||||
let(:email) { '' }
|
||||
it { expect(response.status).to eq(302) }
|
||||
it { expect{ response }.not_to change(Gestionnaire, :count) }
|
||||
it 'Notification email is not send' do
|
||||
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
||||
expect(GestionnaireMailer).not_to receive(:deliver_now!)
|
||||
end
|
||||
end
|
||||
|
||||
context ' when email already exists' do
|
||||
let(:email) { 'test@plop.com' }
|
||||
before do
|
||||
subject
|
||||
post :create, gestionnaire: { email: email }
|
||||
end
|
||||
it { expect(response.status).to eq(302) }
|
||||
it { expect{ response }.not_to change(Gestionnaire, :count) }
|
||||
end
|
||||
|
||||
context 'Email notification' do
|
||||
|
||||
it 'Notification email is sent when email is valid' do
|
||||
expect(GestionnaireMailer).to receive(:new_gestionnaire).and_return(GestionnaireMailer)
|
||||
expect(GestionnaireMailer).to receive(:deliver_now!)
|
||||
subject
|
||||
end
|
||||
|
||||
context 'is not sent when email is not valid' do
|
||||
let(:email) { 'testplop.com' }
|
||||
it {
|
||||
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
||||
expect(GestionnaireMailer).not_to receive(:deliver_now!)
|
||||
subject
|
||||
}
|
||||
end
|
||||
|
||||
it 'is not sent when email already exists' do
|
||||
subject
|
||||
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
||||
expect(GestionnaireMailer).not_to receive(:deliver_now!)
|
||||
subject
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue