module Devise::Models::Confirmable

def active_for_authentication?

calculate if the confirm time has not expired for this user.
is already confirmed, it should never be blocked. Otherwise we need to
by verifying whether a user is active to sign in or not. If the user
Overwrites active_for_authentication? for confirmation
def active_for_authentication?
  super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
end

def after_password_reset

def after_password_reset
  super
  confirm! unless confirmed?
end

def confirm!

is already confirmed, add en error to email field
Confirm a user by setting its confirmed_at to actual time. If the user
def confirm!
  unless_confirmed do
    self.confirmation_token = nil
    self.confirmed_at = Time.now.utc
    save(:validate => false)
  end
end

def confirmation_period_valid?


confirmation_period_valid? # will always return false
# confirm_within = 0.days

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

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

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

Example:

Confirm_within is a model configuration, must always be an integer value.
confirmation sent date does not exceed the confirm in time configured.
We do this by calculating if the difference between today and the
Checks if the confirmation for the user is within the limit time.
def confirmation_period_valid?
  confirmation_sent_at && confirmation_sent_at.utc >= self.class.confirm_within.ago
end

def confirmation_required?

Callback to overwrite if confirmation is required or not.
def confirmation_required?
  !confirmed?
end

def confirmed?

Verifies whether a user is confirmed or not
def confirmed?
  !!confirmed_at
end

def generate_confirmation_token

this token is being generated
Generates a new random token for confirmation, and stores the time
def generate_confirmation_token
  self.confirmed_at = nil
  self.confirmation_token = self.class.confirmation_token
  self.confirmation_sent_at = Time.now.utc
end

def generate_confirmation_token!

def generate_confirmation_token!
  generate_confirmation_token && save(:validate => false)
end

def inactive_message

The message to be shown if the account is inactive.
def inactive_message
  !confirmed? ? :unconfirmed : super
end

def resend_confirmation_token

Resend confirmation token. This method does not need to generate a new token.
def resend_confirmation_token
  unless_confirmed { send_confirmation_instructions }
end

def send_confirmation_instructions

Send confirmation instructions by email
def send_confirmation_instructions
  generate_confirmation_token! if self.confirmation_token.nil?
  self.devise_mailer.confirmation_instructions(self).deliver
end

def skip_confirmation!

to be generated, call skip_confirmation!
If you don't want confirmation to be sent on create, neither a code
def skip_confirmation!
  self.confirmed_at = Time.now.utc
end

def unless_confirmed

if it's already confirmed, otherwise adds an error to email.
Checks whether the record is confirmed or not, yielding to the block
def unless_confirmed
  unless confirmed?
    yield
  else
    self.errors.add(:email, :already_confirmed)
    false
  end
end