Add custom domains (#314)

This commit is contained in:
Riccardo Graziosi
2024-03-24 12:54:02 +01:00
committed by GitHub
parent d47c70f576
commit d17b45c5c4
31 changed files with 221 additions and 39 deletions

View File

@@ -28,10 +28,44 @@ module ApplicationHelper
end
end
def add_subdomain_to(url_helper, resource=nil, options={})
options[:subdomain] = Current.tenant_or_raise!.subdomain if Rails.application.multi_tenancy?
options[:host] = Rails.application.base_url
def get_url_for(url_helper, resource: nil, disallow_custom_domain: false, options: {})
custom_domain = Current.tenant.custom_domain
if Rails.application.multi_tenancy? && (custom_domain.blank? || disallow_custom_domain)
options[:subdomain] = Current.tenant.subdomain
end
if custom_domain.blank? || disallow_custom_domain
options[:host] = Rails.application.base_url
else
options[:host] = custom_domain
end
if Rails.application.base_url.include?('https')
options[:protocol] = 'https'
else
options[:protocol] = 'http'
end
resource ? url_helper.call(resource, options) : url_helper.call(options)
end
def get_tenant_from_request(request)
if Rails.application.multi_tenancy?
request_host_splitted = request.host.split('.')
app_host_splitted = URI.parse(Rails.application.base_url).host.split('.')
if app_host_splitted.join('.') == request_host_splitted.last(app_host_splitted.length).join('.')
return if request.subdomain.blank? or RESERVED_SUBDOMAINS.include?(request.subdomain)
tenant = Tenant.find_by(subdomain: request.subdomain)
else
tenant = Tenant.find_by(custom_domain: request.host)
end
else
tenant = Tenant.first
end
tenant
end
end