1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/models/slot.rb

30 lines
988 B
Ruby

# frozen_string_literal: true
# A Time range, slicing an Availability.
# Slots duration are defined globally by Setting.get('slot_duration') but can be
# overridden per availability.
class Slot < ApplicationRecord
include NotifyWith::NotificationAttachedObject
has_many :slots_reservations, dependent: :destroy
has_many :reservations, through: :slots_reservations
belongs_to :availability
attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :current_user_slots_reservations_ids
def full?(reservable = nil)
availability_places = availability.available_places_per_slot(reservable)
return false if availability_places.nil?
if reservable.nil?
slots_reservations.where(canceled_at: nil).count >= availability_places
else
slots_reservations.includes(:reservation).where(canceled_at: nil).where('reservations.reservable': reservable).count >= availability_places
end
end
def duration
(end_at - start_at).seconds
end
end