File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
activemodel/lib/active_model Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ module ClassMethods
45
45
#
46
46
# ==== Examples
47
47
#
48
- # Using Active Record, which automatically includes ActiveModel::SecurePassword:
48
+ # ===== Using Active Record ( which automatically includes ActiveModel::SecurePassword)
49
49
#
50
50
# # Schema: User(name:string, password_digest:string, recovery_password_digest:string)
51
51
# class User < ActiveRecord::Base
@@ -77,6 +77,29 @@ module ClassMethods
77
77
#
78
78
# user.authenticate("vr00m") # => false, old password
79
79
# user.authenticate("nohack4u") # => user
80
+ #
81
+ # ===== Conditionally requiring a password
82
+ #
83
+ # class Account
84
+ # include ActiveModel::SecurePassword
85
+ #
86
+ # attr_accessor :is_guest, :password_digest
87
+ #
88
+ # has_secure_password
89
+ #
90
+ # def errors
91
+ # errors = super
92
+ # errors.delete(:password, :blank) if is_guest
93
+ # errors
94
+ # end
95
+ # end
96
+ #
97
+ # account = Account.new
98
+ # account.valid? # => false, password required
99
+ #
100
+ # account.is_guest = true
101
+ # account.valid? # => true
102
+ #
80
103
def has_secure_password ( attribute = :password , validations : true )
81
104
# Load bcrypt gem only when has_secure_password is used.
82
105
# This is to avoid ActiveModel (and by extension the entire framework)
You can’t perform that action at this time.
0 commit comments