diff --git a/app/models/alert.rb b/app/models/alert.rb index e88edd4..75d7b80 100644 --- a/app/models/alert.rb +++ b/app/models/alert.rb @@ -22,6 +22,8 @@ class Alert < ActiveRecord::Base validates :semester, :presence => true serialize :sections, Array + validate :section_check + scope :user_alerts, lambda { |user_id| where('Alerts.user_id = ?', user_id) @@ -34,4 +36,20 @@ class Alert < ActiveRecord::Base def course_name "#{self.department} #{self.course}" end + + protected + def section_check + section_count = 0 + sections.each do |section| + unless section =~ /^[A-Za-z0-9 ]$/ || section.empty? + errors.add(:sections, 'You entered an invalid section number') + else + section_count += 1 if !section.empty? + end + end + + if section_count == 0 + errors.add(:sections, 'You must enter at least one section') + end + end end