2012-08-06 03:01:00 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
|
2012-11-04 03:49:36 +00:00
|
|
|
attr_accessible :name, :email, :password, :password_confirmation
|
2012-10-30 00:56:51 +00:00
|
|
|
attr_accessor :updating_password
|
2012-08-18 18:48:43 +00:00
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
self.primary_key = 'id'
|
2012-10-02 05:02:02 +00:00
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# connections (websocket-gateway)
|
2012-10-02 05:02:02 +00:00
|
|
|
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
|
2012-10-30 05:42:16 +00:00
|
|
|
has_many :musician_instruments
|
|
|
|
|
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
|
2012-10-29 10:45:47 +00:00
|
|
|
|
|
|
|
|
# bands
|
2012-10-30 05:42:16 +00:00
|
|
|
has_many :band_musicians
|
|
|
|
|
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
|
2012-10-14 02:18:20 +00:00
|
|
|
|
2012-11-04 13:33:51 +00:00
|
|
|
# followers
|
|
|
|
|
has_many :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id"
|
|
|
|
|
has_many :inverse_followers, :through => :followers, :source => :user, :class_name => "JamRuby::User", :foreign_key => "follower_id"
|
2012-11-03 13:54:55 +00:00
|
|
|
|
2012-11-04 22:54:53 +00:00
|
|
|
# user followings
|
2012-11-04 13:33:51 +00:00
|
|
|
has_many :followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "follower_id"
|
|
|
|
|
has_many :inverse_followings, :through => :followings, :source => :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
|
|
|
|
|
|
2012-11-04 22:54:53 +00:00
|
|
|
# band followings
|
|
|
|
|
has_many :band_followings, :class_name => "JamRuby::BandFollower", :foreign_key => "follower_id"
|
|
|
|
|
has_many :inverse_band_followings, :through => :band_followings, :foreign_key => "band_id"
|
|
|
|
|
|
2012-11-04 13:33:51 +00:00
|
|
|
# favorites (needs Recording model)
|
2012-11-03 13:54:55 +00:00
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# friends
|
2012-10-26 10:33:39 +00:00
|
|
|
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"
|
2012-10-26 10:33:39 +00:00
|
|
|
has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :class_name => "JamRuby::User"
|
2012-11-03 19:32:27 +00:00
|
|
|
|
|
|
|
|
# connections / music sessions
|
2012-10-03 03:50:23 +00:00
|
|
|
has_many :created_music_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::MusicSession" # sessions *created* by the user
|
2012-11-02 06:51:52 +00:00
|
|
|
has_many :music_sessions, :through => :connections, :class_name => "JamRuby::MusicSession"
|
2012-10-26 10:33:39 +00:00
|
|
|
|
2012-11-04 03:02:11 +00:00
|
|
|
# invitations
|
2012-10-26 10:33:39 +00:00
|
|
|
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"
|
2012-08-06 03:01:00 +00:00
|
|
|
|
|
|
|
|
has_secure_password
|
|
|
|
|
|
|
|
|
|
before_save { |user| user.email = email.downcase }
|
2012-11-04 03:49:36 +00:00
|
|
|
before_save :create_remember_token
|
2012-11-03 15:38:00 +00:00
|
|
|
|
|
|
|
|
after_save :limit_to_five_instruments
|
2012-08-06 03:01:00 +00:00
|
|
|
|
|
|
|
|
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-10-07 04:57:23 +00:00
|
|
|
validates_presence_of :password_confirmation, :if => :should_validate_password?
|
2012-08-29 13:21:30 +00:00
|
|
|
#validates :password_confirmation, presence: true
|
2012-10-07 04:57:23 +00:00
|
|
|
validates_confirmation_of :password, :if => :should_validate_password?
|
2012-08-29 13:21:30 +00:00
|
|
|
|
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-08-26 18:28:08 +00:00
|
|
|
|
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
|
2012-08-29 13:21:30 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-03 13:54:55 +00:00
|
|
|
# helper method for creating / updating a User
|
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
|
|
|
|
|
|
2012-11-03 13:54:55 +00:00
|
|
|
# name
|
|
|
|
|
unless params[:name].nil?
|
|
|
|
|
user.name = params[:name]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# email
|
|
|
|
|
unless params[:email].nil?
|
|
|
|
|
user.email = params[:email]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# password
|
|
|
|
|
unless params[:password].nil?
|
|
|
|
|
user.password = params[:password]
|
2012-10-29 10:45:47 +00:00
|
|
|
end
|
2012-11-03 13:54:55 +00:00
|
|
|
|
|
|
|
|
# password confirmation
|
|
|
|
|
unless params[:password_confirmation].nil?
|
|
|
|
|
user.password_confirmation = params[:password_confirmation]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# musician flag
|
|
|
|
|
unless params[:musician].nil?
|
|
|
|
|
user.musician = params[:musician]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# instruments
|
|
|
|
|
unless params[:instruments].nil?
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
# delete all instruments for this user first
|
2012-11-03 19:32:27 +00:00
|
|
|
unless user.id.nil? || user.id.length == 0
|
|
|
|
|
MusicianInstrument.delete_all(["user_id = ?", user.id])
|
|
|
|
|
end
|
2012-11-03 13:54:55 +00:00
|
|
|
|
|
|
|
|
# loop through each instrument in the array and save to the db
|
|
|
|
|
params[:instruments].each do |instrument|
|
|
|
|
|
mu = MusicianInstrument.new()
|
|
|
|
|
mu.user_id = user.id
|
2012-11-03 15:38:00 +00:00
|
|
|
mu.instrument_id = instrument[:id]
|
2012-11-03 19:32:27 +00:00
|
|
|
mu.priority = instrument[:priority]
|
2012-11-03 13:54:55 +00:00
|
|
|
mu.proficiency_level = instrument[:proficiency_level]
|
|
|
|
|
user.musician_instruments << mu
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-10-29 10:45:47 +00:00
|
|
|
end
|
2012-11-03 13:54:55 +00:00
|
|
|
|
|
|
|
|
user.updated_at = Time.now.getutc
|
|
|
|
|
user.save
|
2012-10-29 10:45:47 +00:00
|
|
|
return user
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-03 15:38:00 +00:00
|
|
|
def limit_to_five_instruments
|
|
|
|
|
if instruments.count > 5
|
|
|
|
|
errors.add(:instruments, "No more than 5 instruments are allowed.")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
private
|
2012-10-02 05:02:02 +00:00
|
|
|
def create_remember_token
|
|
|
|
|
self.remember_token = SecureRandom.urlsafe_base64
|
2012-11-04 03:02:11 +00:00
|
|
|
end
|
2012-08-06 03:01:00 +00:00
|
|
|
end
|
2012-08-22 03:07:01 +00:00
|
|
|
end
|