2016-03-23 18:39:41 +01:00
|
|
|
class Slot < ActiveRecord::Base
|
|
|
|
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?
|
|
|
|
|
2017-02-28 13:23:31 +01:00
|
|
|
# for backward compatibility
|
|
|
|
def reservation
|
|
|
|
reservations.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
update_column(:destroying, true)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-02-27 16:15:27 +01:00
|
|
|
def is_complete?
|
|
|
|
reservations.length >= availability.nb_total_places
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
|
|
|
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',
|
|
|
|
receiver: User.admins,
|
|
|
|
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',
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_be_modified?
|
|
|
|
return false if (start_at - Time.now) / 1.day < 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def dates_were_modified?
|
|
|
|
start_at_changed? or end_at_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def canceled?
|
|
|
|
canceled_at_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_ex_start_end_dates_attrs
|
|
|
|
update_columns(ex_start_at: start_at_was, ex_end_at: end_at_was)
|
|
|
|
end
|
|
|
|
end
|