module JamRuby class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation attr_accessor :updating_password self.primary_key = 'id' # re-visit later (used for multiple table inheritance w/ Musician model) # belongs_to :biz, :polymorphic => true has_many :connections, :class_name => "JamRuby::Connection" has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => "JamRuby::Friendship", :foreign_key => "friend_id" has_many :inverse_friends, :through => :inverse_friendships, :source => :user has_many :music_session_clients, :class_name => "JamRuby::MusicSessionClient" has_many :created_music_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::MusicSession" # sessions *created* by the user has_many :music_sessions, :through => :music_session_clients, :class_name => "JamRuby::MusicSession" 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} validates_length_of :password, minimum: 6, maximum: 100, :if => :should_validate_password? 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 def friends?(user) return self.friends.exists?(user) end def to_s return email unless email.nil? return name unless name.nil? return id end private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 end end end