class Assignment < ActiveRecord::Base
  has_many :igrades
  has_many :ggrades
  def validate
    # THIS WILL ENFORCE THAT BOTH First_Name and Last_Name WILL BE ENTERED  
    errors.add_on_empty %w( description due_date)
    if self.due_date < Date.today
      errors.add("Assignment due date cannot be in the past. It")
    end
  end
  # ORDERING OF ASSIGNMENTS SHOULD BE UNIQUE (1-100). IT'S FAIR TO ASSUME
  # THAT ONE COURSE WILL NOT EXCEED 100 ASSIGNMENTS.
  validates_uniqueness_of :ordering,
  :message => "This Assignment ordering exists already"
  def before_destroy()
    # WE DO NOT ALLOW TO DELETE ASSIGNMENT IF GRADES EXIST FOR IT (I or G)
    if Igrade.exists?(:assignment_id => "#{self.id}") ||
       Ggrade.exists?(:assignment_id => "#{self.id}")
      errors.add_to_base(
      "Can't delete graded assignment, Edit it or delete all grades first")
      false
    end  
  end  
end
