Sha256: 4eb9a753f489ab01e4a0f463184dc7e10dfe5b52457fd4cad7e699118097b6cd
Contents?: true
Size: 1.21 KB
Versions: 3
Compression:
Stored size: 1.21 KB
Contents
require 'digest/md5' class User < ActiveRecord::Base has_many :posts, :order => 'created_at DESC' has_many :drafts, :dependent => true has_many :links, :dependent => true has_many :images, :dependent => true has_one :stylesheet, :dependent => true has_one :face, :dependent => true validates_presence_of :username, :name validates_uniqueness_of :username validates_confirmation_of :password def before_save self.password = password.empty? ? self.class.find(id).password : Digest::MD5.hexdigest(password) end class << self def authenticate(username, password) find_first(["username = ? AND password = ?", username, Digest::MD5.hexdigest(password)]) end def top_posters(n=5) posters = find_by_sql("SELECT u.id FROM users u, posts p " + "WHERE u.id = p.user_id GROUP BY u.id " + "ORDER BY COUNT(p.id) DESC LIMIT #{n}").flatten posters.map { |p| find(p) } end end 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 end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
EliteJournal-1.9.400 | app/models/user.rb |
EliteJournal-1.9.401 | app/models/user.rb |
EliteJournal-1.9.403 | app/models/user.rb |