Module: PgRls::Tenant::Switchable

Extended by:
ActiveSupport::Concern
Included in:
PgRls::Tenant
Defined in:
app/models/pg_rls/tenant/switchable.rb

Overview

Switchable Module

Instance Method Summary collapse

Instance Method Details

#reset_rlsObject



35
36
37
# File 'app/models/pg_rls/tenant/switchable.rb', line 35

def reset_rls
  PgRls::Current.reset
end

#run_within(tenant) ⇒ Object



15
16
17
18
19
20
# File 'app/models/pg_rls/tenant/switchable.rb', line 15

def run_within(tenant)
  set_rls!(tenant)
  yield(PgRls::Current.tenant).presence
ensure
  PgRls::Tenant.reset_rls
end

#set_rls!(tenant) ⇒ Object



28
29
30
31
32
33
# File 'app/models/pg_rls/tenant/switchable.rb', line 28

def set_rls!(tenant)
  PgRls::Current.tenant = Searchable.by_rls_object(tenant)
  raise PgRls::Error::TenantNotFound, "No tenant found for #{tenant}" unless PgRls::Current.tenant.present?

  PgRls::Current.tenant
end

#switch(tenant) ⇒ Object



9
10
11
12
13
# File 'app/models/pg_rls/tenant/switchable.rb', line 9

def switch(tenant)
  set_rls!(tenant)
rescue PgRls::Error::TenantNotFound, PgRls::Error::InvalidSearchInput
  nil
end

#with_tenant!Object



22
23
24
25
26
# File 'app/models/pg_rls/tenant/switchable.rb', line 22

def with_tenant!(...)
  PgRls::Deprecation.warn("This method is deprecated and will be removed in future versions. " \
                          "please use PgRls::Tenant.run_within instead.")
  run_within(...)
end