Skip to content

Instantly share code, notes, and snippets.

@shyam-habarakada
Created November 13, 2011 17:23
Show Gist options
  • Save shyam-habarakada/1362363 to your computer and use it in GitHub Desktop.
Save shyam-habarakada/1362363 to your computer and use it in GitHub Desktop.
rails models with before_save callbacks and fixtures
# == 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