Basic validation on sections
This commit is contained in:
parent
71fb2efd84
commit
8d43db7c75
1 changed files with 18 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue