Class: PgMultitenantSchemas::TenantResolver
- Inherits:
-
Object
- Object
- PgMultitenantSchemas::TenantResolver
- Defined in:
- lib/pg_multitenant_schemas/tenant_resolver.rb
Overview
Tenant resolution from HTTP requests
Class Method Summary collapse
-
.extract_subdomain(host) ⇒ Object
Extract subdomain from host.
-
.find_tenant_by_subdomain(subdomain) ⇒ Object
Find tenant by subdomain (only active tenants).
-
.resolve_tenant_from_request(request) ⇒ Object
Resolve tenant from request.
-
.resolve_tenant_with_fallback(request) ⇒ Object
Resolve tenant with fallback options.
Class Method Details
.extract_subdomain(host) ⇒ Object
Extract subdomain from host
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pg_multitenant_schemas/tenant_resolver.rb', line 8 def extract_subdomain(host) return nil if invalid_host?(host) host = clean_host(host) parts = host.split(".") return nil if parts.length < 2 subdomain = parts.first.downcase return nil if excluded_subdomain?(subdomain, parts) subdomain end |
.find_tenant_by_subdomain(subdomain) ⇒ Object
Find tenant by subdomain (only active tenants)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pg_multitenant_schemas/tenant_resolver.rb', line 54 def find_tenant_by_subdomain(subdomain) return nil if subdomain.blank? tenant_model = PgMultitenantSchemas.configuration.tenant_model # Attempt to find active tenant if tenant_model.respond_to?(:active) tenant_model.active.find_by(subdomain: subdomain) else tenant_model.find_by(subdomain: subdomain, status: "active") end rescue StandardError => e ::Rails.logger.error "PgMultitenantSchemas: Error finding tenant '#{subdomain}': #{e.}" nil end |
.resolve_tenant_from_request(request) ⇒ Object
Resolve tenant from request
71 72 73 74 75 76 |
# File 'lib/pg_multitenant_schemas/tenant_resolver.rb', line 71 def resolve_tenant_from_request(request) subdomain = extract_subdomain(request.host) return nil if subdomain.blank? find_tenant_by_subdomain(subdomain) end |
.resolve_tenant_with_fallback(request) ⇒ Object
Resolve tenant with fallback options
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/pg_multitenant_schemas/tenant_resolver.rb', line 79 def resolve_tenant_with_fallback(request) tenant = resolve_tenant_from_request(request) # If no tenant found and development fallback is enabled if tenant.nil? && PgMultitenantSchemas.configuration.development_fallback && ::Rails.env.development? ::Rails.logger.info "PgMultitenantSchemas: No tenant found, using development fallback" return nil # This will cause switch to default schema end tenant end |