Add validation to avoid overlapping

Add validation in EventSchedule to avoid that several event schedules which belongs to the same schedule and room overlap.
This commit is contained in:
Ana 2016-08-23 02:20:16 +02:00 committed by Ana María Martínez Gómez
parent eea35c5578
commit 37edffe7de
2 changed files with 40 additions and 0 deletions

View file

@ -11,6 +11,8 @@ class EventSchedule < ActiveRecord::Base
validates :start_time, presence: true
validates :event, uniqueness: { scope: :schedule }
validate :not_overlapping
scope :confirmed, -> { joins(:event).where('state = ?', 'confirmed') }
scope :canceled, -> { joins(:event).where('state = ?', 'canceled') }
scope :withdrawn, -> { joins(:event).where('state = ?', 'withdrawn') }
@ -36,4 +38,15 @@ class EventSchedule < ActiveRecord::Base
def conference_id
schedule.program.conference_id
end
def not_overlapping
if room
room.event_schedules.where(schedule: schedule).where.not(id: id).each do |e|
if (e.start_time <= start_time && e.end_time > start_time) || (e.end_time >= end_time && e.start_time < end_time) || (e.start_time > start_time && e.start_time < end_time)
errors.add(:event, "can't be scheduled at the same time than other event in the same room")
break
end
end
end
end
end