Validate that tracks don't overlap

Also, add scope for accepted tracks
This commit is contained in:
AEtherC0r3 2017-08-08 14:18:42 +03:00 committed by Stella Rouzi
parent 16f294f3e8
commit 7067803cb9
2 changed files with 106 additions and 0 deletions

View file

@ -31,9 +31,11 @@ class Track < ActiveRecord::Base
validates :description, presence: true, if: :self_organized?
validate :valid_dates
validate :valid_room, if: :self_organized_and_accepted_or_confirmed?
validate :overlapping
before_validation :capitalize_color
scope :accepted, -> { where(state: 'accepted') }
scope :confirmed, -> { where(state: 'confirmed') }
scope :cfp_active, -> { where(cfp_active: true) }
@ -209,4 +211,20 @@ class Track < ActiveRecord::Base
errors.add(:room, "must be a room of #{program.conference.venue.name}")
end
end
##
# Check that there is no other track in the same room with overlapping dates
def overlapping
return unless start_date && end_date && room && program.try(:tracks)
(program.tracks.accepted + program.tracks.confirmed - [self]).each do |other_track|
if other_track.room == room &&
other_track.start_date && other_track.end_date &&
(other_track.start_date <= start_date && other_track.end_date >= start_date ||
other_track.start_date <= end_date && other_track.end_date >= end_date ||
start_date <= other_track.start_date && other_track.end_date <= end_date)
errors.add(:track, 'has overlapping dates with a confirmed or accepted track in the same room')
break
end
end
end
end