1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/controllers/api/trainings_controller.rb
Sylvain 08244d53dc improve trainings load times
- do not eager load trainings availabilities in INDEX action if not requsted
- in trainings monitoring (font), do not load all availabilities for all traninings by default, by select a training first to load its availabilities
2016-06-15 12:01:09 +02:00

63 lines
1.7 KiB
Ruby

class API::TrainingsController < API::ApiController
include ApplicationHelper
before_action :authenticate_user!, except: [:index, :show]
before_action :set_training, only: [:update, :destroy]
def index
@requested_attributes = params[:requested_attributes]
@trainings = policy_scope(Training)
if attribute_requested?(@requested_attributes, 'availabilities')
@trainings = @trainings.includes(:availabilities => [:slots => [:reservation => [:user => [:profile, :trainings]]]]).order('availabilities.start_at DESC')
end
end
def show
@training = Training.includes(availabilities: {slots: {reservation: {user: [:profile, :trainings] }}})
.where(id: params[:id]).first
end
def create
authorize Training
@training = Training.new(training_params)
if @training.save
render :show, status: :created, location: @training
else
render json: @training.errors, status: :unprocessable_entity
end
end
def update
authorize Training
if params[:training][:users].present?
members = User.where(id: valid_training_params[:users])
members.each do |m|
m.trainings << @training
end
else
@training.update(training_params)
end
head :no_content
end
def destroy
authorize @training
@training.destroy
head :no_content
end
private
def set_training
@training = Training.find(params[:id])
end
def valid_training_params
params.require(:training).permit(:id, users: [])
end
def training_params
params.require(:training).permit(:id, :name, :description, :machine_ids, :plan_ids, :nb_total_places, machine_ids: [], plan_ids: [])
end
end