Multilocation en utilisant Apartment Gem

Comprenons d'abord la différence entre une location unique et une location multilocation

Location unique
:

Chaque client possède sa propre instance logicielle, sa propre base de données et ne sert qu'à un seul client. Ici, le logiciel peut être personnalisé pour répondre aux exigences spécifiques du client car il est indépendant.


Locations multiples:

Dans ce cas, une seule instance d’application logicielle sert plusieurs clients. Ici, nous appelons chaque client comme locataire. Ici, nous pouvons modifier les règles de l'interface utilisateur et les règles métier, mais nous ne pouvons pas modifier le code de l'application puisque la même architecture est partagée par plusieurs clients. C’est économique puisque les coûts de développement et de maintenance du logiciel sont partagés entre les clients. Mais les mises à jour ne peuvent être effectuées que par le fournisseur.


Avantages :
1. Single hardware that manages multiple customers.
2. Cost savings. Cost can be shared between all the customers.
3. Easy maintenance
4. Configuration can be changed without touching codebase.
5. New tenants can be added easily since it is a shared architecture.

Désavantages:
DB customization will be difficult to handle since it is a single DB shared between multiple customers with different tenants.

There are milia, acts_as_tenant and apartment gem to handle multi-tenancy in the Rails application.

Appartement Gemme :

This gem adopts a different way to deal with multi tenancy. It works by making another DB for each tenant. For PostgreSql, it creates another Schema for each tenant. I like this approach in light of the fact that, for this situation, the information is really disengaged. The database is not shared and on the off chance that you need to erase a client’s information, all you need to do is to simply drop their database (or pattern). It also supports Elevators and allows for the automatic switch between the customer’s tenant and thereby automating the switching logic.

This gem helps us to deal with multi tenancy in Rails application.

Add the line below, to your Gemfile and perform bundle install.

joyau 'appartement'

Pour générer le fichier d'initialisation de l'appartement, exécutez la commande ci-dessous :

bundle exec rails génère appartement: installer
config.exclusive_models = %w{ Locataire } 

Here, we can specify the tenants which are of global scope, such as the Authentication model which is a common one for all the tenants.

Here, I will be creating a Customer with subdomains, so, whenever customer logs in to the application, apartment fetches the subdomains and will query the database, based on tenant subdomain.

rails g scaffold Nom du client : chaîne domaine_tenant : chaîne

faire

base de données rails: migrer

Nous devons créer le locataire de l'appartement, une fois que nous avons créé le client.

Apartment::Tenant.create('tenant_name') - Pour créer un locataire

Utilisez le rappel after_create dans votre modèle pour créer des locataires.

def create_tenant Apartment::Tenant.create(tenant_domain) fin

Once the above callback gets executed, all the migrations will run under that respective tenant.

Switching of subdomains will be handled automatically when we request the application with subdomains. Below is the configuration line to switch the tenants based on subdomain.

Rails.application.config.middleware.use 'Appartement :: Ascenseurs :: Sous-domaine'

Pour basculer entre les locataires

Appartement ::Tenant.switch !('tenant_name')

All your ActiveRecord queries will be routed to the specific tenant when switch is called for.
Switch will be in the root scope when you call with no arguments.

Even tenant switch can be done based on the first subdomain but we need to set config in the initializer file. We can even exclude some subdomains here, such as the normal subdomain.

config.middleware.use 'Appartement :: Ascenseurs :: FirstSubdomain'
Appartement :: Ascenseurs :: FirstSubdomain.exclusive_subdomains = ['www']

We can also restrict the users not to create some domains such as www, admin. These domains will not be available for the users while they were being registered.

Need to uncomment below config line in the apartment initializer file.

Apartment::Elevators::Subdomain.exclusive_subdomains = ["public", "www" et "admin" ]

Nous pouvons également changer, en fonction de l'hôte complet. Ici, nous devons trouver le nom du locataire correspondant dans le hachage en utilisant la ligne de configuration suivante. Nous devons ajouter la ligne ci-dessous à votre application.rb

config.middleware.use 'Appartement :: Ascenseurs :: HostHash', {'example.com' => 'example_tenant'}

Supprimer les locataires :

La commande ci-dessous peut être utilisée pour supprimer des locataires :

Appartement ::Tenant.drop('tenant_name')

Veuillez consulter la ressource officielle de Gem pour trouver plus de documentation. 

Abonnez-vous pour les dernières mises à jour

Articles Similaires

Laissez un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

French
English
English
Japanese
German
French
Spanish

WhatsApp nous

Quitter la version mobile