### Install acts_as_tenant Gem Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Add the acts_as_tenant gem to your application's Gemfile to include its multitenancy functionality. ```ruby gem 'acts_as_tenant' ``` -------------------------------- ### ActsAsTenant Configuration Options Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Provides examples of configuring ActsAsTenant globally via an initializer, including setting `require_tenant` and customizing `job_scope`. ```ruby ActsAsTenant.configure do |config| config.require_tenant = false # true # Customize the query for loading the tenant in background jobs config.job_scope = ->{ all } end ``` -------------------------------- ### Test Tenant Middleware Setup Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Configuration for RSpec integration tests to use `TestTenantMiddleware` for proper tenant isolation and cleanup between requests. ```ruby # test.rb require_dependency 'acts_as_tenant/test_tenant_middleware' Rails.application.configure do config.middleware.use ActsAsTenant::TestTenantMiddleware end ``` -------------------------------- ### Rails Console Tenant Setup Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Shows a configuration for Rails console to automatically set the current tenant to the first available account upon loading, and reset it after `reload!`. ```ruby SET_TENANT_PROC = lambda do if defined?(Rails::Console) puts "> ActsAsTenant.current_tenant = Account.first" ActsAsTenant.current_tenant = Account.first end end Rails.application.configure do if Rails.env.development? # Set the tenant to the first account in development on load config.after_initialize do SET_TENANT_PROC.call end # Reset the tenant after calling 'reload!' in the console ActiveSupport::Reloader.to_complete do SET_TENANT_PROC.call end end end ``` -------------------------------- ### Rails Migration and Model Setup for acts_as_tenant Source: https://github.com/erwinm/acts_as_tenant/blob/master/docs/blog_post.md This snippet demonstrates the necessary steps to integrate acts_as_tenant into a Rails application. It includes a database migration to add the tenant ID to a model and the model configuration using acts_as_tenant and validates_uniqueness_to_tenant. ```ruby class Addaccounttoproject < ActiveRecord::Migration def change add_column :projects, :account_id, :integer end end class Project < ActiveRecord::Base acts_as_tenant(:account) validates_uniqueness_to_tenant :name end ``` -------------------------------- ### Sidekiq Integration Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Instructions for integrating ActsAsTenant with Sidekiq by requiring a specific initializer file. ```ruby require 'acts_as_tenant/sidekiq' ``` -------------------------------- ### Conditional require_tenant Configuration Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Demonstrates how to set `require_tenant` to a lambda for dynamic evaluation at runtime, allowing conditional tenant requirement based on request environment. ```ruby ActsAsTenant.configure do |config| config.require_tenant = lambda do if $request_env.present? return false if $request_env["REQUEST_PATH"].start_with?("/admin/") end end end ``` -------------------------------- ### Manually Set Current Tenant Source: https://github.com/erwinm/acts_as_tenant/blob/master/docs/blog_post.md Demonstrates how to manually set the current tenant within a controller. This involves fetching the tenant object first and then passing it to the `set_current_tenant_to` method. ```ruby class ApplicationController < ActionController::Base current_account = Account.method_to_find_the_current_account set_current_tenant_to(current_account) end ``` -------------------------------- ### Manually Setting Current Tenant with before_action Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Demonstrates how to manually set the current tenant in a Rails application using a `before_action`. This involves declaring `set_current_tenant_through_filter` and defining a method to fetch and set the tenant. ```ruby class ApplicationController < ActionController::Base set_current_tenant_through_filter before_action :your_method_that_finds_the_current_tenant def your_method_that_finds_the_current_tenant current_account = Account.find_it set_current_tenant(current_account) end end ``` ```ruby class MembersController < ActionController::Base set_current_tenant_through_filter before_action :set_tenant before_action :set_member, only: [:show, :edit, :update, :destroy] def set_tenant set_current_tenant(current_user.account) end end ``` -------------------------------- ### acts_as_tenant with belongs_to Options Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Demonstrates passing standard `belongs_to` options like `counter_cache` directly to the `acts_as_tenant` declaration. ```ruby acts_as_tenant(:account, counter_cache: true) ``` -------------------------------- ### Custom Primary Key Configuration Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Shows how to specify a custom primary key for the `acts_as_tenant` association when it differs from the default `id`. ```ruby acts_as_tenant(:account, :primary_key => 'primaryID') ``` -------------------------------- ### Custom Foreign Key Configuration Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Demonstrates how to specify a custom foreign key for the `acts_as_tenant` association when it differs from the default `account_id`. ```ruby acts_as_tenant(:account, :foreign_key => 'accountID') ``` -------------------------------- ### Set Current Tenant by Subdomain or Domain Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Configure the ApplicationController to set the current tenant by first attempting to use the subdomain and then falling back to the domain if no subdomain match is found. It specifies the tenant model and the attributes for lookup. ```APIDOC set_current_tenant_by_subdomain_or_domain(:account, :subdomain, :domain) # Parameters: # :account: The model representing tenants (e.g., Account). # :subdomain: The attribute on the tenant model used for subdomain lookup (e.g., :subdomain). # :domain: The attribute on the tenant model used for domain lookup (e.g., :domain). # # Options: # subdomain_lookup: :first or :last (default) to specify which subdomain to use if multiple exist. # # Example: # class ApplicationController < ActionController::Base # set_current_tenant_by_subdomain_or_domain(:account, :subdomain, :domain) # end ``` -------------------------------- ### HABTM Association Scoping Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Illustrates how to scope a model in a `has_and_belongs_to_many` relationship using the `through` option with `acts_as_tenant`. ```ruby class Organisation < ActiveRecord::Base has_many :organisations_users has_many :users, through: :organisations_users end class User < ActiveRecord::Base has_many :organisations_users acts_as_tenant :organisation, through: :organisations_users end class OrganisationsUser < ActiveRecord::Base belongs_to :user acts_as_tenant :organisation end ``` -------------------------------- ### Rails Default Error Page Styling Source: https://github.com/erwinm/acts_as_tenant/blob/master/spec/dummy/public/422.html This snippet contains CSS styles for a default Rails error page, typically displayed for unhandled exceptions or specific HTTP error codes like 422 (Unprocessable Entity). It defines the layout and appearance of error messages. ```css .rails-default-error-page { background-color: #EFEFEF; color: #2E2F30; text-align: center; font-family: arial, sans-serif; margin: 0; } .rails-default-error-page div.dialog { width: 95%; max-width: 33em; margin: 4em auto 0; } .rails-default-error-page div.dialog > div { border: 1px solid #CCC; border-right-color: #999; border-left-color: #999; border-bottom-color: #BBB; border-top: #B00100 solid 4px; border-top-left-radius: 9px; border-top-right-radius: 9px; background-color: white; padding: 7px 12% 0; box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); } .rails-default-error-page h1 { font-size: 100%; color: #730E15; line-height: 1.5em; } .rails-default-error-page div.dialog > p { margin: 0 0 1em; padding: 1em; background-color: #F7F7F7; border: 1px solid #CCC; border-right-color: #999; border-left-color: #999; border-bottom-color: #999; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-color: #DADADA; color: #666; box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); } ``` -------------------------------- ### RSpec Configuration for ActsAsTenant Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Configures RSpec to manage tenant switching for tests. It sets a default account before the suite runs and manages the `current_tenant` and `test_tenant` based on test type (request or other). It also clears the tenant after each test. ```ruby config.before(:suite) do |example| # Make the default tenant globally available to the tests $default_account = Account.create! end config.before(:each) do |example| if example.metadata[:type] == :request # Set the `test_tenant` value for integration tests ActsAsTenant.test_tenant = $default_account else # Otherwise just use current_tenant ActsAsTenant.current_tenant = $default_account end end config.after(:each) do |example| # Clear any tenancy that might have been set ActsAsTenant.current_tenant = nil ActsAsTenant.test_tenant = nil end ``` -------------------------------- ### Set Current Tenant by Subdomain Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Configure the ApplicationController to automatically set the current tenant based on the subdomain of the incoming request. It specifies the tenant model and the attribute used for lookup. ```APIDOC set_current_tenant_by_subdomain(:account, :subdomain) # Parameters: # :account: The model representing tenants (e.g., Account). # :subdomain: The attribute on the tenant model used for subdomain lookup (e.g., :subdomain). # # Options: # subdomain_lookup: :first or :last (default) to specify which subdomain to use if multiple exist. # # Example: # class ApplicationController < ActionController::Base # set_current_tenant_by_subdomain(:account, :subdomain) # end ``` -------------------------------- ### Set Current Tenant by Subdomain Source: https://github.com/erwinm/acts_as_tenant/blob/master/docs/blog_post.md Configures acts_as_tenant to determine the current tenant based on the request's subdomain. It specifies the tenant model (e.g., Account) and the attribute on that model that stores the subdomain. ```ruby class ApplicationController < ActionController::Base set_current_tenant_by_subdomain(:account, :subdomain) end ``` -------------------------------- ### Scoping Models with acts_as_tenant Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Details how to configure a Rails model to be tenant-scoped using `acts_as_tenant`. This requires a corresponding tenant ID column in the model's database table. When a current tenant is set, all queries and new object creations for this model will be automatically scoped. ```ruby class AddAccountToProjects < ActiveRecord::Migration def up add_column :projects, :account_id, :integer add_index :projects, :account_id end end ``` ```ruby class Project < ActiveRecord::Base acts_as_tenant(:account) end ``` ```ruby # Manually setting the current tenant for testing ActsAsTenant.current_tenant = Account.find(3) # All searches are scoped by the tenant Project.all # => all projects with account_id == 3 Project.tasks.all # => all tasks with account_id == 3 # New objects are scoped to the current tenant @project = Project.new(:name => 'big project') # => <#Project id: nil, name: 'big project', :account_id: 3> # It will not allow the creation of objects outside the current_tenant scope @project.account_id = 2 @project.save # => false # It will not allow association with objects outside the current tenant scope @task = Task.new # => <#Task id: nil, name: nil, project_id: nil, :account_id: 3> ``` -------------------------------- ### Unprocessable Entity Error Message Source: https://github.com/erwinm/acts_as_tenant/blob/master/spec/dummy/public/422.html This snippet represents a typical error message displayed in a Rails application when a request is rejected due to being unprocessable, often indicated by a 422 HTTP status code. It suggests potential causes and advises checking logs. ```text The change you wanted was rejected (422) . The change you wanted was rejected. =================================== Maybe you tried to change something you didn't have access to. If you are the application owner check the logs for more information. ``` -------------------------------- ### Setting Tenant for a Block of Code Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Shows how to set the current tenant for a specific block of code using `ActsAsTenant.with_tenant`. This is useful for background jobs or isolated operations that need to run within a particular tenant's context. All tenant-setting methods are thread-safe. ```ruby ActsAsTenant.with_tenant(current_account) do # Current tenant is set for all code in this block end ``` -------------------------------- ### Allowing Tenant Updating for a Block Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Illustrates how to enable tenant updating for models within a specific block of code using `ActsAsTenant.with_mutable_tenant`. This feature is useful for administrative screens where users might need to change the tenant associated with existing models. ```ruby ActsAsTenant.with_mutable_tenant do # Tenant updating is enabled for all code in this block end ``` -------------------------------- ### Redundant belongs_to Declaration Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Highlights that `belongs_to :account` is redundant when `acts_as_tenant(:account)` is already declared on the model. ```ruby class User < ActiveRecord::Base acts_as_tenant(:account) # YES belongs_to :account # REDUNDANT end ``` -------------------------------- ### Uniqueness Validation within Tenant Scope Source: https://github.com/erwinm/acts_as_tenant/blob/master/docs/blog_post.md Explains the use of `validates_uniqueness_to_tenant` for enforcing unique attribute values within the scope of the current tenant. This method accepts the same options as the standard `validates_uniqueness_of` validator. ```ruby validates_uniqueness_to_tenant :attribute_name ``` -------------------------------- ### Validating Uniqueness within Tenant Scope Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Explains how to enforce unique attribute validation scoped to the current tenant using `validates_uniqueness_to_tenant`. This method accepts all the same options as Rails' `validates_uniqueness_of`. ```ruby validates_uniqueness_to_tenant :name, :email ``` -------------------------------- ### Disabling Tenant Checking for a Block Source: https://github.com/erwinm/acts_as_tenant/blob/master/README.md Explains how to temporarily disable tenant checking for a block of code using `ActsAsTenant.without_tenant`. This is beneficial for shared routes or administrative interfaces where tenant scoping might not be desired or required. ```ruby ActsAsTenant.without_tenant do # Tenant checking is disabled for all code in this block end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.