From 8d43db7c7516de9bdd2f4916fbd07e63c32fbe12 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Fri, 12 Apr 2013 13:53:25 -0400 Subject: [PATCH] Basic validation on sections --- app/models/alert.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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