Allow rendering different ERB files per tenant #5030
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
References
Background
Sometimes it might be convenient to use completely different views for different tenants. For example, a certain tenant might use a footer that has nothing to do with the default one.
For these cases, instead of adding
case Tenant.current_schema
conditions to the view, it might be tidier to use a different file.For this purpose, we can use Rails variants, which means that a tenant named
mytenant
will use a template ending with.html+mytenant.erb
if it's available.Objectives
Notes
This works with components too, but has a limitation: when using the
custom/
folder to add ERB files for a tenant, the default tenant ERB file needs to be added to thecustom/
folder as well; if there aren't changes to this file, a symbolic link will do.For example, if we're writing a custom
admin/action_component
view for the tenantmilky-way
but don't need to change this file for the default tenant:app/components/custom/admin/action_component.rb
according to the components customizations documentationmilky-way
tenant and save it underapp/components/custom/admin/action_component.html+milky-way.erb
app/components/custom/admin/
folder and runln -s ../../admin/action_component.html.erb
We're also adding some controller tests. Since Rails doesn't load the middleware during controller tests, we're stubbing the
current_schema
method directly instead of changing the subdomain of the request.