From 5c61b72d216a55f3ce856ca4da4787fec1f5fc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 6 Oct 2022 17:48:42 +0200 Subject: [PATCH] Identify the current tenant in the tag This way it will be possible to write CSS and JavaScript code that will only apply to specific tenants. Note that CSS customization is still limited because it isn't possible to use different SCSS variables per tenant. --- .../common_html_attributes_component.rb | 6 ++- .../common_html_attributes_component_spec.rb | 50 +++++++++++++++---- spec/system/multitenancy_spec.rb | 4 ++ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/app/components/layout/common_html_attributes_component.rb b/app/components/layout/common_html_attributes_component.rb index 41cb8323b..bface8c6c 100644 --- a/app/components/layout/common_html_attributes_component.rb +++ b/app/components/layout/common_html_attributes_component.rb @@ -4,7 +4,7 @@ class Layout::CommonHTMLAttributesComponent < ApplicationComponent private def attributes - sanitize([dir, lang].compact.join(" ")) + sanitize([dir, lang, html_class].compact.join(" ")) end def dir @@ -14,4 +14,8 @@ class Layout::CommonHTMLAttributesComponent < ApplicationComponent def lang "lang=\"#{I18n.locale}\"" end + + def html_class + "class=\"tenant-#{Tenant.current_schema}\"" if Rails.application.config.multitenancy + end end diff --git a/spec/components/layout/common_html_attributes_component_spec.rb b/spec/components/layout/common_html_attributes_component_spec.rb index 2d6fc3238..ded22103c 100644 --- a/spec/components/layout/common_html_attributes_component_spec.rb +++ b/spec/components/layout/common_html_attributes_component_spec.rb @@ -3,16 +3,36 @@ require "rails_helper" describe Layout::CommonHTMLAttributesComponent do let(:component) { Layout::CommonHTMLAttributesComponent.new } - it "includes the default language by default" do - render_inline component + context "with multitenancy disabled" do + before { allow(Rails.application.config).to receive(:multitenancy).and_return(false) } - expect(page.text).to eq 'lang="en"' + it "includes the default language by default" do + render_inline component + + expect(page.text).to eq 'lang="en"' + end + + it "includes the current language" do + I18n.with_locale(:es) { render_inline component } + + expect(page.text).to eq 'lang="es"' + end end - it "includes the current language" do - I18n.with_locale(:es) { render_inline component } + context "with multitenancy enabled" do + it "includes a class with the 'public' suffix for the default tenant" do + render_inline component - expect(page.text).to eq 'lang="es"' + expect(page.text).to eq 'lang="en" class="tenant-public"' + end + + it "includes a class with the schema name as suffix for other tenants" do + allow(Tenant).to receive(:current_schema).and_return "private" + + render_inline component + + expect(page.text).to eq 'lang="en" class="tenant-private"' + end end context "RTL languages" do @@ -25,10 +45,22 @@ describe Layout::CommonHTMLAttributesComponent do after { I18n.enforce_available_locales = default_enforce } - it "includes the dir attribute" do - I18n.with_locale(:ar) { render_inline component } + context "with multitenancy disabled" do + before { allow(Rails.application.config).to receive(:multitenancy).and_return(false) } - expect(page.text).to eq 'dir="rtl" lang="ar"' + it "includes the dir attribute" do + I18n.with_locale(:ar) { render_inline component } + + expect(page.text).to eq 'dir="rtl" lang="ar"' + end + end + + context "with multitenancy enabled" do + it "includes the dir and the class attributes" do + I18n.with_locale(:ar) { render_inline component } + + expect(page.text).to eq 'dir="rtl" lang="ar" class="tenant-public"' + end end end end diff --git a/spec/system/multitenancy_spec.rb b/spec/system/multitenancy_spec.rb index ef1a90564..ef161fb9c 100644 --- a/spec/system/multitenancy_spec.rb +++ b/spec/system/multitenancy_spec.rb @@ -28,12 +28,16 @@ describe "Multitenancy" do visit polls_path expect(page).to have_content "Human rights for Martians?" + expect(page).to have_css "html.tenant-mars" + expect(page).not_to have_css "html.tenant-venus" end with_subdomain("venus") do visit polls_path expect(page).to have_content "There are no open votings" + expect(page).to have_css "html.tenant-venus" + expect(page).not_to have_css "html.tenant-mars" end end