module PgRls::Tenant

Tenant Controller

Attributes

tenant[R]

Public Class Methods

fetch() click to toggle source
# File lib/pg_rls/tenant.rb, line 21
def fetch
  @fetch ||= PgRls.main_model.find_by_tenant_id(
    PgRls.connection_class.connection.execute(
      "SELECT current_setting('rls.tenant_id')"
    ).getvalue(0, 0)
  )
rescue ActiveRecord::StatementInvalid
  'no tenant is selected'
end
find_tenant(resource) click to toggle source
# File lib/pg_rls/tenant.rb, line 31
def find_tenant(resource)
  @tenant = nil

  PgRls.search_methods.each do |method|
    @method = method
    @tenant ||= PgRls.main_model.send("find_by_#{method}!", resource)
  rescue NoMethodError => e
    @error = e
  rescue ActiveRecord::RecordNotFound
    raise PgRls::Errors::TenantNotFound
  end
end
switch(resource) click to toggle source
# File lib/pg_rls/tenant.rb, line 7
def switch(resource)
  @fetch = nil
  connection_adapter = PgRls.connection_class
  find_tenant(resource)
  connection_adapter.connection.execute(format('SET rls.tenant_id = %s',
                                               connection_adapter.connection.quote(tenant.tenant_id)))
  "RLS changed to '#{tenant.send(@method)}'"
rescue StandardError => e
  puts 'connection was not made'
  puts @error || e
end