Remove instructeur
This commit is contained in:
parent
79b808470c
commit
477f7c9837
4 changed files with 64 additions and 0 deletions
|
@ -31,6 +31,22 @@ module Instructeurs
|
||||||
redirect_to instructeur_groupe_path(procedure, groupe_instructeur)
|
redirect_to instructeur_groupe_path(procedure, groupe_instructeur)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def remove_instructeur
|
||||||
|
if groupe_instructeur.instructeurs.one?
|
||||||
|
flash[:alert] = "Suppression impossible : il doit y avoir au moins un instructeur dans le groupe"
|
||||||
|
|
||||||
|
else
|
||||||
|
@instructeur = Instructeur.find(instructeur_id)
|
||||||
|
groupe_instructeur.instructeurs.destroy(@instructeur)
|
||||||
|
flash[:notice] = "L’instructeur « #{@instructeur.email} » a été retiré du groupe."
|
||||||
|
GroupeInstructeurMailer
|
||||||
|
.remove_instructeur(groupe_instructeur, @instructeur, current_user.email)
|
||||||
|
.deliver_later
|
||||||
|
end
|
||||||
|
|
||||||
|
redirect_to instructeur_groupe_path(procedure, groupe_instructeur)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_instructeur(email)
|
def create_instructeur(email)
|
||||||
|
@ -74,5 +90,9 @@ module Instructeurs
|
||||||
def instructeur_email
|
def instructeur_email
|
||||||
params[:instructeur][:email].strip.downcase
|
params[:instructeur][:email].strip.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def instructeur_id
|
||||||
|
params[:instructeur][:id]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,3 +27,11 @@
|
||||||
- @instructeurs.each do |instructeur|
|
- @instructeurs.each do |instructeur|
|
||||||
%tr
|
%tr
|
||||||
%td= instructeur.email
|
%td= instructeur.email
|
||||||
|
%td.actions= button_to 'retirer',
|
||||||
|
{ action: :remove_instructeur },
|
||||||
|
{ method: :delete,
|
||||||
|
data: { confirm: "Êtes-vous sûr de vouloir retirer l’instructeur « #{instructeur.email} » du groupe « #{@groupe_instructeur.label} » ?" },
|
||||||
|
params: { instructeur: { id: instructeur.id }},
|
||||||
|
class: 'button' }
|
||||||
|
|
||||||
|
= paginate @instructeurs
|
||||||
|
|
|
@ -291,6 +291,7 @@ Rails.application.routes.draw do
|
||||||
resources :groupes, only: [:index, :show], controller: 'groupe_instructeurs' do
|
resources :groupes, only: [:index, :show], controller: 'groupe_instructeurs' do
|
||||||
member do
|
member do
|
||||||
post 'add_instructeur'
|
post 'add_instructeur'
|
||||||
|
delete 'remove_instructeur'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -62,4 +62,39 @@ describe Instructeurs::GroupeInstructeursController, type: :controller do
|
||||||
it { expect(response).to redirect_to(instructeur_groupe_path(procedure, gi_1_2)) }
|
it { expect(response).to redirect_to(instructeur_groupe_path(procedure, gi_1_2)) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#remove_instructeur' do
|
||||||
|
let!(:new_instructeur) { create(:instructeur) }
|
||||||
|
|
||||||
|
before { gi_1_1.instructeurs << instructeur << new_instructeur }
|
||||||
|
|
||||||
|
def remove_instructeur(instructeur)
|
||||||
|
delete :remove_instructeur,
|
||||||
|
params: {
|
||||||
|
procedure_id: procedure.id,
|
||||||
|
id: gi_1_1.id,
|
||||||
|
instructeur: { id: instructeur.id }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are many instructeurs' do
|
||||||
|
before { remove_instructeur(new_instructeur) }
|
||||||
|
|
||||||
|
it { expect(gi_1_1.instructeurs).to include(instructeur) }
|
||||||
|
it { expect(gi_1_1.reload.instructeurs.count).to eq(1) }
|
||||||
|
it { expect(response).to redirect_to(instructeur_groupe_path(procedure, gi_1_1)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there is only one instructeur' do
|
||||||
|
before do
|
||||||
|
remove_instructeur(new_instructeur)
|
||||||
|
remove_instructeur(instructeur)
|
||||||
|
end
|
||||||
|
|
||||||
|
it { expect(gi_1_1.instructeurs).to include(instructeur) }
|
||||||
|
it { expect(gi_1_1.instructeurs.count).to eq(1) }
|
||||||
|
it { expect(flash.alert).to eq('Suppression impossible : il doit y avoir au moins un instructeur dans le groupe') }
|
||||||
|
it { expect(response).to redirect_to(instructeur_groupe_path(procedure, gi_1_1)) }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue