Module: Devise::Models::Invitable

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_invitable/model.rb

Overview

Invitable is responsible to send emails with invitations. When an invitation is sent to an email, an account is created for it. An invitation has a link to set the password, as reset password from recoverable.

Configuration:

invite_for: the time you want the user will have to confirm the account after
            is invited. When invite_for is zero, the invitation won't expire.
            By default invite_for is 0.

Examples:

User.find(1).invited?             # true/false
User.send_invitation(:email => '[email protected]') # send invitation
User.accept_invitation!(:invitation_token => '...')   # accept invitation with a token
User.find(1).accept_invitation!   # accept invitation
User.find(1).resend_invitation!   # reset invitation status and send invitation again

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



23
24
25
26
27
# File 'lib/devise_invitable/model.rb', line 23

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

Instance Method Details

#accept_invitation!Object

Accept an invitation by clearing invitation token and confirming it if model is confirmable



31
32
33
34
35
36
# File 'lib/devise_invitable/model.rb', line 31

def accept_invitation!
  if self.invited?
    self.invitation_token = nil
    save(:validate => false)
  end
end

#invited?Boolean

Verifies whether a user has been invited or not

Returns:

  • (Boolean)


39
40
41
# File 'lib/devise_invitable/model.rb', line 39

def invited?
  !new_record? && !invitation_token.nil?
end

#resend_invitation!Object

Reset invitation token and send invitation again



53
54
55
56
57
58
59
60
# File 'lib/devise_invitable/model.rb', line 53

def resend_invitation!
  if new_record? || invited?
    self.skip_confirmation! if self.new_record? and self.respond_to? :skip_confirmation!
    generate_invitation_token
    save(:validate => false)
    send_invitation
  end
end

#send_invitationObject

Send invitation by email



44
45
46
47
48
49
50
# File 'lib/devise_invitable/model.rb', line 44

def send_invitation
  # don't know why token does not get generated unless I add these
  generate_invitation_token
  save(:validate => false)
  
  ::Devise::Mailer.invitation(self).deliver
end

#valid_invitation?Boolean

Verify whether a invitation is active or not. If the user has been invited, we need to calculate if the invitation time has not expired for this user, in other words, if the invitation is still valid.

Returns:

  • (Boolean)


65
66
67
# File 'lib/devise_invitable/model.rb', line 65

def valid_invitation?
  invited? && invitation_period_valid?
end