Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rendering different ERB files per tenant #5030

Merged
merged 3 commits into from
Nov 29, 2022
Merged

Conversation

javierm
Copy link
Member

@javierm javierm commented Nov 19, 2022

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

  • Allow rendering different views per tenant

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 the custom/ 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 tenant milky-way but don't need to change this file for the default tenant:

  1. Create app/components/custom/admin/action_component.rb according to the components customizations documentation
  2. Create the custom view for the milky-way tenant and save it under app/components/custom/admin/action_component.html+milky-way.erb
  3. Enter the app/components/custom/admin/ folder and run ln -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.

@javierm javierm self-assigned this Nov 19, 2022
@javierm javierm added this to Doing in Consul Democracy Nov 19, 2022
@javierm javierm added the 1.6 label Nov 19, 2022
@javierm javierm changed the title Allow rendering different views per tenant Allow rendering different ERB files per tenant Nov 19, 2022
Base automatically changed from multitenancy to master November 19, 2022 17:07
@javierm javierm force-pushed the tenant_variants branch 3 times, most recently from 73f6baf to b523501 Compare November 22, 2022 00:09
@javierm javierm moved this from Doing to Reviewing in Consul Democracy Nov 22, 2022
@javierm javierm marked this pull request as ready for review November 22, 2022 00:10
@javierm javierm force-pushed the tenant_variants branch 2 times, most recently from 4553282 to abcbd29 Compare November 28, 2022 16:12
@taitus taitus self-assigned this Nov 28, 2022
@javierm javierm moved this from Reviewing to Doing in Consul Democracy Nov 28, 2022
@javierm javierm moved this from Doing to Reviewing in Consul Democracy Nov 28, 2022
Consul Democracy automation moved this from Reviewing to Testing Nov 29, 2022
ViewComponent 2.78.0 adds support for variants with dashes and dots in
their names; since we're going to add variants named after a subdomain,
we need this feature.
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're using Rails variants [1], which means that a
tenant named `mytenant` will use a template ending with
`.html+mytenant.erb` if it's available.

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 the `custom/` 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 tenant `milky-way` but don't need to change this file for the
default tenant:

1. Create `app/components/custom/admin/action_component.rb` according to
   the components customizations documentation [2]
2. Create the custom view for the `milky-way` tenant and save it under
   `app/components/custom/admin/action_component.html+milky-way.erb`
3. Enter the `app/components/custom/admin/` folder and run `ln -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.

[1] https://guides.rubyonrails.org/v6.0/layouts_and_rendering.html#the-variants-option
[2] https://docs.consulproject.org/docs/english-documentation/customization/components
We were assigning variants in a controller, in the context of a request.
However, when sending emails, there is no request and no controller is
involved, so we also need to set the variant in the ApplicationMailer
class.
Consul Democracy automation moved this from Testing to Release 2.0.0 Nov 29, 2022
@javierm javierm deleted the tenant_variants branch November 29, 2022 13:22
@javierm javierm removed the 2.0 label Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants