class Alumnu < ActiveRecord::Base
  has_many :donations
  # VALIDATION RULES #
  # Every alumnus must have a first name and a last name.
  # If the second line of an address is entered then the first line must also have text.
  # Zip code should be limited to numbers and dashes.
  # State should be limited to 2-letter capitalized abbreviations. 
  # Year of graduation must be after 1900.
  # Email address must contain text followed by an @ followed by some amount of text, a period, and additional text.
  # All fields are optional except for first name, last name, and year of graduation.  
  
  def validate
    # THIS WILL ENFORCE THAT BOTH First_Name and Last_Name WILL BE ENTERED
    errors.add_on_empty %w( First_Name Last_Name)  
  end 
  # VALIDATION RULE 2 IS A COMPOSITE CONDITION WHERE VERIFICATION OF ONE FIELD DEPENDS ON ANOTHER
  validates_presence_of :Home_Address_Line_1,
  :unless => Proc.new {|ad| ad.Home_Address_Line_2.nil? },
  :message => "can't be blank - if Line 2 is filled"
  
  # THE FOLLOWING 3 LINES ENFORCE THAT IF A VALUE IS ENTERED, IT CONFORMS TO THE VALIDATION RULES
  validates_format_of :Email_Address, :with => /^((\s*)|([a-zA-Z0-9]+@[A-Za-z0-9]+\.[A-Za-z0-9]{1,}))$/, 
  :message =>" should be: text@text.text"   
  validates_format_of :State, :with => /^((\s*)|([A-Z][A-Z]))$/, 
  :message => "is 2 capital letters or space"
  validates_format_of :Home_Zip, :with => /^(([-0-9]+)|(\s*))$/, 
  :message => "is optional, but if specified must be digits & dashes " 
  # THE FOLLOWING ENFORCES 3 THINGS: Year_Of_Graduation IS ENTERED, IS NUMERIC AND IS > 1900
  validates_numericality_of :Year_Of_Graduation, :greater_than => 1900   
end
