# == Schema Information # Schema version: 20090222005710 # # Table name: users # # id :integer(4) not null, primary key # login :string(40) # first_name :string(100) # last_name :string(100) # email :string(100) # crypted_password :string(40) # salt :string(40) # created_at :datetime # updated_at :datetime # remember_token :string(40) # remember_token_expires_at :datetime # city_id :integer(4) # avatar_file_name :string(255) # avatar_content :string(255) # avatar_file_size :integer(4) # sex :string(255) # status :string(255) # occupation :string(255) # birthday :date # require 'digest/sha1' class User < ActiveRecord::Base include Authentication include Authentication::ByPassword include Authentication::ByCookieToken validates_presence_of :login validates_length_of :login, :within => 3..40 validates_uniqueness_of :login validates_format_of :login, :with => Authentication.login_regex, :message => Authentication.bad_login_message validates_presence_of :email validates_length_of :email, :within => 6..100 #r@a.wk validates_uniqueness_of :email validates_format_of :email, :with => Authentication.email_regex, :message => Authentication.bad_email_message belongs_to :city has_many :pops has_many :places, :through => :pops, :uniq => true has_many :feeds has_many :feed_items, :through => :feeds has_many :sent_messages, :foreign_key => "sender_id", :class_name => "Message" has_many :received_messages, :foreign_key => "receiver_id", :class_name => "Message" has_many :friendships has_many :friends, :through => :friendships, :class_name => "User" has_many :wishlists has_many :wished_places, :through => :wishlists, :class_name => "Place", :uniq => true has_many :social_networks has_attached_file :avatar, :styles => { :large => "70x70", :thumb => "45x45" } # HACK HACK HACK -- how to do attr_accessible from here? # prevents a user from submitting a crafted form that bypasses activation # anything else you want your user to change should be added here. attr_accessible :login, :email, :first_name, :last_name, :password, :password_confirmation, :birthday, :occupation, :city def to_param self.login end def name(length = :long) # :long means first_name last_name if self.first_name && self.last_name && (length == :long) [self.first_name, self.last_name].join ' ' elsif self.first_name && (length == :short) self.first_name else self.login end end def age (Time.now.year - self.birthday.year).round if self.birthday end def locations array = [self.city] array << self.city.state if self.city.state array << self.city.country array end def popped_in? place self.pops.count(:conditions => {:place_id => place.id}) > 0 end # Suggested places to find a random number of places which dont belong to user def suggested_places(limit=5) own_places = self.places.find(:all, :select => "`places`.id") places_query = own_places.empty? ? "NULL" : own_places.collect(&:id).join(',') Place.all(:conditions => "`places`.id NOT IN (#{places_query})", :limit => limit, :order => "RAND()") end # Authenticates a user by their login name and unencrypted password. Returns the user or nil. # # uff. this is really an authorization, not authentication routine. # We really need a Dispatch Chain here or something. # This will also let us return a human error message. # def self.authenticate(login, password) return nil if login.blank? || password.blank? u = find_by_login(login) # need to get the salt u && u.authenticated?(password) ? u : nil end def login=(value) write_attribute :login, (value ? value.downcase : nil) end def email=(value) write_attribute :email, (value ? value.downcase : nil) end protected end