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

268 lines
8.1 KiB
Ruby
Raw Normal View History

module JamRuby
class User < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
2012-11-11 04:23:38 +00:00
attr_accessible :first_name, :last_name, :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-11-13 07:40:06 +00:00
# authorizations (for facebook, etc -- omniauth)
has_many :user_authorizations
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-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-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"
2012-11-11 04:23:38 +00:00
has_many :inverse_band_followings, :through => :band_followings, :class_name => "JamRuby::Band", :foreign_key => "band_id"
2012-11-04 22:54:53 +00:00
2012-11-04 13:33:51 +00:00
# favorites (needs Recording model)
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-11-03 19:32:27 +00:00
# connections / music sessions
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"
# 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"
2012-11-09 06:51:17 +00:00
# This causes the authenticate method to be generated (among other stuff)
has_secure_password
before_save { |user| user.email = email.downcase }
2012-11-04 03:49:36 +00:00
before_save :create_remember_token
after_save :limit_to_five_instruments
2012-11-11 04:23:38 +00:00
validates :first_name, presence: true, length: {maximum: 50}
validates :last_name, presence: true, length: {maximum: 50}
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
2012-11-09 06:29:05 +00:00
@online ||= !self.connections.nil? && self.connections.size > 0
2012-10-14 02:18:20 +00:00
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
def location
# TODO: implement a single string version of location;
# this will be indexed into elasticsearch and returned in search
return "Austin, TX"
end
2012-10-07 04:57:23 +00:00
def should_validate_password?
updating_password || new_record?
end
2012-11-11 04:23:38 +00:00
#def name
# return self.first_name + " " + self.last_name
#end
2012-10-07 18:02:26 +00:00
def friends?(user)
return self.friends.exists?(user)
end
2012-11-06 04:47:50 +00:00
def friend_count
return self.friends.size
end
def follower_count
return self.followers.size
end
def following_count
return self.followings.size
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
# 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-11 04:23:38 +00:00
# first name
unless params[:first_name].nil?
user.first_name = params[:first_name]
end
# last name
unless params[:last_name].nil?
user.last_name = params[:last_name]
end
# username
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
# 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
2012-11-06 02:55:08 +00:00
# city
unless params[:city].nil?
user.city = params[:city]
end
# state
unless params[:state].nil?
user.state = params[:state]
end
# country
unless params[:country].nil?
user.country = params[:country]
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
# 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
mu.instrument_id = instrument[:id]
2012-11-03 19:32:27 +00:00
mu.priority = instrument[:priority]
mu.proficiency_level = instrument[:proficiency_level]
user.musician_instruments << mu
end
end
2012-10-29 10:45:47 +00:00
end
user.updated_at = Time.now.getutc
user.save
2012-10-29 10:45:47 +00:00
return user
end
def limit_to_five_instruments
if instruments.count > 5
errors.add(:instruments, "No more than 5 instruments are allowed.")
end
end
### Elasticsearch/Tire integration ###
#
# Define the name based on the environment
# We wouldn't like to erase dev data during
# test runs!
#
index_name("#{Environment.mode}-#{Environment.application}-users")
def to_indexed_json
{
2012-11-11 04:23:38 +00:00
:first_name => first_name,
:last_name => last_name,
:photo_url => photo_url,
:location => location,
:musician => musician
}.to_json
end
class << self
def create_search_index
Tire.index(User.index_name) do
create(
:settings => Search.index_settings,
:mappings => {
"jam_ruby/user" => {
:properties => {
:photo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false},
:location => { :type => :string },
2012-11-11 04:23:38 +00:00
:first_name => { :type => :string, :boost => 100 },
:last_name => { :type => :string, :boost => 100 },
:musician => { :type => :boolean, :index => :not_analyzed, :include_in_all => false}
}
}
}
)
end
end
def delete_search_index
search_index.delete
end
def search_index
Tire.index(User.index_name)
end
end
### Elasticsearch/Tire integration
2012-10-07 04:57:23 +00:00
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
end