iownbey (owner)

Revisions

gist: 80920 Download_button fork
public
Public Clone URL: git://gist.github.com/80920.git
Embed All Files: show embed
user.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# == 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