module MultiTenantSupport

Constants

PROTECTED

Scoped and proteced

PROTECTED_EXCEPT_READ

Scoped and protected except read across tenant

UNPROTECTED

Scoped but unprotected

VERSION

Public Instance Methods

allow_read_across_tenant() { || ... } click to toggle source
# File lib/multi-tenant-support.rb, line 74
def allow_read_across_tenant
  raise 'Cannot read across tenant, try wrap in without_current_tenant' if current_tenant

  if block_given?
    Current.set(protection_state: PROTECTED_EXCEPT_READ) do
      yield
    end
  else
    Current.protection_state = PROTECTED_EXCEPT_READ
  end
end
allow_read_across_tenant?() click to toggle source
# File lib/multi-tenant-support.rb, line 54
def allow_read_across_tenant?
  current_tenant.nil? && [PROTECTED_EXCEPT_READ, UNPROTECTED].include?(Current.protection_state)
end
app() { |app| ... } click to toggle source
# File lib/multi_tenant_support/config/app.rb, line 19
def app
  @app ||= Config::App.new
  return @app unless block_given?

  yield @app
end
configure(&block) click to toggle source
# File lib/multi-tenant-support.rb, line 17
def configure(&block)
  instance_eval(&block)
end
console() { |console| ... } click to toggle source
# File lib/multi_tenant_support/config/console.rb, line 14
def console
  @console ||= Config::Console.new
  return @console unless block_given?

  yield @console
end
controller() { |controller| ... } click to toggle source
# File lib/multi_tenant_support/config/controller.rb, line 15
def controller
  @controller ||= Config::Controller.new
  return @controller unless block_given?

  yield @controller
end
current_tenant() click to toggle source
# File lib/multi-tenant-support.rb, line 21
def current_tenant
  Current.tenant_account
end
current_tenant_account_method() click to toggle source
# File lib/multi_tenant_support/config/controller.rb, line 22
def current_tenant_account_method
  controller.current_tenant_account_method
end
current_tenant_id() click to toggle source
# File lib/multi-tenant-support.rb, line 25
def current_tenant_id
  Current.tenant_account&.send(model.tenant_account_primary_key)
end
full_protected?() click to toggle source
# File lib/multi-tenant-support.rb, line 50
def full_protected?
  current_tenant || Current.protection_state == PROTECTED
end
model() { |model| ... } click to toggle source
# File lib/multi_tenant_support/config/model.rb, line 30
def model
  @model ||= Config::Model.new
  return @model unless block_given?

  yield @model
end
set_current_tenant(tenant) click to toggle source
# File lib/multi-tenant-support.rb, line 29
def set_current_tenant(tenant)
  Current.tenant_account = tenant
  Current.protection_state = PROTECTED
end
turn_off_protection() { || ... } click to toggle source
# File lib/multi-tenant-support.rb, line 62
def turn_off_protection
  raise 'Cannot turn off protection, try wrap in without_current_tenant' if current_tenant

  if block_given?
    Current.set(protection_state: UNPROTECTED) do
      yield
    end
  else
    Current.protection_state = UNPROTECTED
  end
end
turn_on_full_protection() { || ... } click to toggle source
# File lib/multi-tenant-support.rb, line 86
def turn_on_full_protection
  if block_given?
    Current.set(protection_state: PROTECTED) do
      yield
    end
  else
    Current.protection_state = PROTECTED
  end
end
under_tenant(tenant_account) { || ... } click to toggle source
# File lib/multi-tenant-support.rb, line 34
def under_tenant(tenant_account, &block)
  raise ArgumentError, "block is missing" if block.nil?

  Current.set(tenant_account: tenant_account, protection_state: PROTECTED) do
    yield
  end
end
unprotected?() click to toggle source
# File lib/multi-tenant-support.rb, line 58
def unprotected?
  current_tenant.nil? && Current.protection_state == UNPROTECTED
end
without_current_tenant() { || ... } click to toggle source
# File lib/multi-tenant-support.rb, line 42
def without_current_tenant(&block)
  raise ArgumentError, "block is missing" if block.nil?

  Current.set(tenant_account: nil) do
    yield
  end
end