2019-05-07 12:24:51 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-15 18:08:02 +02:00
|
|
|
# Time range, slicing an Availability.
|
2020-05-20 17:36:57 +02:00
|
|
|
# Its duration is defined by globally by Setting.get('slot_duration') but can be overridden per availability
|
2019-05-07 12:24:51 +02:00
|
|
|
# During a slot a Reservation is possible
|
2020-03-25 17:45:53 +01:00
|
|
|
# Only reserved slots are persisted in DB, others are instantiated on the fly
|
2020-03-25 10:16:47 +01:00
|
|
|
class Slot < ApplicationRecord
|
2016-03-23 18:39:41 +01:00
|
|
|
include NotifyWith::NotificationAttachedObject
|
|
|
|
|
2017-02-28 13:23:31 +01:00
|
|
|
has_many :slots_reservations, dependent: :destroy
|
|
|
|
has_many :reservations, through: :slots_reservations
|
2016-03-23 18:39:41 +01:00
|
|
|
belongs_to :availability
|
|
|
|
|
2017-02-28 13:23:31 +01:00
|
|
|
attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :is_reserved_by_current_user
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
after_update :set_ex_start_end_dates_attrs, if: :dates_were_modified?
|
|
|
|
after_update :notify_member_and_admin_slot_is_modified, if: :dates_were_modified?
|
|
|
|
|
|
|
|
after_update :notify_member_and_admin_slot_is_canceled, if: :canceled?
|
2019-11-20 17:06:42 +01:00
|
|
|
after_update :update_event_nb_free_places, if: :canceled?
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2017-02-28 13:23:31 +01:00
|
|
|
# for backward compatibility
|
|
|
|
def reservation
|
|
|
|
reservations.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
update_column(:destroying, true)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2019-05-07 12:24:51 +02:00
|
|
|
def complete?
|
2017-02-27 16:15:27 +01:00
|
|
|
reservations.length >= availability.nb_total_places
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
2019-05-07 12:24:51 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def notify_member_and_admin_slot_is_modified
|
|
|
|
NotificationCenter.call type: 'notify_member_slot_is_modified',
|
|
|
|
receiver: reservation.user,
|
|
|
|
attached_object: self
|
|
|
|
NotificationCenter.call type: 'notify_admin_slot_is_modified',
|
2020-04-29 15:34:30 +02:00
|
|
|
receiver: User.admins_and_managers,
|
2016-03-23 18:39:41 +01:00
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_member_and_admin_slot_is_canceled
|
|
|
|
NotificationCenter.call type: 'notify_member_slot_is_canceled',
|
|
|
|
receiver: reservation.user,
|
|
|
|
attached_object: self
|
|
|
|
NotificationCenter.call type: 'notify_admin_slot_is_canceled',
|
2020-04-29 15:34:30 +02:00
|
|
|
receiver: User.admins_and_managers,
|
2016-03-23 18:39:41 +01:00
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def dates_were_modified?
|
2020-03-24 16:45:27 +01:00
|
|
|
saved_change_to_start_at? || saved_change_to_end_at?
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def canceled?
|
2020-03-24 16:45:27 +01:00
|
|
|
saved_change_to_canceled_at?
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_ex_start_end_dates_attrs
|
2020-10-20 15:14:11 +02:00
|
|
|
update_columns(ex_start_at: start_at_before_last_save, ex_end_at: end_at_before_last_save)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2019-11-20 17:06:42 +01:00
|
|
|
|
|
|
|
def update_event_nb_free_places
|
|
|
|
return unless reservation.reservable_type == 'Event'
|
|
|
|
raise NotImplementedError if reservations.count > 1
|
|
|
|
|
|
|
|
reservation.update_event_nb_free_places
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|