Sha256: ea83ebcf5f38000531f1398b987e2b3e56a2fb82862db5a99c1185ac1dd51613

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 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
  validates_inclusion_of    :posts_per_page, :in => 1..25
  
  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)
      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
  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

2 entries across 2 versions & 1 rubygems

Version Path
EliteJournal-1.9.480 app/models/user.rb
EliteJournal-1.9.492 app/models/user.rb