Module: TenantKit

Defined in:
lib/tenant_kit.rb,
lib/tenant_kit/job.rb,
lib/tenant_kit/model.rb,
lib/tenant_kit/errors.rb,
lib/tenant_kit/current.rb,
lib/tenant_kit/railtie.rb,
lib/tenant_kit/testing.rb,
lib/tenant_kit/version.rb,
lib/tenant_kit/configuration.rb,
lib/tenant_kit/controller_concern.rb,
lib/generators/tenant_kit/install/install_generator.rb,
lib/generators/tenant_kit/migration/migration_generator.rb

Overview

TenantKit — row-level (shared-schema) multi-tenancy for Rails.

The module itself exposes configuration and the sanctioned ways to move in and out of tenant scope: TenantKit.with_tenant and TenantKit.without_tenant. Everything else hangs off Current (the request-scoped current tenant) and the Model macro belongs_to_tenant.

Defined Under Namespace

Modules: ControllerConcern, Generators, Job, Model, Testing Classes: Configuration, Current, Error, NoTenantSet, Railtie

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.configTenantKit::Configuration

Returns the singleton configuration object.

Returns:



21
22
23
# File 'lib/tenant_kit.rb', line 21

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ TenantKit::Configuration

Yields the configuration for editing, typically from an initializer.

Yield Parameters:

Returns:



29
30
31
32
# File 'lib/tenant_kit.rb', line 29

def configure
  yield config
  config
end

.scoping_active?Boolean

Returns true when a tenant is set and scoping is not disabled — i.e. queries should be filtered to the current tenant.

Returns:

  • (Boolean)

    true when a tenant is set and scoping is not disabled — i.e. queries should be filtered to the current tenant.



64
65
66
# File 'lib/tenant_kit.rb', line 64

def scoping_active?
  Current.tenant.present? && !Current.scoping_disabled
end

.scoping_disabled?Boolean

Returns true when inside a without_tenant block.

Returns:



69
70
71
# File 'lib/tenant_kit.rb', line 69

def scoping_disabled?
  !!Current.scoping_disabled
end

.with_tenant(tenant) { ... } ⇒ Object

Runs the block with tenant as the current tenant, restoring the previous tenant afterwards (even on error). The sanctioned way to switch tenants.

Parameters:

  • tenant (Object)

    the tenant record to scope to.

Yields:

  • the block to run under tenant.

Returns:

  • (Object)

    the block's return value.



40
41
42
43
44
45
46
# File 'lib/tenant_kit.rb', line 40

def with_tenant(tenant)
  previous = Current.tenant
  Current.tenant = tenant
  yield
ensure
  Current.tenant = previous
end

.without_tenant { ... } ⇒ Object

Runs the block with tenant scoping disabled — the single, greppable escape hatch for admin tools, seeds, migrations, and the console. Restores the previous state afterwards (even on error).

Yields:

  • the block to run unscoped.

Returns:

  • (Object)

    the block's return value.



54
55
56
57
58
59
60
# File 'lib/tenant_kit.rb', line 54

def without_tenant
  was = Current.scoping_disabled
  Current.scoping_disabled = true
  yield
ensure
  Current.scoping_disabled = was
end