Identify the current tenant in the <html> 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.
This commit is contained in:
@@ -4,7 +4,7 @@ class Layout::CommonHTMLAttributesComponent < ApplicationComponent
|
|||||||
private
|
private
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
sanitize([dir, lang].compact.join(" "))
|
sanitize([dir, lang, html_class].compact.join(" "))
|
||||||
end
|
end
|
||||||
|
|
||||||
def dir
|
def dir
|
||||||
@@ -14,4 +14,8 @@ class Layout::CommonHTMLAttributesComponent < ApplicationComponent
|
|||||||
def lang
|
def lang
|
||||||
"lang=\"#{I18n.locale}\""
|
"lang=\"#{I18n.locale}\""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_class
|
||||||
|
"class=\"tenant-#{Tenant.current_schema}\"" if Rails.application.config.multitenancy
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ require "rails_helper"
|
|||||||
describe Layout::CommonHTMLAttributesComponent do
|
describe Layout::CommonHTMLAttributesComponent do
|
||||||
let(:component) { Layout::CommonHTMLAttributesComponent.new }
|
let(:component) { Layout::CommonHTMLAttributesComponent.new }
|
||||||
|
|
||||||
|
context "with multitenancy disabled" do
|
||||||
|
before { allow(Rails.application.config).to receive(:multitenancy).and_return(false) }
|
||||||
|
|
||||||
it "includes the default language by default" do
|
it "includes the default language by default" do
|
||||||
render_inline component
|
render_inline component
|
||||||
|
|
||||||
@@ -14,6 +17,23 @@ describe Layout::CommonHTMLAttributesComponent do
|
|||||||
|
|
||||||
expect(page.text).to eq 'lang="es"'
|
expect(page.text).to eq 'lang="es"'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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="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
|
context "RTL languages" do
|
||||||
let!(:default_enforce) { I18n.enforce_available_locales }
|
let!(:default_enforce) { I18n.enforce_available_locales }
|
||||||
@@ -25,10 +45,22 @@ describe Layout::CommonHTMLAttributesComponent do
|
|||||||
|
|
||||||
after { I18n.enforce_available_locales = default_enforce }
|
after { I18n.enforce_available_locales = default_enforce }
|
||||||
|
|
||||||
|
context "with multitenancy disabled" do
|
||||||
|
before { allow(Rails.application.config).to receive(:multitenancy).and_return(false) }
|
||||||
|
|
||||||
it "includes the dir attribute" do
|
it "includes the dir attribute" do
|
||||||
I18n.with_locale(:ar) { render_inline component }
|
I18n.with_locale(:ar) { render_inline component }
|
||||||
|
|
||||||
expect(page.text).to eq 'dir="rtl" lang="ar"'
|
expect(page.text).to eq 'dir="rtl" lang="ar"'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -28,12 +28,16 @@ describe "Multitenancy" do
|
|||||||
visit polls_path
|
visit polls_path
|
||||||
|
|
||||||
expect(page).to have_content "Human rights for Martians?"
|
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
|
end
|
||||||
|
|
||||||
with_subdomain("venus") do
|
with_subdomain("venus") do
|
||||||
visit polls_path
|
visit polls_path
|
||||||
|
|
||||||
expect(page).to have_content "There are no open votings"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user