2020-03-25 17:45:53 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# MachinesAvailability is the relation table between a Machine and an Availability.
|
|
|
|
# It defines periods in the agenda, when the given machine can be reserved by members.
|
2020-03-25 10:16:47 +01:00
|
|
|
class MachinesAvailability < ApplicationRecord
|
2016-03-23 18:39:41 +01:00
|
|
|
belongs_to :machine
|
|
|
|
belongs_to :availability
|
|
|
|
after_destroy :cleanup_availability
|
|
|
|
|
|
|
|
# when the MachinesAvailability is deleted (from Machine destroy cascade), we delete the corresponding
|
2017-02-13 14:38:28 +01:00
|
|
|
# availability if the deleted machine was the last of this availability slot, and the availability is not
|
2016-03-23 18:39:41 +01:00
|
|
|
# currently being destroyed.
|
|
|
|
def cleanup_availability
|
2020-03-25 17:45:53 +01:00
|
|
|
return if availability.destroying
|
|
|
|
|
|
|
|
return unless availability.machines_availabilities.empty?
|
|
|
|
|
|
|
|
availability.safe_destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|