1
0
Fork 0

Basic validation on sections

This commit is contained in:
Andrew Tomaka 2013-04-12 13:53:25 -04:00
parent 71fb2efd84
commit 8d43db7c75
1 changed files with 18 additions and 0 deletions

View File

@ -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