Validate room and start_time for EventSchedule

Don't allow an event to be scheduled outside of it's track's room and
time slot
This commit is contained in:
AEtherC0r3 2017-08-08 16:18:11 +03:00 committed by Stella Rouzi
parent 7067803cb9
commit c4eec6a3de
2 changed files with 87 additions and 0 deletions

View file

@ -12,6 +12,8 @@ class EventSchedule < ActiveRecord::Base
validates :event, uniqueness: { scope: :schedule }
validate :start_after_end_hour
validate :start_before_start_hour
validate :room_of_track
validate :during_track
scope :confirmed, -> { joins(:event).where('state = ?', 'confirmed') }
scope :canceled, -> { joins(:event).where('state = ?', 'canceled') }
@ -52,4 +54,26 @@ class EventSchedule < ActiveRecord::Base
def conference_id
schedule.program.conference_id
end
##
# Validates that the event is scheduled in the same room as it's track
#
def room_of_track
if event && event.track.try(:room) && event.track.room != room
errors.add(:room, "must be the same as the track's room (#{event.track.room.name})")
end
end
##
# Validates that the event is scheduled within it's track's time slot
#
def during_track
if event && event.track.try(:start_date) && event.track.start_date > start_time
errors.add(:start_time, "can't be before the track's start date (#{event.track.start_date})")
end
if event && event.track.try(:end_date) && event.track.end_date + 1.day < end_time
errors.add(:end_time, "can't be after the track's end date (#{event.track.end_date})")
end
end
end