In a demo application we want to handle tenants in different schemas (default apartment configuration for Postgres). These are to be selected from the subdomain of the application url.
I got it working, but then broke it again (it started using public instead of the selected schema).
I could narrow the problem down to this: If I use the following piece of code everything works as expected:
require 'apartment/elevators/first_subdomain'
Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain
Apartment::Elevators::FirstSubdomain.excluded_subdomains = ['demo', 'localhost']
If I do this myself (extending the missing Tenant behavior to return a 404), it doesn't work anymore:
class DemoUserSelectionMiddleware < Apartment::Elevators::Generic
def parse_tenant_name(request)
subdomain = request.host.split('.')[0]
unless Rails.application.config.is_demo && subdomain.start_with?('demo_')
'public'
else
subdomain
end
end
def call(*args)
begin
super
rescue Apartment::TenantNotFound
Rails.logger.error "ERROR: Apartment Tenant not found: #{Apartment::Tenant.current.inspect}"
return [404, {"Content-Type" => "application/json"}, ["Error" => "Tenant not found"]]
end
end
end
No error, no log entries, nothing. It just decides to use 'public'. Using byebug I see that the subdomain is properly found.
Am I missing something obvious here?