MultiClient
Features
* Blocks calls to unscoped (unless whitelisted)
* Gives you a copy of tenant specific classes with the Unscoped suffix, for unscoped access (Post => PostUnscoped).
Installation
Add it to your gemfile:
# Gemfile
gem 'multi_client'
Run the installer:
> rails generate multi_client:install
Add migrations and migrate
> rake multi_client:install:migrations && rake db:migrate
How do I scope routing to clients?
# config/routes.rb
constraints MultiClient::Subdomain do
resources :posts
end
=> acme.example.com/posts
How do I scope routing to non-clients?
# config/routes.rb
constraints MultiClient::NoSubdomain do
resources :posts
end
=> www.example.com/posts
How do I set the current client?
# console
MultiClient::Client.set_current_by_identifier '100'
How do I unset the current client?
# console
MultiClient::Client.unset_current
How do I execute something in the scope of a Tenant?
# console
Tenant.with_tenant(Tenant.find(1)) do
# This call will be scoped to tenant_id = 1
Post.all
end
How do I setup my application so that a client is looked up automatically?
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include MultiClient::ControllerWithClient
helper_method :current_client
end
Gotchas
include MultiClient::ModelWithClient::ActiveSupport::Concern after calls to any class methods.
If you get errors like Paperclip complaining for missing validators or missing association, please make sure to include MultiClient::ModelWithClient::ActiveSupport::Concern after calls to any class methods
Wrong:
class Picture
# PictureUnscoped won't pickup the validation
include MultiClient::ModelWithClient
:image, presence: true,
content_type: { content_type: /image\/(jpeg|png)/ }
end
Correct:
class Picture
:image, presence: true,
content_type: { content_type: /image\/(jpeg|png)/ }
# Including the module after the class_method call.
include MultiClient::ModelWithClient
end
Paperclip does not find assets when using the :class interpolation
When you save an asset with paperclip and use the unscoped version of a model (i.e. PostUnscoped). The file path will include the class name with the “_unscoped ”suffix.
Trying to access this attachment with the regular, scoped version of the model fails, becuase the file path does not the “_unscoped” part.
Solution: This gem adds an interpolation called :class_without_unscoped. Use this interpolation in your url and path paperclip options instead of the origional :class_name.
License
This project rocks and uses MIT-LICENSE.