2012-08-06 03:01:00 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
|
|
|
|
|
attr_accessible :name, :email, :password, :password_confirmation
|
2012-08-29 13:21:30 +00:00
|
|
|
attr_accessor :updating_password
|
2012-08-18 18:48:43 +00:00
|
|
|
|
2012-08-22 12:57:50 +00:00
|
|
|
self.primary_key = 'id'
|
|
|
|
|
|
2012-08-29 13:21:30 +00:00
|
|
|
has_many :jam_session_members, :class_name => "JamRuby::JamSessionMember"
|
|
|
|
|
has_many :created_jam_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::JamSession" # sessions *created* by the user
|
|
|
|
|
has_many :jam_sessions, :through => :jam_session_members, :class_name => "JamRuby::JamSession"
|
2012-08-06 03:01:00 +00:00
|
|
|
|
|
|
|
|
has_secure_password
|
|
|
|
|
|
|
|
|
|
before_save { |user| user.email = email.downcase }
|
|
|
|
|
before_save :create_remember_token
|
|
|
|
|
|
|
|
|
|
validates :name, presence: true, length: {maximum: 50}
|
|
|
|
|
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
|
|
|
|
|
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX},
|
|
|
|
|
uniqueness: {case_sensitive: false}
|
2012-08-29 13:21:30 +00:00
|
|
|
validates_length_of :password, minimum: 6, maximum: 100, :if => :should_validate_password?
|
2012-08-06 03:01:00 +00:00
|
|
|
|
2012-08-29 13:21:30 +00:00
|
|
|
validates_presence_of :password_confirmation, :if => :should_validate_password?
|
|
|
|
|
#validates :password_confirmation, presence: true
|
|
|
|
|
validates_confirmation_of :password, :if => :should_validate_password?
|
|
|
|
|
|
|
|
|
|
def should_validate_password?
|
|
|
|
|
updating_password || new_record?
|
|
|
|
|
end
|
2012-08-26 18:28:08 +00:00
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
|
return email unless email.nil?
|
|
|
|
|
return name unless name.nil?
|
|
|
|
|
return id
|
|
|
|
|
end
|
2012-08-29 13:21:30 +00:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
def create_remember_token
|
|
|
|
|
self.remember_token = SecureRandom.urlsafe_base64
|
|
|
|
|
end
|
2012-08-06 03:01:00 +00:00
|
|
|
end
|
2012-08-22 03:07:01 +00:00
|
|
|
end
|