SimplyTheTenant
Short description and motivation.
A simpler alternative to acts_as_tenant. For most applications acts_as_tenant is probably what you should reach for, it's battletested, and more feature rich.
So, that begs the question, what is the point of this gem at all?
- I just kinda wanted to write a multitenancy gem. For basically any applications, prefer
acts_as_tenant, since it's battletested. acts_as_tenantusesActiveSupport::CurrentAttributesunder the hood, but that isn't exposed to the user. While that isn't exactly hard to add withacts_as_tenant,simply_the_tenantdoes this out of the box.simply_the_tenantadheres to your domain model.Current.accountvscurrent_tenant, making things marginally easier to reason about.simply_the_tenantuses Rails 7query_constraints, so certain queries will use compound indices, which is nice for anyone who wants to use composite primary keys or sharding.simply_the_tenantrequires explicit scoping to access the data of a tenant or to access global data.
Overall, acts_as_tenant does do all of the things this gem does. simply_the_tenant just presents them in a different way and provides less configuration options. If that pleases you, feel free to use simply_the_tenant instead!
Installation
Add this line to your application's Gemfile:
gem "simply_the_tenant"
Getting started
Setting up simply_the_tenant is essentially identical to acts_as_tenant. But, a thing to keep in mind is that simply_the_tenatn is strict about naming. There is currently no way to tell simply_the_tenant what foreign key to use.
Model Setup
class MyTenant < ApplicationRecord
simply_the_tenant
end
Anything that belongs to the MyTenant must have a my_tenant_id column.
class User < ApplicationRecord
belongs_to_tenant :my_tenant
end
This will set up a default scope, query constraints, automatic setting of my_tenant_id and validations for the tenant.
You'll also need to setup a Current model if you don't already have one.
class Current < ActiveSupport::CurrentAttributes
attribute :my_tenant
end
Controller Setup
simply_the_tenant uses the last subdomain from a request to determine the tenant to scope a given request to. There is currently no way to change this in simple_tenant
class ApplicationController < ActionController::Base
sets_current_tenant :my_tenant
def some_cool_action
# Current.my_tenant is accessible here automatically
end
end
Background Processing
simply_the_tenant currently doesn't support automatically setting the tenant for any background processing libraries out of the box. This will change shortly.
Testing
simply_the_tenant also doesn't support automatically setting the tenant for any testing libraries out of the box. This will change shortly.
Contributing
- Fork the repo
- Make changes
- Run the tests
bundle exec appraisals bin/test - Run the linter
bundle exec rubocop - Submit a PR
License
The gem is available as open source under the terms of the MIT License.