2021-06-22 11:13:44 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for Machines
|
|
|
|
class MachineService
|
2023-02-17 12:06:32 +01:00
|
|
|
class << self
|
2023-03-07 11:39:36 +01:00
|
|
|
include ApplicationHelper
|
|
|
|
|
2023-02-17 12:06:32 +01:00
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def list(filters)
|
|
|
|
sort_by = Setting.get('machines_sort_by') || 'default'
|
|
|
|
machines = if sort_by == 'default'
|
|
|
|
Machine.includes(:machine_image, :plans)
|
|
|
|
else
|
|
|
|
Machine.includes(:machine_image, :plans).order(sort_by)
|
|
|
|
end
|
|
|
|
# do not include soft destroyed
|
|
|
|
machines = machines.where(deleted_at: nil)
|
|
|
|
|
|
|
|
machines = filter_by_disabled(machines, filters)
|
2023-03-07 11:39:36 +01:00
|
|
|
filter_by_categories(machines, filters)
|
2023-02-17 12:06:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# @param machines [ActiveRecord::Relation<Machine>]
|
|
|
|
# @param filters [ActionController::Parameters]
|
|
|
|
def filter_by_disabled(machines, filters)
|
|
|
|
return machines if filters[:disabled].blank?
|
2022-11-22 14:17:25 +01:00
|
|
|
|
2021-06-22 11:13:44 +02:00
|
|
|
state = filters[:disabled] == 'false' ? [nil, false] : true
|
2023-02-17 12:06:32 +01:00
|
|
|
machines.where(disabled: state)
|
2021-06-22 11:13:44 +02:00
|
|
|
end
|
|
|
|
|
2023-02-17 12:06:32 +01:00
|
|
|
# @param machines [ActiveRecord::Relation<Machine>]
|
|
|
|
# @param filters [ActionController::Parameters]
|
2023-03-07 11:39:36 +01:00
|
|
|
def filter_by_categories(machines, filters)
|
2023-02-17 12:06:32 +01:00
|
|
|
return machines if filters[:category].blank?
|
|
|
|
|
2023-03-07 11:39:36 +01:00
|
|
|
machines.where(machine_category_id: filters[:category].split(',').map { |id| id == 'none' ? nil : id })
|
2023-02-17 12:06:32 +01:00
|
|
|
end
|
2021-06-22 11:13:44 +02:00
|
|
|
end
|
|
|
|
end
|