Module: Devise::Models::SmsConfirmable

Extended by:
ActiveSupport::Concern
Includes:
SmsAuthenticatable
Defined in:
lib/devise_sms_confirmable/models/sms_confirmable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object



29
30
31
32
33
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 29

def self.required_fields(klass)
  required_methods = [:sms_confirmed_at, :sms_confirmation_sent_at, :sms_confirmation_token]
  required_methods << :unconfirmed_phone if klass.sms_reconfirmable
  required_methods
end

Instance Method Details

#active_for_authentication?Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 94

def active_for_authentication?

  super && (!sms_confirmation_required? || sms_confirmed? || sms_confirmation_period_valid?)
end

#generate_sms_confirmation_token!Object



103
104
105
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 103

def generate_sms_confirmation_token!
  generate_sms_confirmation_token && save(validate: false)
end

#initialize(*args, &block) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 20

def initialize(*args, &block)
  @bypass_sms_confirmation_postpone = false
  @skip_sms_reconfirmation_in_callback = false
  @sms_reconfirmation_required = false
  @skip_sms_confirmation_notification = false
  @raw_sms_confirmation_token = nil
  super
end

#pending_sms_reconfirmation?Boolean

override Devise::Models::Confirmable#pending_sms_reconfirmation?

Returns:

  • (Boolean)


108
109
110
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 108

def pending_sms_reconfirmation?
  self.class.sms_reconfirmable && unconfirmed_phone.present?
end

#resend_sms_confirmation_instructionsObject

Resend confirmation token. Regenerates the token if the period is expired.



88
89
90
91
92
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 88

def resend_sms_confirmation_instructions
  pending_any_sms_confirmation do
    send_sms_confirmation_instructions
  end
end

#send_phone_changed_notificationObject



99
100
101
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 99

def send_phone_changed_notification
  send_sms_devise_notification(:phone_changed, to: phone_before_last_save)
end

#send_sms_confirmation_instructionsObject



77
78
79
80
81
82
83
84
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 77

def send_sms_confirmation_instructions
  unless @raw_sms_confirmation_token
    generate_sms_confirmation_token!
  end

  opts = pending_sms_reconfirmation? ? { to: unconfirmed_phone } : { }
  send_sms_devise_notification(:confirmation_instructions, @raw_sms_confirmation_token, opts)
end

#send_sms_reconfirmation_instructionsObject



65
66
67
68
69
70
71
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 65

def send_sms_reconfirmation_instructions
  @sms_reconfirmation_required = false

  unless @skip_confirmation_notification
    send_sms_confirmation_instructions
  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!



114
115
116
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 114

def skip_sms_confirmation!
  self.sms_confirmed_at = Time.now.utc
end

#skip_sms_confirmation_notification!Object



73
74
75
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 73

def skip_sms_confirmation_notification!
  @skip_confirmation_notification = true
end

#skip_sms_reconfirmation!Object



118
119
120
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 118

def skip_sms_reconfirmation!
  @bypass_sms_confirmation_postpone = true
end

#sms_confirm(args = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 35

def sms_confirm(args={})
  pending_any_sms_confirmation do
    if sms_confirmation_period_expired?
      self.errors.add(:phone, :sms_confirmation_period_expired,
                      period: Devise::TimeInflector.time_ago_in_words(self.class.sms_confirm_within.ago))
      return false
    end

    self.sms_confirmed_at = Time.now.utc

    saved = if pending_sms_reconfirmation?
              skip_sms_reconfirmation!
              self.phone = unconfirmed_phone
              self.unconfirmed_phone = nil

              save(validate: true, context: args[:context])
            else
              save(validate: args[:ensure_valid] == true, context: args[:context])
            end

    after_sms_confirmation if saved
    saved
  end
end

#sms_confirmed?Boolean

Verifies whether a user is sms-confirmed or not

Returns:

  • (Boolean)


61
62
63
# File 'lib/devise_sms_confirmable/models/sms_confirmable.rb', line 61

def sms_confirmed?
  !!sms_confirmed_at
end