2016-02-05 17:40:58 +01:00
|
|
|
class Admin::GestionnairesController < AdminController
|
|
|
|
include SmartListing::Helper::ControllerExtensions
|
|
|
|
helper SmartListing::Helper
|
|
|
|
|
|
|
|
def index
|
|
|
|
@gestionnaires = smart_listing_create :gestionnaires,
|
2016-05-24 16:39:39 +02:00
|
|
|
current_administrateur.gestionnaires,
|
|
|
|
partial: "admin/gestionnaires/list",
|
|
|
|
array: true
|
2016-02-05 17:40:58 +01:00
|
|
|
@gestionnaire ||= Gestionnaire.new
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def create
|
2016-05-24 16:39:39 +02:00
|
|
|
@gestionnaire = Gestionnaire.find_by_email(params[:gestionnaire][:email])
|
2016-05-26 16:44:10 +02:00
|
|
|
procedure_id = params[:procedure_id]
|
2016-05-20 15:39:17 +02:00
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
if @gestionnaire.nil?
|
|
|
|
new_gestionnaire!
|
2016-02-05 17:40:58 +01:00
|
|
|
else
|
2016-05-24 16:39:39 +02:00
|
|
|
assign_gestionnaire!
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-11-15 05:19:30 +01:00
|
|
|
if procedure_id.present?
|
2016-10-18 15:49:46 +02:00
|
|
|
redirect_to admin_procedure_accompagnateurs_path(procedure_id: procedure_id)
|
|
|
|
else
|
|
|
|
redirect_to admin_gestionnaires_path
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-02-09 12:23:49 +01:00
|
|
|
def destroy
|
2016-05-24 16:39:39 +02:00
|
|
|
Gestionnaire.find(params[:id]).administrateurs.delete current_administrateur
|
2016-02-09 12:23:49 +01:00
|
|
|
redirect_to admin_gestionnaires_path
|
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def new_gestionnaire!
|
2016-10-18 15:49:46 +02:00
|
|
|
attributes = params.require(:gestionnaire).permit(:email)
|
|
|
|
.merge(password: SecureRandom.hex(5))
|
|
|
|
|
|
|
|
@gestionnaire = Gestionnaire.create(attributes.merge(
|
|
|
|
administrateurs: [current_administrateur]
|
|
|
|
))
|
2016-05-24 16:39:39 +02:00
|
|
|
|
|
|
|
if @gestionnaire.errors.messages.empty?
|
2016-12-16 14:39:17 +01:00
|
|
|
User.create(attributes)
|
2016-05-24 16:39:39 +02:00
|
|
|
flash.notice = 'Accompagnateur ajouté'
|
|
|
|
GestionnaireMailer.new_gestionnaire(@gestionnaire.email, @gestionnaire.password).deliver_now!
|
|
|
|
GestionnaireMailer.new_assignement(@gestionnaire.email, current_administrateur.email).deliver_now!
|
|
|
|
else
|
|
|
|
flash.alert = @gestionnaire.errors.full_messages.join('<br />').html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_gestionnaire!
|
|
|
|
if current_administrateur.gestionnaires.include? @gestionnaire
|
|
|
|
flash.alert = 'Accompagnateur déjà ajouté'
|
|
|
|
else
|
|
|
|
GestionnaireMailer.new_assignement(@gestionnaire.email, current_administrateur.email).deliver_now!
|
|
|
|
@gestionnaire.administrateurs.push current_administrateur
|
|
|
|
flash.notice = 'Accompagnateur ajouté'
|
|
|
|
#TODO Mailer no assign_to
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
2016-10-18 15:49:46 +02:00
|
|
|
end
|