Skip to content

fakingfantastic/devise_invitable

 
 

Repository files navigation

DeviseInvitable

It adds support to devise for send invitations by email (it requires to be authenticated) and accept the invitation setting the password.

DeviseInvitable currently only support Rails 3, if you want to use it with Rails 2.3 you must install version 0.2.3

Installation for Rails ~> 3.0 and Devise ~> 1.1

Install DeviseInvitable gem, it will also install dependencies (such as devise and warden):

gem install devise_invitable

Add DeviseInvitable to your Gemfile (and Devise if you weren’t using them):

gem 'devise',           '~> 1.1.3'
gem 'devise_invitable', '~> 0.3.4'

Automatic installation

Run the following generator to add DeviseInvitable’s configuration option in the Devise configuration file (config/initializers/devise.rb):

rails generate devise_invitable:install

When you are done, you are ready to add DeviseInvitable to any of your Devise models using the following generator:

rails generate devise_invitable MODEL

Replace MODEL by the class name you want to add DeviseInvitable, like User, Admin, etc. This will add the :invitable flag to your model’s Devise modules. The generator will also create a migration file (if your ORM support them). Continue reading this file to understand exactly what the generator produces and how to use it.

Manual installation

Follow the walkthrough for Devise and after it’s done, follow this walkthrough.

Add :invitable to the devise call in your model (we’re assuming here you already have a User model with some Devise modules):

class User < ActiveRecord::Base
  devise :database_authenticatable, :confirmable, :invitable
end

Add t.invitable to your Devise model migration:

create_table :users do
  ...
  t.invitable
  ...
end
add_index :users, :invitation_token

or for a model that already exists, define a migration to add DeviseInvitable to your model:

change_table :users do |t|
  t.string   :invitation_token, :limit => 60
  t.datetime :invitation_sent_at
  t.index    :invitation_token
end

# Allow null encrypted_password
change_column_null :users, :encrypted_password, true
# Allow null password_salt (add it if you are using Devise's encryptable module)
change_column_null :users, :password_salt, true

Model configuration

DeviseInvitable adds a new configuration option:

  • invite_for: The period the generated invitation token is valid, after this period, the invited resource won’t be able to accept the invitation. When invite_for is 0 (the default), the invitation won’t expire.

You can set this configuration option in the Devise initializer as follow:

# ==> Configuration for :invitable
# The period the generated invitation token is valid, after
# this period, the invited resource won't be able to accept the invitation.
# When invite_for is 0 (the default), the invitation won't expire.
# config.invite_for = 2.weeks

or directly as parameters to the devise method:

devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks

For more details, see config/initializers/devise.rb (after you invoked the “devise_invitable:install” generator described above).

Configuring views

All the views are packaged inside the gem. If you’d like to customize the views, invoke the following generator and it will copy all the views to your application:

rails generate devise_invitable:views

You can also use the generator to generate scoped views:

rails generate devise_invitable:views users

Please refer to Devise’s README for more information about views.

Usage

Send an invitation

To send an invitation to a user, use the invite! class method. :email must be present in the parameters hash. You can also include other attributes in the hash. The record will not be validated.

User.invite!(:email => "new_user@example.com", :name => "John Doe")
# => an invitation email will be sent to new_user@example.com

Accept an invitation

To accept an invitation with a token use the accept_invitation! class method. :invitation_token must be present in the parameters hash. You can also include other attributes in the hash.

User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")

Integration in a Rails application

Since the invitations controller take care of all the creation/acceptation of an invitation, in most cases you wouldn’t call the invite! and accept_invitation! methods directly. Instead, in your views, put a link to new_user_invitation_path or new_invitation_path(:user) or even /users/invitation/new to prepare and send an invitation (to a user in this example). After an invitation is created and sent, the inviter will be redirected to after_sign_in_path_for(resource_name).

The invitation email includes a link to accept the invitation that looks like this: /users/invitation/accept?invitation_token=abcd123. When clicked, the invited must set a password in order to accept its invitation. Note that if the invitation_token is not present or not valid, the invited is redirected to after_sign_out_path_for(resource_name). You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks. More on Devise’s README, “Controller filters and helpers” section.

Controller filter

InvitationsController uses authenticate_inviter! filter to restrict who can send invitations. You can override this method in your ApplicationController.

Default behavior requires authentication of the same resource as the invited one. For example, if your model User is invitable, it will allow all authenticated users to send invitations to other users.

You would have a User model which is configured as invitable and an Admin model which is not. If you want to allow only admins to send invitations, simply overwrite the authenticate_inviter! method as follow:

class ApplicationController < ActionController::Base
protected
  def authenticate_inviter!
    authenticate_admin!
  end
end

I18n

DeviseInvitable uses flash messages with I18n with the flash keys :send_instructions, :invitation_token_invalid and :updated. To customize your app, you can modify the generated locale file:

en:
  devise:
    invitations:
      send_instructions: 'An invitation email has been sent to %{email}.'
      invitation_token_invalid: 'The invitation token provided is not valid!'
      updated: 'Your password was set successfully. You are now signed in.'

You can also create distinct messages based on the resource you’ve configured using the singular name given in routes:

en:
  devise:
    invitations:
      user:
        send_instructions: 'A new user invitation has been sent to %{email}.'
        invitation_token_invalid: 'Your invitation token is not valid!'
        updated: 'Welcome on board! You are now signed in.'

The DeviseInvitable mailer uses the same pattern as Devise to create mail subject messages:

en:
  devise:
    mailer:
      invitation_instructions:
        subject: 'You got an invitation!'
        user_subject: 'You got a user invitation!'

Take a look at the generated locale file (in config/locales/devise_invitable.en.yml) to check all available messages.

Other ORMs

DeviseInvitable supports ActiveRecord and Mongoid, like Devise.

Contributors

Check them all at:

github.com/scambra/devise_invitable/contributors

Special thanks to rymai for the Rails 3 support, his fork was a great help.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 Sergio Cambra. See LICENSE for details.

About

An invitation strategy for devise

Resources

License

Stars

Watchers

Forks

Packages

No packages published