2013-04-02 02:43:01 -04:00
|
|
|
class Alert < ActiveRecord::Base
|
2013-04-24 19:12:12 -04:00
|
|
|
enum_attr :semester, %w(US13 FS13 SS14)
|
2013-04-12 13:12:29 -04:00
|
|
|
attr_accessible :course, :department, :semester, :sections
|
2013-04-07 06:14:15 -04:00
|
|
|
attr_protected :user_id
|
2013-04-07 06:21:53 -04:00
|
|
|
attr_protected :alerted
|
2013-04-02 03:07:11 -04:00
|
|
|
|
|
|
|
validates :department, :presence => true,
|
|
|
|
:length => {
|
|
|
|
:minimum => 2,
|
|
|
|
:maximum => 4
|
|
|
|
},
|
|
|
|
:format => {
|
|
|
|
:with => /\A[A-Za-z]+\Z/
|
|
|
|
}
|
|
|
|
validates :course, :presence => true,
|
|
|
|
:length => {
|
|
|
|
:minimum => 3,
|
|
|
|
:maximum => 4
|
|
|
|
},
|
|
|
|
:format => {
|
|
|
|
:with => /\A[0-9]+[A-Za-z]?\Z/
|
|
|
|
}
|
2013-04-03 19:44:01 -04:00
|
|
|
validates :semester, :presence => true
|
2013-04-11 21:38:41 -04:00
|
|
|
|
2013-04-12 13:12:29 -04:00
|
|
|
serialize :sections, Array
|
2013-04-12 13:53:25 -04:00
|
|
|
validate :section_check
|
|
|
|
|
2013-04-02 03:07:11 -04:00
|
|
|
|
2013-04-06 01:55:37 -04:00
|
|
|
scope :user_alerts, lambda { |user_id|
|
|
|
|
where('Alerts.user_id = ?', user_id)
|
|
|
|
}
|
|
|
|
|
2013-04-02 03:07:11 -04:00
|
|
|
def alerted?
|
|
|
|
@alerted
|
|
|
|
end
|
|
|
|
|
2013-04-03 19:47:38 -04:00
|
|
|
def course_name
|
2013-04-03 19:32:59 -04:00
|
|
|
"#{self.department} #{self.course}"
|
2013-04-02 03:07:11 -04:00
|
|
|
end
|
2013-04-12 13:53:25 -04:00
|
|
|
|
|
|
|
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
|
2013-04-02 02:43:01 -04:00
|
|
|
end
|