Skip to content

Instantly share code, notes, and snippets.

@volkanunsal
Created August 1, 2010 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volkanunsal/503884 to your computer and use it in GitHub Desktop.
Save volkanunsal/503884 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
class User < ActiveRecord::Base
has_one :company
attr_accessor :password
# Validations
validates_presence_of :email
validates_presence_of :password, :if => :password_required
validates_presence_of :password_confirmation, :if => :password_required
validates_length_of :password, :within => 4..40, :if => :password_required
validates_confirmation_of :password, :if => :password_required
validates_length_of :email, :within => 3..100
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
# Callbacks
before_save :generate_password
##
# This method is for authentication purpose
#
def self.authenticate(email, password)
account = first(:conditions => { :email => email }) if email.present?
account && account.password_clean == password ? account : nil
end
##
# This method is used to retrieve the original password.
#
def password_clean
crypted_password.decrypt(salt)
end
private
def generate_password
return if password.blank?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
self.crypted_password = password.encrypt(self.salt)
end
def password_required
crypted_password.blank? || !password.blank?
end
end
%p.message
Please fill in the form below. You can also use Facebook or OpenID to register.
- form_for :user, '/register', :id => 'register-form' do |f|
%p
= f.label :email
= f.error_message_on :email
= f.text_field :email
%p
= f.label :password
= f.error_message_on :password
= f.password_field :password
%p
= f.label "Re-type password"
= f.error_message_on :password_confirmation
= f.password_field :password_confirmation
= f.submit "Signup", :class => 'button'
get :new, :map=>"/register" do
if params["without_layout"] == "true" then @without_layout = true else @without_layout = false end
@user=User.new
render "user/register", :layout => !@without_layout
end
post :create, :map=>"/register" do
@user = User.new(params[:user])
if @user.save
flash[:notice] = 'You have successfully logged in.'
redirect "/user"
else
flash[:notice] = 'There was a problem with your registration. Please try again.'
redirect "/register"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment