Home

Awesome

Memberships

This is an example that shows how to implement a multi-tenant application (i.e. a single application that supports multiple customers) that allows each user to create and belong to multiple organizations in Rails 5. Users can have different privileges to different organizations (e.g. be an administrator of an organization and a normal user of another).

For this example we are assuming three roles: owner, admin and user.

Use cases

Owners and administrators have the following additional use cases:

Implementation details

We are using Devise but we are adding two additional models:

We also created a virtual model called Invitation that will allow us to manage the invitations.

The controllers are the following:

Finally, we added a current_organization_id column to User that represents the organization that the user is currently working on.

For the views we used [Bootstrap 4].

We used SQLite for the database.

Finally, we wrote some integration tests. Ideally, you would want to refactor some of these tests as model and controller tests.

Setup

The requirements for this project are the following:

First, clone the project with the following command:

$ git clone git@github.com:germanescobar/memberships.git

Then, install the dependencies with the following commands:

$ bundle
$ yarn install

Load the schema:

$ rails db:schema:load

That's it! Run the server:

$ rails s

Go to http://localhost:3000/ on your favorite browser. Sign up and create an organization.

FAQ

How is this different from devise_invitable?

We checked devise_invitable but it seems targeted to application-wide invitations, not scoped like we did here.

How can I contribute to this project?

You can open an issue first so we can discuss the change/improvement. You can also create a Pull Request once the issue is discussed, or for one of the opened issues.