Created
November 13, 2011 17:23
-
-
Save shyam-habarakada/1362363 to your computer and use it in GitHub Desktop.
rails models with before_save callbacks and fixtures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: users | |
# | |
# id :integer(4) not null, primary key | |
# first_name :string(255) | |
# last_name :string(255) | |
# email :string(255) | |
# thumbnail :string(255) | |
# user_type_id :integer(4) | |
# created_at :datetime | |
# updated_at :datetime | |
# encrypted_password :string(255) | |
# salt :string(255) | |
# | |
require 'base64' | |
require 'digest' | |
class User < ActiveRecord::Base | |
attr_accessor :password | |
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :thumbnail, :user_type_id | |
before_save :update_salt_and_encrypted_password | |
# ... various members and methods deleted for brevity | |
private | |
def make_salt | |
# ... implementation details hidden | |
end | |
def encrypt(string) | |
# ... implementation details hidden | |
end | |
def update_salt_and_encrypted_password | |
# Make a new salt if the password has changed | |
self.salt = make_salt | |
self.encrypted_password = encrypt(password) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment