module Devise::Models::Confirmable

def self.included(base)

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

def active?

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 an user is active to sign in or not. If the user
Overwrites active? from Devise::Models::Activatable for confirmation
def active?
  super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
end

def confirm!

is already confirmed, add en error to email field
Confirm a user by setting it's confirmed_at to actual time. If the user
def confirm!
  unless_confirmed do
    self.confirmation_token = nil
    self.confirmed_at = Time.now
    save(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_in 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?
  !@skip_confirmation
end

def confirmed?

Verifies whether a user is confirmed or not
def confirmed?
  !new_record? && !confirmed_at.nil?
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 = Devise.friendly_token
  self.confirmation_sent_at = Time.now.utc
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?
  ::DeviseMailer.deliver_confirmation_instructions(self)
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
  @skip_confirmation = true
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.class.add_error_on(self, :email, :already_confirmed)
    false
  end
end