demarches-normaliennes/app/controllers/new_administrateur/services_controller.rb

95 lines
2.7 KiB
Ruby
Raw Normal View History

2018-04-19 16:15:44 +02:00
module NewAdministrateur
class ServicesController < AdministrateurController
def index
@services = services.ordered
2018-04-20 12:03:02 +02:00
@procedure = procedure
2018-04-19 16:15:44 +02:00
end
2018-04-19 10:44:14 +02:00
def new
2018-04-24 10:07:20 +02:00
@procedure = procedure
2018-04-19 10:44:14 +02:00
end
def create
new_service = Service.new(service_params)
new_service.administrateur = current_administrateur
if new_service.save
2018-04-19 15:59:11 +02:00
redirect_to services_path(procedure_id: params[:procedure_id]),
notice: "#{new_service.nom} créé"
2018-04-19 10:44:14 +02:00
else
@procedure = procedure
2018-04-19 10:44:14 +02:00
flash[:alert] = new_service.errors.full_messages
render :new
end
end
2018-04-19 18:12:48 +02:00
def edit
@service = service
2018-04-24 10:07:20 +02:00
@procedure = procedure
2018-04-19 18:12:48 +02:00
end
def update
@service = service
if @service.update(service_params)
2018-04-19 15:59:11 +02:00
redirect_to services_path(procedure_id: params[:procedure_id]),
notice: "#{@service.nom} modifié"
2018-04-19 18:12:48 +02:00
else
@procedure = procedure
2018-04-19 18:12:48 +02:00
flash[:alert] = @service.errors.full_messages
render :edit
end
end
2018-04-20 12:03:02 +02:00
def add_to_procedure
procedure = current_administrateur.procedures.find(procedure_params[:id])
service = services.find(procedure_params[:service_id])
procedure.update(service: service)
redirect_to admin_procedure_path(procedure.id),
notice: "service affecté : #{procedure.service.nom}"
end
2018-04-23 15:32:27 +02:00
def destroy
service_to_destroy = service
if service_to_destroy.procedures.present?
if service_to_destroy.procedures.count == 1
message = "la procédure #{service_to_destroy.procedures.first.libelle} utilise encore le service #{service_to_destroy.nom}. Veuillez l'affecter à un autre service avant de pouvoir le supprimer"
else
message = "les procédures #{service_to_destroy.procedures.map(&:libelle).join(', ')} utilisent encore le service #{service.nom}. Veuillez les affecter à un autre service avant de pouvoir le supprimer"
end
flash[:alert] = message
redirect_to services_path(procedure_id: params[:procedure_id])
else
service_to_destroy.destroy
redirect_to services_path(procedure_id: params[:procedure_id]),
notice: "#{service_to_destroy.nom} est supprimé"
end
end
2018-04-19 16:15:44 +02:00
private
2018-04-19 10:44:14 +02:00
def service_params
params.require(:service).permit(:nom, :organisme, :siret, :type_organisme, :email, :telephone, :horaires, :adresse)
2018-04-19 10:44:14 +02:00
end
2018-04-19 18:12:48 +02:00
def service
services.find(params[:id])
end
2018-04-19 16:15:44 +02:00
def services
current_administrateur.services
end
2018-04-20 12:03:02 +02:00
def procedure_params
params.require(:procedure).permit(:id, :service_id)
end
def procedure
current_administrateur.procedures.find(params[:procedure_id])
end
2018-04-19 16:15:44 +02:00
end
end