Files

Class/Module Index [+]

Quicksearch

Devise::Models::Confirmable

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:

* +confirm_within+: 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 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

Public Instance Methods

active_for_authentication?() click to toggle source

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.

# File lib/devise/models/confirmable.rb, line 62
def active_for_authentication?
  super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
end
confirm!() click to toggle source

Confirm a user by setting its confirmed_at to actual time. If the user is already confirmed, add en error to email field

# File lib/devise/models/confirmable.rb, line 34
def confirm!
  unless_confirmed do
    self.confirmation_token = nil
    self.confirmed_at = Time.now.utc
    save(:validate => false)
  end
end
confirmed?() click to toggle source

Verifies whether a user is confirmed or not

# File lib/devise/models/confirmable.rb, line 43
def confirmed?
  !!confirmed_at
end
inactive_message() click to toggle source

The message to be shown if the account is inactive.

# File lib/devise/models/confirmable.rb, line 67
def inactive_message
  !confirmed? ? :unconfirmed : super
end
resend_confirmation_token() click to toggle source

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

# File lib/devise/models/confirmable.rb, line 54
def resend_confirmation_token
  unless_confirmed { send_confirmation_instructions }
end
send_confirmation_instructions() click to toggle source

Send confirmation instructions by email

# File lib/devise/models/confirmable.rb, line 48
def send_confirmation_instructions
  generate_confirmation_token! if self.confirmation_token.nil?
  self.devise_mailer.confirmation_instructions(self).deliver
end
skip_confirmation!() click to toggle source

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

# File lib/devise/models/confirmable.rb, line 73
def skip_confirmation!
  self.confirmed_at = Time.now.utc
end

Protected Instance Methods

after_password_reset() click to toggle source
# File lib/devise/models/confirmable.rb, line 130
def after_password_reset
  super
  confirm! unless confirmed?
end
confirmation_period_valid?() click to toggle source

Checks if the confirmation for the user is within the limit time. We do this by calculating if the difference between today and the confirmation sent date does not exceed the confirm in time configured. Confirm_within is a model configuration, must always be an integer value.

Example:

# confirm_within = 1.day and confirmation_sent_at = today
confirmation_period_valid?   # returns true

# confirm_within = 5.days and confirmation_sent_at = 4.days.ago
confirmation_period_valid?   # returns true

# confirm_within = 5.days and confirmation_sent_at = 5.days.ago
confirmation_period_valid?   # returns false

# confirm_within = 0.days
confirmation_period_valid?   # will always return false
# File lib/devise/models/confirmable.rb, line 103
def confirmation_period_valid?
  confirmation_sent_at && confirmation_sent_at.utc >= self.class.confirm_within.ago
end
confirmation_required?() click to toggle source

Callback to overwrite if confirmation is required or not.

# File lib/devise/models/confirmable.rb, line 80
def confirmation_required?
  !confirmed?
end
generate_confirmation_token() click to toggle source

Generates a new random token for confirmation, and stores the time this token is being generated

# File lib/devise/models/confirmable.rb, line 120
def generate_confirmation_token
  self.confirmed_at = nil
  self.confirmation_token = self.class.confirmation_token
  self.confirmation_sent_at = Time.now.utc
end
generate_confirmation_token!() click to toggle source
# File lib/devise/models/confirmable.rb, line 126
def generate_confirmation_token!
  generate_confirmation_token && save(:validate => false)
end
unless_confirmed() click to toggle source

Checks whether the record is confirmed or not, yielding to the block if it's already confirmed, otherwise adds an error to email.

# File lib/devise/models/confirmable.rb, line 109
def unless_confirmed
  unless confirmed?
    yield
  else
    self.errors.add(:email, :already_confirmed)
    false
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.