1
0
Fork 0
MSU-Course-Alerter/app/models/alert.rb

56 lines
1.6 KiB
Ruby
Raw Normal View History

2013-04-02 02:43:01 -04:00
class Alert < ActiveRecord::Base
attr_accessible :course, :department, :semester, :sections
attr_protected :user_id
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
serialize :sections, Array
2013-04-12 13:53:25 -04:00
validate :section_check
2013-04-02 03:07:11 -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
def course_name
"#{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