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

148 lines
4.6 KiB
Ruby

module JamRuby
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :admin, :musician
attr_accessor :updating_password
self.primary_key = 'id'
# connections (websocket-gateway)
has_many :connections, :class_name => "JamRuby::Connection"
# friend requests
has_many :friend_requests, :class_name => "JamRuby::FriendRequest"
# instruments
has_many :musician_instruments
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
# bands
has_many :band_musicians
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
# followers / followings
# favorites
# friends
has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id"
has_many :friends, :through => :friendships, :class_name => "JamRuby::User"
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"
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 => :connections, :class_name => "JamRuby::MusicSession"
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
after_save :limit_to_five_instruments
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 online
@online = !self.connections.nil? && self.connections.size > 0
return @online
end
def photo_url
# TODO: move image path to config
@photo_url = "http://www.jamkazam.com/images/users/photos/#{self.id}.gif";
end
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
# helper method for creating / updating a User
def self.save(params)
if params[:id].nil?
user = User.new()
else
user = User.find(params[:id])
end
# 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]
end
# 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
MusicianInstrument.delete_all(["user_id = ?", user.id])
# 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
puts "INSTRUMENT ID #{instrument[:id]}"
mu.instrument_id = instrument[:id]
puts "proficiency level #{instrument[:proficiency_level]}"
mu.proficiency_level = instrument[:proficiency_level]
user.musician_instruments << mu
end
end
end
user.updated_at = Time.now.getutc
user.save
return user
end
def limit_to_five_instruments
if instruments.count > 5
errors.add(:instruments, "No more than 5 instruments are allowed.")
end
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
end