# An individual person # # Belongs to: # * Company # * Address # # Validation rules: # * Email must match regular expression EMAIL_REGEX # * Email must be unique # * First name must not be empty # * Last name must not be empty # * Gender must be set to 'M' or 'F' # class Person < ActiveRecord::Base belongs_to :company belongs_to :address has_many :tasks, :order => 'complete ASC, start DESC', :dependent => :nullify # Email regular expression; from http://regular-expressions.info/email.html EMAIL_REGEX = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i validates_format_of :email, :with => EMAIL_REGEX, :message => "Please supply a valid email address" validates_uniqueness_of :email, :message => "This email address already exists" validates_presence_of :first_name, :message => 'Please enter a first name' validates_presence_of :last_name, :message => 'Please enter a last name' validates_associated :address # Map single-letter representation of gender to full English word GENDERS = {'M' => 'male', 'F' => 'female'} validates_inclusion_of :gender, :in => GENDERS.keys, :message => "Please select 'M' or 'F' for the genre" # Return an array of all people in the database, ordered # by last_name ASC, then by first_name ASC def self.find_all_ordered find :all, :order => 'last_name, first_name' end def full_name out = (title.blank? ? '' : title + ' ') out + first_name + ' ' + last_name end end