Module: Devise::Models::Confirmable

Includes:
Activatable
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, after updating it’s email and also when manually requested by a new confirmation instruction request. Whenever the user update it’s email, his account is automatically unconfirmed, it means it won’t be able to sign in again without confirming the account again through the email that was sent.

Configuration:

confirm_within: the time you want the user will have to confirm it's account
                without blocking his access. When confirm_within is zero, the
                user won't be able to sign in without confirming. 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 confirm_within is
                zero, it means users always have to confirm to sign in.

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
User.find(1).resend_confirmation! # generates a new token and resent it

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/devise/models/confirmable.rb', line 34

def self.included(base)
  base.class_eval do
    extend ClassMethods

    before_create :generate_confirmation_token, :if => :confirmation_required?
    after_create  :send_confirmation_instructions, :if => :confirmation_required?
  end
end

Instance Method Details

#active?Boolean

Overwrites active? from Devise::Models::Activatable for confirmation by verifying whether an 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.

Returns:

  • (Boolean)


72
73
74
# File 'lib/devise/models/confirmable.rb', line 72

def active?
  super && (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 en error to email field



45
46
47
48
49
50
51
# File 'lib/devise/models/confirmable.rb', line 45

def confirm!
  unless_confirmed do
    self.confirmation_token = nil
    self.confirmed_at = Time.now
    save(false)
  end
end

#confirmed?Boolean

Verifies whether a user is confirmed or not

Returns:

  • (Boolean)


54
55
56
# File 'lib/devise/models/confirmable.rb', line 54

def confirmed?
  !new_record? && !confirmed_at.nil?
end

#inactive_messageObject

The message to be shown if the account is inactive.



77
78
79
# File 'lib/devise/models/confirmable.rb', line 77

def inactive_message
  !confirmed? ? :unconfirmed : super
end

#resend_confirmation_tokenObject

Resend confirmation token. This method does not need to generate a new token.



64
65
66
# File 'lib/devise/models/confirmable.rb', line 64

def resend_confirmation_token
  unless_confirmed { send_confirmation_instructions }
end

#send_confirmation_instructionsObject

Send confirmation instructions by email



59
60
61
# File 'lib/devise/models/confirmable.rb', line 59

def send_confirmation_instructions
  ::DeviseMailer.deliver_confirmation_instructions(self)
end

#skip_confirmation!Object

If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!



83
84
85
86
# File 'lib/devise/models/confirmable.rb', line 83

def skip_confirmation!
  self.confirmed_at  = Time.now
  @skip_confirmation = true
end