0
class User < ActiveRecord::Base
0
- # has a bunch of prefs
0
+ concerned_with :validation, :findability, :profile, :statistics
0
named_scope :musicians, {:conditions => ['assets_count > ?',0], :order => 'assets_count DESC', :include => :pic}
0
named_scope :activated, {:conditions => {:activation_code => nil}, :order => 'created_at DESC', :include => :pic}
0
@@ -21,8 +18,6 @@ class User < ActiveRecord::Base
0
belongs_to :facebook_account
0
- formats_attributes :bio
0
before_validation :geocode_address
0
@@ -40,272 +35,24 @@ class User < ActiveRecord::Base
0
has_many :top_tracks, :class_name => 'Asset', :limit => 10, :order => 'listens_count DESC'
0
- # Virtual attribute for the unencrypted password
0
- attr_accessor :password
0
- # These attributes can be changed via mass assignment
0
+ # The following attributes can be changed via mass assignment
0
attr_accessible :login, :email, :password, :password_confirmation, :website, :myspace,
0
:bio, :display_name, :itunes, :settings, :city, :country
0
- validates_presence_of :email
0
- validates_format_of :email, :with => Format::EMAIL
0
- validates_presence_of :password, :if => :password_required?
0
- validates_presence_of :password_confirmation, :if => :password_required?
0
- validates_length_of :password, :within => 5..40, :if => :password_required?
0
- validates_confirmation_of :password, :if => :password_required?
0
- validates_length_of :email, :within => 3..40
0
- validates_uniqueness_of :email, :case_sensitive => false
0
- validates_uniqueness_of :login
0
- validates_format_of :login, :with => Format::STRING, :message => ' must be lowercase and only made from numbers and letters'
0
- validates_length_of :login, :within => 3..40
0
- validates_length_of :display_name, :within => 3..50, :allow_blank => true
0
- validates_length_of :bio, :within => 0..500, :message => "can't be empty (or longer than 600 characters)", :on => :update
0
- validates_format_of :identity_url, :with => /^https?:\/\//i, :allow_nil => true
0
- validates_format_of :itunes, :with => /^phobos.apple.com\/WebObjects\/MZStore.woa\/wa\/viewPodcast\?id=/i, :allow_blank => true, :message => 'link must be a link to the itunes store'
0
- # Methods related to validation
0
- before_save { |u| u.display_name = u.login if u.display_name.blank? }
0
- before_validation { |u| u.identity_url = nil if u.identity_url.blank? }
0
- before_save :encrypt_password
0
before_create :make_first_user_admin, :make_activation_code
0
- def self.currently_online
0
- User.find(:all, :conditions => ["last_seen_at > ?", Time.now-5.hours])
0
- # Generates magic %LIKE% sql statements for all columns
0
- def self.conditions_by_like(value, *columns)
0
- columns = self.content_columns if columns.size==0
0
- columns = columns[0] if columns[0].kind_of?(Array)
0
- conditions = columns.map {|c|
0
- c = c.name if c.kind_of? ActiveRecord::ConnectionAdapters::Column
0
- "#{c} LIKE " + ActiveRecord::Base.connection.quote("%#{value}%")
0
- def has_public_playlists?
0
- playlists.public.size >= 1
0
- def has_as_favorite?(asset)
0
- favorite_asset_ids.include?(asset.id)
0
- def favorite_asset_ids
0
- Track.find(:all, :conditions => {:playlist_id => favorites}).collect(&:asset_id)
0
- @favorites ||= self.playlists.favorites.find(:first)
0
- pic && !pic.new_record?
0
- Gchart.line(:size => '420x150',:title => 'listens', :data => track_play_history, :axis_with_labels => 'r,x', :axis_labels => ["0|#{(track_play_history.max.to_f/2).round}|#{track_play_history.max}","30 days ago|15 days ago|Today"], :line_colors =>'cc3300', :background => '313327', :custom => 'chm=B,3d4030,0,0,0&chls=3,1,0&chg=25,50,1,0')
0
- def track_play_history
0
- track_plays.count(:all, :conditions => ['listens.created_at > ?',30.days.ago.at_midnight], :group => 'DATE(listens.created_at)').collect{|tp| tp[1]}
0
- "alonetone.com/#{login}"
0
- self.bio_html || BlueCloth::new(self.bio).to_html
0
- self[:website] || site
0
- self[:display_name] || login
0
- def self.dummy_pic(size)
0
- find(:first).dummy_pic(size)
0
- (self.listens_count.to_f / ((((Time.now - self.assets.find(:all, :limit => 1, :order => 'created_at').first.created_at) / 60 / 60 / 24 )).ceil)).ceil
0
- def number_of_tracks_listened_to
0
- Listen.count(:all, :order => 'count_all DESC', :conditions => {:listener_id => self})
0
- User.find(most_listened_to_user_ids(10), :include => :pic)
0
- when :album then 'no-pic-200.png'
0
- when :large then 'no-pic-125.png'
0
- when :small then 'no-pic-50.png'
0
- when :tiny then 'no-pic-25.png'
0
- when nil then 'no-pic-200.jpg'
0
- def avatar(size = nil)
0
- return dummy_pic(size) unless self.has_pic?
0
- self.pic.public_filename(size)
0
- def self.paginate_by_params(params)
0
- when 'recently_joined'
0
- self.activated.paginate(:all, :per_page => 15, :page => params[:page])
0
- when 'monster_uploaders'
0
- self.musicians.paginate(:all,:per_page => 15, :page => params[:page])
0
- when 'dedicated_listeners'
0
- @entries = WillPaginate::Collection.create((params[:page] || 1), 15) do |pager|
0
- # returns an array, like so: [User, number_of_listens]
0
- result = Listen.count(:all, :order => 'count_all DESC', :conditions => 'listener_id != ""', :group => :listener, :limit => pager.per_page, :offset => pager.offset)
0
- # inject the result array into the paginated collection:
0
- unless pager.total_entries
0
- # the pager didn't manage to guess the total count, do it manually
0
- pager.total_entries = Listen.count(:listener_id, :conditions => 'listens.listener_id != ""')
0
- @entries = WillPaginate::Collection.create((params[:page] || 1), 15) do |pager|
0
- distinct_users = Asset.find(:all, :select => 'DISTINCT user_id', :include => [:user => :pic], :order => 'assets.created_at DESC', :limit => pager.per_page, :offset => pager.offset)
0
- pager.replace(distinct_users.collect(&:user)) # only send back the users
0
- unless pager.total_entries
0
- # the pager didn't manage to guess the total count, do it manually
0
- pager.total_entries = User.musicians.count(:all, :conditions => 'assets_count > 0')
0
- self.recently_seen.paginate(:all, :page => params[:page], :per_page => 15)
0
- ###### login related shit
0
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
0
- def self.authenticate(login, password)
0
- u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
0
- u && u.authenticated?(password) ? u : nil
0
- # Activates the user in the database.
0
- self.activated_at = Time.now.utc
0
- self.activation_code = nil
0
- self[:plus_enabled] = true
0
- # Returns true if the user has just been activated.
0
- def authenticated?(password)
0
- crypted_password == encrypt(password)
0
- def self.search(query, options = {})
0
- with_scope :find => { :conditions => build_search_conditions(query) } do
0
- def self.build_search_conditions(query)
0
- query && ['LOWER(display_name) LIKE :q OR LOWER(login) LIKE :q', {:q => "%#{query}%"}]
0
def to_xml(options = {})
0
options[:except] ||= []
0
options[:except] << :email << :token << :token_expires_at << :crypted_password << :identity_url << :fb_user_id << :activation_code << :admin << :salt
0
- # Encrypts some data with the salt.
0
- def self.encrypt(password, salt)
0
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
0
- # Encrypts the password with the user salt
0
- self.class.encrypt(password, salt)
0
- self.token = Digest::SHA1.hexdigest(Time.now.to_s + crypted_password.to_s + rand(123456789).to_s).to_s
0
- # this is not currently honored
0
- self.token_expires_at = Time.now.utc+1.year
0
- alias_method :reset_token!, :reset_login_key!
0
- return unless city && country
0
- geo=GeoKit::Geocoders::MultiGeocoder.geocode([city, country].compact.join(', '))
0
- self.lat, self.lng = geo.lat,geo.lng if geo.success
0
- def most_listened_to_user_ids(limit = 10)
0
- self.listens.count(:track_owner, :group => 'track_owner_id',
0
- :order => 'count_track_owner DESC',
0
- :conditions => ['track_owner_id != ?',self.id]).collect(&:first)
0
def make_first_user_admin
0
self.admin = true if User.count == 0
0
- return if password.blank?
0
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
0
- self.crypted_password = encrypt(password)
0
- def password_required?
0
- crypted_password.blank? || !password.blank?
0
- def make_activation_code
0
- self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )