/app/models/user.rb
https://github.com/mcgournj/sample_app · Ruby · 66 lines · 44 code · 19 blank · 3 comment · 6 complexity · 94e88c387a7717279f9b4509a1833337 MD5 · raw file
- class User < ActiveRecord::Base
- attr_accessor :password
- attr_accessible(:name, :email, :password, :password_confirmation)
- has_many :microposts, :dependent => :destroy
- email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
- validates :name, :presence => true,
- :length => { :maximum => 50 }
- validates :email, :presence => true,
- :format => { :with => email_regex},
- :uniqueness => { :case_sensitive => false }
- #Automatically create the virtual attribure 'password_confirmation'.
- validates :password, :presence => true,
- :confirmation => true,
- :length => { :within => 6..20 }
- before_save :encrypt_password
- #return true if the user's password matches the submitted password.
- def has_password?(submitted_password)
- encrypted_password == encrypt(submitted_password)
- end
- def self.authenticate(email, submitted_password)
- user = find_by_email(email)
- return nil if user.nil?
- return user if user.has_password?(submitted_password)
- end
- def self.authenticate_with_salt(id, cookie_salt)
- user = find_by_id(id)
- (user && user.salt == cookie_salt) ? user : nil
- end
- def feed
- # This is a preliminary - filled out in chapter 12.
- Micropost.where("user_id = ?", id)
- end
- private
- def encrypt_password
- self.salt = make_salt if new_record?
- self.encrypted_password = encrypt(password)
- end
- def encrypt(string)
- secure_hash("#{salt}--#{string}")
- end
- def make_salt
- secure_hash("#{Time.now.utc}--#{password}")
- end
- def secure_hash(string)
- Digest::SHA2.hexdigest(string)
- end
- end