box-o-sand/RubyFun/cookbook/015/hodgepodge/app/models/user.rb
Dan Buch 0a9428093c Add 'RubyFun/' from commit 'b01c6826131196ba58b5288a3182f2526c89c249'
git-subtree-dir: RubyFun
git-subtree-mainline: a04a502787
git-subtree-split: b01c682613
2013-01-09 23:50:14 -05:00

33 lines
789 B
Ruby

require 'sha1'
class User < ActiveRecord::Base
attr_accessor :password
attr_protected :hashed_password
validates_uniqueness_of :username
validates_confirmation_of :password, :if => lambda { |user|
user.new_record? or not user.password.blank? }
validates_length_of :password, :within => 5..40, :if => lambda { |user|
user.new_record? or not user.password.blank? }
def self.hashed(str)
SHA1.new(str).to_s
end
def self.authenticate(user_info)
user = find_by_username(user_info[:username])
if user && user.hashed_password == hashed(user_info[:password])
return user
end
end
private
before_save :update_password
def update_password
if not password.blank?
self.hashed_password = self.class.hashed(password)
end
end
end