jam-cloud/lib/jam_ruby/models/user.rb

101 lines
3.4 KiB
Ruby
Raw Normal View History

module JamRuby
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
2012-10-30 00:56:51 +00:00
attr_accessor :updating_password
2012-10-07 04:57:23 +00:00
self.primary_key = 'id'
2012-10-29 10:45:47 +00:00
# connections (websocket-gateway)
has_many :connections, :class_name => "JamRuby::Connection"
2012-10-01 21:27:32 +00:00
2012-10-29 10:45:47 +00:00
# friend requests
2012-10-14 02:18:20 +00:00
has_many :friend_requests, :class_name => "JamRuby::FriendRequest"
2012-10-29 10:45:47 +00:00
# instruments
has_many :musician_instruments
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
2012-10-29 10:45:47 +00:00
# bands
has_many :band_musicians
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
2012-10-14 02:18:20 +00:00
2012-10-29 10:45:47 +00:00
# friends
has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id"
has_many :friends, :through => :friendships, :class_name => "JamRuby::User"
2012-10-01 21:27:32 +00:00
has_many :inverse_friendships, :class_name => "JamRuby::Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :class_name => "JamRuby::User"
2012-10-29 10:45:47 +00:00
# music sessions
2012-10-07 04:57:23 +00:00
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"
2012-10-29 10:45:47 +00:00
# invitations
has_many :received_invitations, :foreign_key => "receiver_id", :inverse_of => :receiver, :class_name => "JamRuby::Invitation"
has_many :sent_invitations, :foreign_key => "sender_id", :inverse_of => :sender, :class_name => "JamRuby::Invitation"
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?
2012-10-07 04:57:23 +00:00
validates_presence_of :password_confirmation, :if => :should_validate_password?
#validates :password_confirmation, presence: true
2012-10-07 04:57:23 +00:00
validates_confirmation_of :password, :if => :should_validate_password?
2012-10-14 02:18:20 +00:00
def online
@online = !self.connections.nil? && self.connections.size > 0
return @online
end
2012-10-29 10:45:47 +00:00
def photo_url
# TODO: move image path to config
@photo_url = "http://www.jamkazam.com/images/users/photos/#{self.id}.gif";
end
2012-10-07 04:57:23 +00:00
def should_validate_password?
updating_password || new_record?
end
2012-10-07 18:02:26 +00:00
def friends?(user)
return self.friends.exists?(user)
end
2012-10-07 04:57:23 +00:00
def to_s
return email unless email.nil?
return name unless name.nil?
return id
end
2012-10-29 10:45:47 +00:00
def self.save(params)
if params[:id].nil?
user = User.new()
else
user = User.find(params[:id])
end
@user = User.find(params[:id])
@user.email = params[:email]
@user.musician = params[:musician]
if @user.musician
@user.instruments = params[:instruments]
@user.bands = params[:bands]
end
@user.save
return user
end
2012-10-07 04:57:23 +00:00
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
end