Tenanfy

This is opinionated use of the awesome Apartment gem. It’s a plug and play ready solution for enabling multitenancy in rails applications. It comes with configurations and helpers I find usefull in my projects.

Apartment is an awesome gem by itself, be sure to read more about it at github.com/bradrobertson/apartment

How

If you don’t know what is tenantcy or how the apartment gem works, please read github.com/bradrobertson/apartment first.

This gem creates 2 models, Tenanfy::[Tenant, Url]. When you include the Tenanfy::Controller to your controller, it will automatically check for the correct Tenant for each request. This is done by looking for the request.host in the Tenanfy::Url table. The current tenant will be available in the ‘current_tenant` method.

This is meant to be used and only tested using PostgreSQL. Every tenant will have it own schema in the database, and the apartment gem will use postgres’ awesome ‘search_path` to look in the current_tenant schema first. You may have shared tables between your tenants, check the `config/initializers/apartment.rb`.

Why

If you don’t need any of this customizations, I recommend you use the Apartment gem alone. I only made this because I had these requirements in more than one project, and maybe someone could find a good use of it too.

Customizations

Custom Views per tenant

In my projects, each tenant usually have a different layout/theme. So the tenant model have a ‘theme` attribute. When this is setted, the `Tenanfy::Controller` will prepend the tenant theme in the view path, for example, if the theme is `blue`, the path `app/views/blue` will be prepended to the controller view_path.

Custom JS/CSS per tenant

There are helpers to include the javascripts/stylesheets with the tenant theme (ex: app/assets/javascripts/blue/application.js).

Example:

<%= tenant_stylesheet_link_tag('application.css') %>   # will include blue/application.css
<%= tenant_javascript_include_tag('application.js') %> # will include blue/application.js

Installation

Gemfile:

gem 'tenanfy'

Console:

rails g tenanfy:install
rake db:migrate

License

This project uses MIT-LICENSE.