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

View File

@@ -45,5 +45,68 @@ describe EventSchedule do
end
end
end
describe '#room_of_track' do
before :each do
conference = create(:conference)
conference.venue = create(:venue)
@room = create(:room, venue: conference.venue)
@track = create(:track, program: conference.program, room: @room)
@event = create(:event, program: conference.program, track: @track)
end
context 'is valid' do
it 'when scheduled in the track\'s room' do
event_schedule = build(:event_schedule, event: @event, room: @room)
expect(event_schedule.valid?).to eq true
end
it 'when the track doesn\'t have a room' do
@track.room = nil
@track.save!
event_schedule = build(:event_schedule, event: @event)
expect(event_schedule.valid?).to eq true
end
end
context 'is invalid' do
it 'when scheduled in different room than the track\'s' do
event_schedule = build(:event_schedule, event: @event)
expect(event_schedule.valid?).to eq false
expect(event_schedule.errors[:room]).to eq ["must be the same as the track's room (#{@room.name})"]
end
end
end
describe '#during_track' do
before :each do
conference = create(:conference, start_date: Date.current - 1.day, start_hour: 0, end_hour: 24)
conference.venue = create(:venue)
@room = create(:room, venue: conference.venue)
@track = create(:track, program: conference.program, room: @room, start_date: Date.current, end_date: Date.current)
@event = create(:event, program: conference.program, track: @track)
end
context 'is valid' do
it 'when scheduled during the track\'s time slot' do
event_schedule = build(:event_schedule, event: @event, room: @room, start_time: Date.current + 3.hours)
expect(event_schedule.valid?).to eq true
end
end
context 'is invalid' do
it 'when scheduled before the track\'s start date' do
event_schedule = build(:event_schedule, event: @event, room: @room, start_time: Date.current - 1.hour)
expect(event_schedule.valid?).to eq false
expect(event_schedule.errors[:start_time]).to eq ["can't be before the track's start date (#{@track.start_date})"]
end
it 'when event ends after the track\'s end date' do
event_schedule = build(:event_schedule, event: @event, room: @room, start_time: Date.current + 1.day - 10.minutes)
expect(event_schedule.valid?).to eq false
expect(event_schedule.errors[:end_time]).to eq ["can't be after the track's end date (#{@track.end_date})"]
end
end
end
end
end