Module: Devise::Models::MobileConfirmable

Defined in:
lib/devise_mobile_confirmable/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#can_send_mobile_confirmation_token?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/devise_mobile_confirmable/model.rb', line 49

def can_send_mobile_confirmation_token?
  seconds_to_unlock_mobile_confirmation_token <= 0
end

#change_mobile(new_mobile) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/devise_mobile_confirmable/model.rb', line 10

def change_mobile(new_mobile)
  errors.add(self.class.mobile_field, :blank) if new_mobile.blank?
  errors.add(self.class.mobile_field, :indifferent) if mobile == new_mobile
  errors.add(:base, :retry_later) unless can_send_mobile_confirmation_token?
  return false unless errors.empty?

  self.unconfirmed_mobile = new_mobile
  if mobile_confirmation_token_expired?
    self.mobile_confirmation_token = generate_mobile_confirmation_token
    self.mobile_confirmation_failure = 0
  end
  self.mobile_confirmation_sent_at = Time.now
  return false unless save
  send_mobile_confirmation_token
  errors.empty?
end

#confirm_mobile_token(token) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/devise_mobile_confirmable/model.rb', line 27

def confirm_mobile_token(token)
  errors.add(:mobile_confirmation_token, :blank) if token.blank?
  errors.add(:mobile_confirmation_token, :expired) if mobile_confirmation_token_expired?
  return false unless errors.empty?
  if mobile_confirmation_token != token
    update(:mobile_confirmation_failure => mobile_confirmation_failure + 1)
    errors.add(:mobile_confirmation_token, :invalid)
    return false
  end
  self.mobile = unconfirmed_mobile
  self.unconfirmed_mobile = nil
  self.mobile_confirmation_token = nil
  self.mobile_confirmed_at = Time.now
  self.mobile_confirmation_failure = 0
  save
end

#mobileObject



58
59
60
61
62
63
64
# File 'lib/devise_mobile_confirmable/model.rb', line 58

def mobile
  if self.class.mobile_field == :mobile
    super
  else
    self.send(self.class.mobile_field)
  end
end

#mobile=(value) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/devise_mobile_confirmable/model.rb', line 66

def mobile=(value)
  if self.class.mobile_field == :mobile
    super
  else
    self.send(self.class.mobile_field.to_s + '=', value)
  end
end

#mobile_confirmation_token_expired?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/devise_mobile_confirmable/model.rb', line 53

def mobile_confirmation_token_expired?
  mobile_confirmation_token.nil? ||
  mobile_confirmation_failure >= self.class.max_mobile_confirmation_failure && self.class.max_mobile_confirmation_failure != 0
end

#mobile_confirmed?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/devise_mobile_confirmable/model.rb', line 6

def mobile_confirmed?
  mobile_confirmed_at.present?
end

#seconds_to_unlock_mobile_confirmation_tokenObject



44
45
46
47
# File 'lib/devise_mobile_confirmable/model.rb', line 44

def seconds_to_unlock_mobile_confirmation_token
  return 0 if mobile_confirmation_sent_at.nil?
  self.class.throttle_mobile_confirmation_token - (Time.now - mobile_confirmation_sent_at).to_i
end