1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/services/machine_service.rb
2023-08-24 14:41:29 +02:00

43 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# Provides methods for Machines
class MachineService
class << self
include ApplicationHelper
# @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, :space)
else
Machine.includes(:machine_image, :plans, :space).order(sort_by)
end
# do not include soft destroyed
machines = machines.where(deleted_at: nil)
machines = filter_by_disabled(machines, filters)
filter_by_categories(machines, filters)
end
private
# @param machines [ActiveRecord::Relation<Machine>]
# @param filters [ActionController::Parameters]
def filter_by_disabled(machines, filters)
return machines if filters[:disabled].blank?
state = filters[:disabled] == 'false' ? [nil, false] : true
machines.where(disabled: state)
end
# @param machines [ActiveRecord::Relation<Machine>]
# @param filters [ActionController::Parameters]
def filter_by_categories(machines, filters)
return machines if filters[:category].blank?
machines.where(machine_category_id: filters[:category].split(',').map { |id| id == 'none' ? nil : id })
end
end
end