Skip to content

Instantly share code, notes, and snippets.

@rbarazi
Created March 9, 2010 05:33
Show Gist options
  • Save rbarazi/326271 to your computer and use it in GitHub Desktop.
Save rbarazi/326271 to your computer and use it in GitHub Desktop.
[Beginning Rails 3] Listing 5-37. Current User Model, in app/models/user.rb
require 'digest'
class User < ActiveRecord::Base
attr_accessor :password
validates :email, :uniqueness => true,
:length => { :within => 5..50 },
:format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }
validates :password, :confirmation => true,
:length => { :within => 4..20 },
:presence => true,
:if => :password_required?
has_one :profile
has_many :articles, :order => 'published_at DESC, title ASC',
:dependent => :nullify
has_many :replies, :through => :articles, :source => :comments
before_save :encrypt_new_password
def self.authenticate(email, password)
user = find_by_email(email)
return user if user && user.authenticated?(password)
end
def authenticated?(password)
self.hashed_password == encrypt(password)
end
protected
def encrypt_new_password
return if password.blank?
self.hashed_password = encrypt(password)
end
def password_required?
hashed_password.blank? || password.present?
end
def encrypt(string)
Digest::SHA1.hexdigest(string)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment