Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authenticate(username, password) ⇒ Object



21
22
23
# File 'app/models/user.rb', line 21

def authenticate(username, password)
  find_first(["username = ? AND password = ?", username, Digest::MD5.hexdigest(password)])
end

.top_posters(n = 5) ⇒ Object



25
26
27
28
29
# File 'app/models/user.rb', line 25

def top_posters(n=5)
  find_by_sql ["SELECT u.*, COUNT(p.id) FROM #{table_name} u, " +
               "#{Post.table_name} p WHERE u.id = p.user_id "   +
               "GROUP BY u.id ORDER_BY post_count DESC LIMIT ?", n]
end

Instance Method Details

#before_saveObject



16
17
18
# File 'app/models/user.rb', line 16

def before_save
  self.password = password.empty? ? self.class.find(id).password : Digest::MD5.hexdigest(password)
end

#last_postObject



32
33
34
# File 'app/models/user.rb', line 32

def last_post
  self.posts.find_first(nil, 'created_at DESC') || self.posts.build('subject' => 'Example Post', 'rendered' => '<p>This is what a post looks like.</p>', 'created_at' => Time.now)
end