Module: Anomonitor::Tenancy

Defined in:
lib/anomonitor/tenancy.rb

Overview

Resolves tenant schema names and switches connection context.

Anomonitor.configure do |c|
c.tenants = -> { CustomerTenant.pluck(:name) }
c.exclude_tenants = %w[public]
c.tenant_switch = ->(name, &block) { Apartment::Tenant.switch(name, &block) }
end

Class Method Summary collapse

Class Method Details

.multi_tenant?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/anomonitor/tenancy.rb', line 42

def multi_tenant?
  tenant_names.any?
end

.switch(name, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/anomonitor/tenancy.rb', line 31

def switch(name, &block)
  switcher = Anomonitor.config.tenant_switch
  if switcher
    switcher.call(name, &block)
  elsif defined?(::Apartment::Tenant)
    ::Apartment::Tenant.switch(name, &block)
  else
    yield
  end
end

.tenant_namesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/anomonitor/tenancy.rb', line 14

def tenant_names
  cfg = Anomonitor.config
  raw = cfg.tenants
  list =
    case raw
    when Proc then Array(raw.call)
    when nil then []
    else Array(raw)
    end

  excluded = Array(cfg.exclude_tenants).map(&:to_s)
  list.map(&:to_s).reject { |name| name.empty? || excluded.include?(name) }.uniq.sort
rescue StandardError => e
  Anomonitor.logger.warn("[Anomonitor] Failed to resolve tenants: #{e.message}")
  []
end