Module: Devise::Models::Confirmable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/devise/models/confirmable.rb
Overview
Confirmable is responsible to verify if an account is already confirmed to sign in, and to send emails with confirmation instructions. Confirmation instructions are sent to the user email after creating a record and when manually requested by a new confirmation instruction request.
Options
Confirmable adds the following options to devise_for:
* +allow_unconfirmed_access_for+: the time you want to allow the user to access his account
before confirming it. After this period, the user access is denied. You can
use this to let your user access some features of your application without
confirming the account, but blocking it after a certain period (ie 7 days).
By default allow_unconfirmed_access_for is zero, it means users always have to confirm to sign in.
* +reconfirmable+: requires changes to specific attribute (email by default) to be confirmed
(exactly the same way as initial account confirmation) to be applied.
Requires additional attribute (unconfirmed_email by default) db field to be setup
(t.reconfirmable in migrations).
Until confirmed new email is stored in unconfirmed email column, and copied to email
column on successful confirmation.
Examples
User.find(1).confirm! # returns true unless it's already confirmed
User.find(1).confirmed? # true/false
User.find(1).send_confirmation_instructions # manually send instructions
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#active_for_authentication? ⇒ Boolean
Overwrites active_for_authentication? for confirmation by verifying whether a user is active to sign in or not.
-
#confirm! ⇒ Object
Confirm a user by setting it’s confirmed_at to actual time.
-
#confirmable_attribute ⇒ Object
Defines confirmable/unconfirmed attribute accessors.
- #confirmable_attribute=(_confirmable_attribute) ⇒ Object
- #confirmable_attribute_changed? ⇒ Boolean
- #confirmable_attribute_was ⇒ Object
-
#confirmed? ⇒ Boolean
Verifies whether a user is confirmed or not.
- #headers_for(action) ⇒ Object
-
#inactive_message ⇒ Object
The message to be shown if the account is inactive.
- #pending_reconfirmation? ⇒ Boolean
-
#resend_confirmation_token ⇒ Object
Resend confirmation token.
-
#send_confirmation_instructions ⇒ Object
Send confirmation instructions by email.
-
#skip_confirmation! ⇒ Object
If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!.
-
#skip_reconfirmation! ⇒ Object
If you don’t want reconfirmation to be sent, neither a code to be generated, call skip_reconfirmation!.
- #unconfirmed_attribute ⇒ Object
- #unconfirmed_attribute=(_unconfirmed_attribute) ⇒ Object
Class Method Details
.required_fields(klass) ⇒ Object
40 41 42 43 44 |
# File 'lib/devise/models/confirmable.rb', line 40 def self.required_fields(klass) required_methods = [:confirmation_token, :confirmed_at, :confirmation_sent_at] required_methods << klass.unconfirmed_attribute if klass.reconfirmable required_methods end |
Instance Method Details
#active_for_authentication? ⇒ Boolean
Overwrites active_for_authentication? for confirmation by verifying whether a user is active to sign in or not. If the user is already confirmed, it should never be blocked. Otherwise we need to calculate if the confirm time has not expired for this user.
119 120 121 |
# File 'lib/devise/models/confirmable.rb', line 119 def active_for_authentication? super && (!confirmation_required? || confirmed? || confirmation_period_valid?) end |
#confirm! ⇒ Object
Confirm a user by setting it’s confirmed_at to actual time. If the user is already confirmed, add an error to the proper field. If the user is invalid add errors
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/devise/models/confirmable.rb', line 49 def confirm! pending_any_confirmation do self.confirmation_token = nil self.confirmed_at = Time.now.utc if self.class.reconfirmable && unconfirmed_attribute.present? skip_reconfirmation! self.confirmable_attribute = self.unconfirmed_attribute self.unconfirmed_attribute = nil # We need to validate in such cases to enforce e-mail uniqueness save(:validate => true) else save(:validate => false) end end end |
#confirmable_attribute ⇒ Object
Defines confirmable/unconfirmed attribute accessors
68 69 70 |
# File 'lib/devise/models/confirmable.rb', line 68 def confirmable_attribute send(self.class.confirmable_attribute) end |
#confirmable_attribute=(_confirmable_attribute) ⇒ Object
72 73 74 |
# File 'lib/devise/models/confirmable.rb', line 72 def confirmable_attribute=(_confirmable_attribute) send(self.class.confirmable_attribute.to_s + '=', _confirmable_attribute) end |
#confirmable_attribute_changed? ⇒ Boolean
88 89 90 |
# File 'lib/devise/models/confirmable.rb', line 88 def confirmable_attribute_changed? send(self.class.confirmable_attribute.to_s + '_changed?') end |
#confirmable_attribute_was ⇒ Object
76 77 78 |
# File 'lib/devise/models/confirmable.rb', line 76 def confirmable_attribute_was send(self.class.confirmable_attribute.to_s + '_was') end |
#confirmed? ⇒ Boolean
Verifies whether a user is confirmed or not
93 94 95 |
# File 'lib/devise/models/confirmable.rb', line 93 def confirmed? !!confirmed_at end |
#headers_for(action) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/devise/models/confirmable.rb', line 140 def headers_for(action) headers = super if action == :confirmation_instructions && pending_reconfirmation? headers[:to] = unconfirmed_attribute end headers end |
#inactive_message ⇒ Object
The message to be shown if the account is inactive.
124 125 126 |
# File 'lib/devise/models/confirmable.rb', line 124 def !confirmed? ? :unconfirmed : super end |
#pending_reconfirmation? ⇒ Boolean
97 98 99 |
# File 'lib/devise/models/confirmable.rb', line 97 def pending_reconfirmation? self.class.reconfirmable && unconfirmed_attribute.present? end |
#resend_confirmation_token ⇒ Object
Resend confirmation token. This method does not need to generate a new token.
111 112 113 |
# File 'lib/devise/models/confirmable.rb', line 111 def resend_confirmation_token pending_any_confirmation { send_confirmation_instructions } end |
#send_confirmation_instructions ⇒ Object
Send confirmation instructions by email
102 103 104 105 106 107 108 |
# File 'lib/devise/models/confirmable.rb', line 102 def send_confirmation_instructions self.confirmation_token = nil if reconfirmation_required? @reconfirmation_required = false generate_confirmation_token! if self.confirmation_token.blank? self.devise_mailer.confirmation_instructions(self).deliver end |
#skip_confirmation! ⇒ Object
If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!
130 131 132 |
# File 'lib/devise/models/confirmable.rb', line 130 def skip_confirmation! self.confirmed_at = Time.now.utc end |
#skip_reconfirmation! ⇒ Object
If you don’t want reconfirmation to be sent, neither a code to be generated, call skip_reconfirmation!
136 137 138 |
# File 'lib/devise/models/confirmable.rb', line 136 def skip_reconfirmation! @bypass_postpone = true end |
#unconfirmed_attribute ⇒ Object
80 81 82 |
# File 'lib/devise/models/confirmable.rb', line 80 def unconfirmed_attribute send(self.class.unconfirmed_attribute) end |
#unconfirmed_attribute=(_unconfirmed_attribute) ⇒ Object
84 85 86 |
# File 'lib/devise/models/confirmable.rb', line 84 def unconfirmed_attribute=(_unconfirmed_attribute) send(self.class.unconfirmed_attribute.to_s + '=', _unconfirmed_attribute) end |