Module: Mongoid::Multitenancy

Defined in:
lib/mongoid/multitenancy.rb,
lib/mongoid/multitenancy/version.rb,
lib/mongoid/multitenancy/document.rb,
lib/mongoid/multitenancy/validators/tenancy.rb,
lib/mongoid/multitenancy/validators/tenant_uniqueness.rb

Defined Under Namespace

Modules: Document Classes: TenancyValidator, TenantUniquenessValidator

Constant Summary collapse

VERSION =

Version

'2.0.5'.freeze

Class Method Summary collapse

Class Method Details

.current_tenantObject

Returns the current tenant



16
17
18
# File 'lib/mongoid/multitenancy.rb', line 16

def current_tenant
  Thread.current[:current_tenant]
end

.current_tenant=(tenant) ⇒ Object

Set the current tenant. Make it Thread aware



11
12
13
# File 'lib/mongoid/multitenancy.rb', line 11

def current_tenant=(tenant)
  Thread.current[:current_tenant] = tenant
end

.with_tenant(tenant, &block) ⇒ Object

Affects a tenant temporary for a block execution

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/multitenancy.rb', line 21

def with_tenant(tenant, &block)
  raise ArgumentError, 'block required' if block.nil?

  begin
    old_tenant = current_tenant
    self.current_tenant = tenant
    yield
  ensure
    self.current_tenant = old_tenant
  end
end