Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom views and assets for tenants in a multi-tenant rails app?

When using a rails engine, overriding views is as easy as creating new views in the right folder. But when creating a multi-tenant rails app where all tenants reside in the same app (they don't use an engine), how can one override views for tenants.

For example:

App has a views/static/about_us.haml file which needs to be customized for each tenant. What's the best way to override this file for each tenant?

like image 588
Jacob Avatar asked Jun 08 '15 07:06

Jacob


People also ask

What is multitenancy in Rails?

Multitenancy means serving multiple independent customers from one app. Pretty typical for SaaS model. You can implement it on several different levels: Row level - you put a tenant_id column into every DB table and filter by tenant_id in every query.

Which database is best for multi tenants?

Sharded multi-tenant database This design facilitates tenant data to be distributed across multiple databases (shards), with all the data for a particular tenant is all contained in a single shard. A sharding key/tenant identifier is managed and imposed by the database schema.

How do you create a multi-tenant database?

The simplest multi-tenant database pattern uses a single database to host data for all tenants. As more tenants are added, the database is scaled up with more storage and compute resources. This scale up might be all that is needed, although there is always an ultimate scale limit.

What is multi-tenant database architecture?

A multi-tenant OpenEdge database is a shared database with a shared schema and logically and physically isolated data storage on a per tenant or group basis. Each object (table, index, LOB) is stored in a partition. Partitions keep data physically separate for each tenant.


1 Answers

I use the apartment gem which is nice for managing multi tenant environments. Apartment helps you managing schema-based database and helps you with the migration stuff.

The app uses the right schema depending on the subdomain. For example, in the case of superclient.mysuperapp.com, rails will use the superclient database schema and will work only on this schema until the request finishes.

For multitenant views, in my case I use a before_action in ApplicationController.rb to prepend my custom view path:

def prepend_view_paths
  subdomain = request.subdomain
  prepend_view_path "app/views/multitenancy/#{subdomain}"
end

Where subdomain in this case is superclient.

The logic is this: first, rails will look for a view in this path: "app/views/multitenancy/#{subdomain}". And if it doesn't find anything, it continues to find the view in the other paths on the list.

I hope my response will help you.

like image 122
matymad Avatar answered Sep 23 '22 14:09

matymad