Skip to content

Commit

Permalink
Merge pull request #45754 from jonathanhefner/has_secure_password-con…
Browse files Browse the repository at this point in the history
…ditional-password-example

Add example of conditionally requiring a password [ci-skip]
  • Loading branch information
jonathanhefner committed Aug 3, 2022
2 parents 3967766 + f53e3ad commit 3f81469
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion activemodel/lib/active_model/secure_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module ClassMethods
#
# ==== Examples
#
# Using Active Record, which automatically includes ActiveModel::SecurePassword:
# ===== Using Active Record (which automatically includes ActiveModel::SecurePassword)
#
# # Schema: User(name:string, password_digest:string, recovery_password_digest:string)
# class User < ActiveRecord::Base
Expand Down Expand Up @@ -77,6 +77,29 @@ module ClassMethods
#
# user.authenticate("vr00m") # => false, old password
# user.authenticate("nohack4u") # => user
#
# ===== Conditionally requiring a password
#
# class Account
# include ActiveModel::SecurePassword
#
# attr_accessor :is_guest, :password_digest
#
# has_secure_password
#
# def errors
# errors = super
# errors.delete(:password, :blank) if is_guest
# errors
# end
# end
#
# account = Account.new
# account.valid? # => false, password required
#
# account.is_guest = true
# account.valid? # => true
#
def has_secure_password(attribute = :password, validations: true)
# Load bcrypt gem only when has_secure_password is used.
# This is to avoid ActiveModel (and by extension the entire framework)
Expand Down

0 comments on commit 3f81469

Please sign in to comment.