Module: Devise::Models::SmsActivable

Extended by:
ActiveSupport::Concern
Defined in:
lib/models/sms_activable.rb

Overview

SmsActivable is responsible to verify if an account is already confirmed to sign in, and to send sms with confirmation instructions. Confirmation instructions are sent to the user phone after creating a record and when manually requested by a new confirmation instruction request.

Options

Confirmable adds the following options to devise_for:

* +sms_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 0 days, so the user must confirm before entering.
  If you want to allow user to use parts of the site and block others override sms_confirmation_required? 
  and check manually on selected pages using the require_sms_activated! helper or sms_confirmed? property on record

Examples

User.find(1).sms_confirm!      # returns true unless it's already confirmed
User.find(1).sms_confirmed?    # true/false
User.find(1).send_sms_token # manually send token

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Overwrites active? from Devise::Models::Activatable for sms 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.

Returns:

  • (Boolean)


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

def active?
  super && !sms_confirmation_required? || confirmed_sms? || confirmation_sms_period_valid?
end

#confirm_sms!Object

Confirm a user by setting it’s sms_confirmed_at to actual time. If the user is already confirmed, add en error to email field



38
39
40
41
42
43
44
# File 'lib/models/sms_activable.rb', line 38

def confirm_sms!
  unless_sms_confirmed do
    self.sms_confirmation_token = nil
    self.sms_confirmed_at = Time.now
    save(:validate => false)
  end
end

#confirmed_sms?Boolean

Verifies whether a user is sms-confirmed or not

Returns:

  • (Boolean)


47
48
49
# File 'lib/models/sms_activable.rb', line 47

def confirmed_sms?
  !!sms_confirmed_at
end

#inactive_messageObject

The message to be shown if the account is inactive.



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

def inactive_message
  !confirmed_sms? ? I18n.t(:"devise.sms_activations.unconfirmed_sms") : super
end

#resend_sms_tokenObject

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



63
64
65
# File 'lib/models/sms_activable.rb', line 63

def resend_sms_token
  unless_sms_confirmed { send_sms_token }
end

#send_sms_tokenObject

Send confirmation token by sms



52
53
54
55
56
57
58
59
60
# File 'lib/models/sms_activable.rb', line 52

def send_sms_token
  if(self.phone?)
    generate_sms_token! if self.generate_sms_token.nil?
    ::Devise.sms_sender.send_sms(self.phone, I18n.t(:"devise.sms_activations.sms_body", :sms_confirmation_token => self.sms_confirmation_token, :default => self.sms_confirmation_token))
  else
    self.errors.add(:sms_confirmation_token, :no_phone_associated)
    false
  end
end

#skip_sms_confirmation!Object

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



83
84
85
# File 'lib/models/sms_activable.rb', line 83

def skip_sms_confirmation!
  self.sms_confirmed_at = Time.now
end