Module: Devise::Models::Approvable

Includes:
Activatable
Defined in:
lib/approvable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/approvable.rb', line 7

def self.included(base)
  base.class_eval do
    extend ClassMethods

    before_create :generate_approval_token, :if => :approval_required?
    # after_create  :send_approval_instructions unless approved?
  end
end

Instance Method Details

#active?Boolean

Overwrites active? from Devise::Models::Activatable for confirmation by verifying whether an 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)


46
47
48
# File 'lib/approvable.rb', line 46

def active?
  super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
end

#approve!Object



67
68
69
70
71
72
# File 'lib/approvable.rb', line 67

def approve!
  self.is_approved = true
  self.approval_token = nil
  send_confirmation_instructions
  save(:validate => false)
end

#approved?Boolean

Verifies whether a user is confirmed or not

Returns:

  • (Boolean)


75
76
77
# File 'lib/approvable.rb', line 75

def approved?
  self.is_approved
end

#confirm!Object

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



18
19
20
21
22
23
24
# File 'lib/approvable.rb', line 18

def confirm!
  unless_confirmed do
    self.confirmation_token = nil
    self.confirmed_at = Time.now
    save(false)
  end
end

#confirmed?Boolean

Verifies whether a user is confirmed or not

Returns:

  • (Boolean)


27
28
29
# File 'lib/approvable.rb', line 27

def confirmed?
  !new_record? && !confirmed_at.nil?
end

#inactive_messageObject

The message to be shown if the account is inactive.



51
52
53
# File 'lib/approvable.rb', line 51

def inactive_message
  !confirmed? ? :unconfirmed : super
end

#resend_confirmation_tokenObject

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



38
39
40
# File 'lib/approvable.rb', line 38

def resend_confirmation_token
  unless_confirmed { send_confirmation_instructions }
end

#send_approval_instructionsObject

Send confirmation instructions by email



85
86
87
88
# File 'lib/approvable.rb', line 85

def send_approval_instructions
  generate_approval_token if self.approval_token.nil?
  ::DeviseMailer.deliver_approval_instructions(self)
end

#send_confirmation_instructionsObject

Send confirmation instructions by email



32
33
34
35
# File 'lib/approvable.rb', line 32

def send_confirmation_instructions
  generate_confirmation_token if self.confirmation_token.nil?
  ::DeviseMailer.deliver_confirmation_instructions(self)
end

#skip_approval!Object



79
80
81
82
# File 'lib/approvable.rb', line 79

def skip_approval!
  self.is_approved = true
  @skip_approval = true
end

#skip_confirm_and_approve!Object



62
63
64
65
# File 'lib/approvable.rb', line 62

def skip_confirm_and_approve!
  skip_confirmation!
  skip_approval!
end

#skip_confirmation!Object

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



57
58
59
60
# File 'lib/approvable.rb', line 57

def skip_confirmation!
  self.confirmed_at  = Time.now
  @skip_confirmation = true
end