Class: PostgresMultitenant::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/postgres_multitenant/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
# File 'lib/postgres_multitenant/middleware.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/postgres_multitenant/middleware.rb', line 12

def call(env)
  request = ActionDispatch::Request.new(env)
  subdomain = extract_subdomain(request)
  return run_in_schema(PostgresMultitenant.configuration.default_schema, env) if subdomain.blank?

  tenant = PostgresMultitenant.find_tenant_by_subdomain(subdomain) if subdomain.present?

  if tenant
    schema_name = PostgresMultitenant.schema_name_for(tenant)
    return run_in_schema(schema_name.presence || PostgresMultitenant.configuration.default_schema, env)
  end

  handle_tenant_not_found(env, request, subdomain)
end