demarches-normaliennes/app/controllers/administrateurs/labels_controller.rb

63 lines
1.4 KiB
Ruby
Raw Normal View History

2024-10-16 14:31:08 +02:00
# frozen_string_literal: true
module Administrateurs
2024-10-29 15:39:58 +01:00
class LabelsController < AdministrateurController
2024-10-16 14:31:08 +02:00
before_action :retrieve_procedure
before_action :retrieve_label, only: [:edit, :update, :destroy]
2024-10-16 14:31:08 +02:00
before_action :set_colors_collection, only: [:edit, :new, :create, :update]
def index
2024-10-29 15:39:58 +01:00
@labels = @procedure.labels
2024-10-16 14:31:08 +02:00
end
def edit
end
def new
2024-10-29 15:39:58 +01:00
@label = Label.new
2024-10-16 14:31:08 +02:00
end
def create
2024-10-29 15:39:58 +01:00
@label = @procedure.labels.build(label_params)
2024-10-16 14:31:08 +02:00
if @label.save
flash.notice = 'Le label a bien été créé'
2024-10-29 15:39:58 +01:00
redirect_to [:admin, @procedure, :labels]
2024-10-16 14:31:08 +02:00
else
flash.alert = @label.errors.full_messages
render :new
end
end
def update
2024-10-29 15:39:58 +01:00
if @label.update(label_params)
2024-10-16 14:31:08 +02:00
flash.notice = 'Le label a bien été modifié'
2024-10-29 15:39:58 +01:00
redirect_to [:admin, @procedure, :labels]
2024-10-16 14:31:08 +02:00
else
flash.alert = @label.errors.full_messages
render :edit
end
end
def destroy
@label.destroy!
flash.notice = 'Le label a bien été supprimé'
2024-10-29 15:39:58 +01:00
redirect_to [:admin, @procedure, :labels]
2024-10-16 14:31:08 +02:00
end
private
2024-10-29 15:39:58 +01:00
def label_params
params.require(:label).permit(:name, :color)
2024-10-16 14:31:08 +02:00
end
def retrieve_label
2024-10-29 15:39:58 +01:00
@label = @procedure.labels.find(params[:id])
2024-10-16 14:31:08 +02:00
end
def set_colors_collection
2024-10-29 15:39:58 +01:00
@colors_collection = Label.colors.keys
2024-10-16 14:31:08 +02:00
end
end
end