2022-05-09 17:18:49 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for Trainings
|
|
|
|
class TrainingService
|
2023-01-24 14:03:01 +01:00
|
|
|
class << self
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def list(filters)
|
|
|
|
trainings = Training.includes(:training_image, :plans, :machines)
|
2022-05-09 17:18:49 +02:00
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
trainings = filter_by_disabled(trainings, filters)
|
|
|
|
trainings = filter_by_public_page(trainings, filters)
|
|
|
|
|
|
|
|
if filters[:requested_attributes].try(:include?, 'availabilities')
|
|
|
|
trainings = trainings.includes(availabilities: [slots: [reservation: [user: %i[profile trainings]]]])
|
|
|
|
.order('availabilities.start_at DESC')
|
|
|
|
end
|
|
|
|
|
|
|
|
trainings
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
2023-01-24 14:03:01 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# @param trainings [ActiveRecord::Relation<Training>]
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def filter_by_disabled(trainings, filters)
|
|
|
|
return trainings if filters[:disabled].blank?
|
|
|
|
|
|
|
|
state = filters[:disabled] == 'false' ? [nil, false] : true
|
|
|
|
trainings.where(disabled: state)
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
# @param trainings [ActiveRecord::Relation<Training>]
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def filter_by_public_page(trainings, filters)
|
|
|
|
return trainings if filters[:public_page].blank?
|
|
|
|
|
|
|
|
state = filters[:public_page] == 'false' ? [nil, false] : true
|
|
|
|
trainings.where(public_page: state)
|
|
|
|
end
|
2022-05-09 17:18:49 +02:00
|
|
|
end
|
|
|
|
end
|