2014-08-01 03:07:16 -07:00
|
|
|
class User < ActiveRecord::Base
|
|
|
|
# Include default devise modules. Others available are:
|
|
|
|
# :confirmable, :lockable, :timeoutable and :omniauthable
|
|
|
|
devise :database_authenticatable, :registerable,
|
2014-08-01 06:27:08 -07:00
|
|
|
:recoverable, :rememberable, :trackable,
|
|
|
|
:validatable, :authentication_keys => [:login]
|
|
|
|
|
|
|
|
# attr_accessor :login
|
|
|
|
|
2014-10-27 22:36:38 -07:00
|
|
|
has_many :questions, dependent: :destroy
|
|
|
|
has_many :answers, dependent: :destroy
|
|
|
|
has_many :comments, dependent: :destroy
|
|
|
|
|
2014-08-01 06:27:08 -07:00
|
|
|
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
|
|
|
|
|
|
|
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }
|
|
|
|
|
|
|
|
def login=(login)
|
|
|
|
@login = login
|
|
|
|
end
|
|
|
|
|
|
|
|
def login
|
|
|
|
@login || self.screen_name || self.email
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_first_by_auth_conditions(warden_conditions)
|
|
|
|
conditions = warden_conditions.dup
|
|
|
|
if login = conditions.delete(:login)
|
|
|
|
where(conditions).where(["lower(screen_name) = :value OR lower(email) = :value", { :value => login.downcase }]).first
|
|
|
|
else
|
|
|
|
where(conditions).first
|
|
|
|
end
|
|
|
|
end
|
2014-08-01 03:07:16 -07:00
|
|
|
end
|