Spree::Tenant
class. This is the highest level class in the multi-tenant system.
Every tenanted model includes a tenant_id
column. This is used to identify which tenant a record belongs to. It’s row-level multi-tenancy.
After creating a new tenant it will automatically create store and run Spree seeds to populate data such as countries, zones, tax rates, etc.
SpreeMultiTenant::Base
class. spree_multi_tenant
automatically sets the Spree.base_class
to SpreeMultiTenant::Base
so all standard Spree models will be tenanted (with an exclusion of Spree::CustomDomain
).
When creating a new model that you want to be tenanted remember to inherit from SpreeMultiTenant::Base
and not from Spree::Base
and add tenant_id
column to the table, eg.
belongs_to :tenant
to the model, it’s handled by gem.Spree::CustomDomain
. The selector works in tandem with the default Spree::Stores::FindCurrent finder.
First it finds the current store, and later sets the current tenant based on the store’s tenant_id
.
Tenant record is accessible in views and controllers as current_tenant
. In models you can access it via SpreeMultiTenant.current_tenant
.
Spree::Store.default
will return the default store for the first tenant.SpreeMultiTenant.current_tenant
is set will be executed in the context of the tenant, what that means:
tenant_id = <current_tenant.id>
to the querytenant_id
to the current tenant ID (you don’t need to do it manually)tenant_id
to the desired value.SpreeMultiTenant.current_tenant
is set will be executed in the context of the tenant.
SpreeMultiTenant.with_tenant
to enforce it, eg.
SpreeMultiTenant.without_tenant
bin/rails console
or bin/rails runner
you can set the tenant context by: