Skip to content

Commit

Permalink
has_secure_password shouldn't validate password_digest. It should als…
Browse files Browse the repository at this point in the history
…o take options to turn validations off.
  • Loading branch information
Erich Menge committed May 8, 2012
1 parent ef9dd27 commit 0e1e527
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions activemodel/lib/active_model/secure_password.rb
Expand Up @@ -6,8 +6,9 @@ module ClassMethods
# Adds methods to set and authenticate against a BCrypt password. # Adds methods to set and authenticate against a BCrypt password.
# This mechanism requires you to have a password_digest attribute. # This mechanism requires you to have a password_digest attribute.
# #
# Validations for presence of password, confirmation of password (using # Validations for presence of password on create, confirmation of password (using
# a "password_confirmation" attribute) are automatically added. # a "password_confirmation" attribute) are automatically added.
# If you wish to turn off validations, pass 'validations: false' as an argument.
# You can add more validations by hand if need be. # You can add more validations by hand if need be.
# #
# You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use has_secure_password: # You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use has_secure_password:
Expand All @@ -31,16 +32,20 @@ module ClassMethods
# user.authenticate("mUc3m00RsqyRe") # => user # user.authenticate("mUc3m00RsqyRe") # => user
# User.find_by_name("david").try(:authenticate, "notright") # => false # User.find_by_name("david").try(:authenticate, "notright") # => false
# User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user # User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user
def has_secure_password def has_secure_password(options = {})
# Load bcrypt-ruby only when has_secure_password is used. # Load bcrypt-ruby only when has_secure_password is used.
# This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library. # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
gem 'bcrypt-ruby', '~> 3.0.0' gem 'bcrypt-ruby', '~> 3.0.0'
require 'bcrypt' require 'bcrypt'


attr_reader :password attr_reader :password


validates_confirmation_of :password if options.fetch(:validations, true)
validates_presence_of :password_digest validates_confirmation_of :password
validates_presence_of :password, :on => :create
end

before_create { raise "Password digest missing on new record" if password_digest.blank? }


include InstanceMethodsOnActivation include InstanceMethodsOnActivation


Expand Down

0 comments on commit 0e1e527

Please sign in to comment.