Module: InviteOnly::ControllerExtensions

Defined in:
lib/invite_only/controller_extensions.rb

Instance Method Summary collapse

Instance Method Details

#enable_invite_onlyObject

use this method to enable invite only helper method in your controllers/views



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/invite_only/controller_extensions.rb', line 5

def enable_invite_only
  self.class_eval do
    helper_method :create_invite_code_for

    #use this helper to generate the code you need to hand off to the person
    #identifier can be anything that IDs the person i.e. Email|Username etc...
    #identifier: type=string
    def create_invite_code_for(identifier)
      #if email has been invited before, just update with new code.
      invite = Invite.find_by_identifier(identifier) || Invite.new(identifier:identifier)
      #code is only valid for the identifier provided and can only be used once.
      invite.is_used=false
      invite.code=SecureRandom.urlsafe_base64
      invite.save! #no need to validate
      return invite.code
    end
  end
end