class Admin < ActiveRecord::Base
  has_many :history_status_companies
  has_many :request_companies
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :active
  attr_accessor :ignore_password
  
  validates :email, 
            :uniqueness => true,   
            :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
  
  validates_presence_of :name, :email
  validates_presence_of :password_confirmation, :if => :validate_password?
  validates_confirmation_of :password, :if => :validate_password?
  
  def validate_password?
    return true if ignore_password == nil || ignore_password == false
    return false
  end
  
protected
  def self.find_for_database_authentication(conditions)
    where(conditions)
    .where(["active = true"])
    .first
  end

end
