From 2c0ede3aaa19bac1eac52d31363561c66a5e96b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 6 Oct 2022 18:10:35 +0200 Subject: [PATCH] Use the dir attribute in dashboard and mailer layouts We forgot to do so in commit d827768c0. In order to avoid the same mistake in the future, we're extracting a method to get these attributes. We're also adding tests, since we didn't have any tests to check that the `dir` attribute was properly set. --- .../common_html_attributes_component.html.erb | 1 + .../common_html_attributes_component.rb | 17 ++++++++++ app/helpers/layouts_helper.rb | 4 +++ app/mailers/application_mailer.rb | 4 +-- app/views/layouts/admin.html.erb | 3 +- app/views/layouts/application.html.erb | 2 +- app/views/layouts/dashboard.html.erb | 2 +- app/views/layouts/devise.html.erb | 2 +- app/views/layouts/mailer.html.erb | 2 +- app/views/layouts/management.html.erb | 3 +- .../common_html_attributes_component_spec.rb | 34 +++++++++++++++++++ 11 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 app/components/layout/common_html_attributes_component.html.erb create mode 100644 app/components/layout/common_html_attributes_component.rb create mode 100644 spec/components/layout/common_html_attributes_component_spec.rb diff --git a/app/components/layout/common_html_attributes_component.html.erb b/app/components/layout/common_html_attributes_component.html.erb new file mode 100644 index 000000000..fce00b5b2 --- /dev/null +++ b/app/components/layout/common_html_attributes_component.html.erb @@ -0,0 +1 @@ +<%= attributes -%> diff --git a/app/components/layout/common_html_attributes_component.rb b/app/components/layout/common_html_attributes_component.rb new file mode 100644 index 000000000..41cb8323b --- /dev/null +++ b/app/components/layout/common_html_attributes_component.rb @@ -0,0 +1,17 @@ +class Layout::CommonHTMLAttributesComponent < ApplicationComponent + delegate :rtl?, to: :helpers + + private + + def attributes + sanitize([dir, lang].compact.join(" ")) + end + + def dir + 'dir="rtl"' if rtl? + end + + def lang + "lang=\"#{I18n.locale}\"" + end +end diff --git a/app/helpers/layouts_helper.rb b/app/helpers/layouts_helper.rb index e97727b34..32ddde240 100644 --- a/app/helpers/layouts_helper.rb +++ b/app/helpers/layouts_helper.rb @@ -9,4 +9,8 @@ module LayoutsHelper link_to(text, path, options.merge(title: title)) end end + + def common_html_attributes + render Layout::CommonHTMLAttributesComponent.new + end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 757bc32ff..4451e348e 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,7 +1,5 @@ class ApplicationMailer < ActionMailer::Base - helper :settings - helper :application - helper :mailer + helper :application, :layouts, :mailer, :settings default from: proc { "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>" } layout "mailer" before_action :set_asset_host diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index 47cba9f90..48ad4426d 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -1,6 +1,5 @@ - lang="<%= I18n.locale %>"> - +> <%= render "layouts/common_head", default_title: "Admin" %> <%= content_for :head %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 04fa2920a..495a18838 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,5 +1,5 @@ - lang="<%= I18n.locale %>" data-current-user-id="<%= current_user&.id %>"> + data-current-user-id="<%= current_user&.id %>"> <%= render "layouts/common_head", default_title: setting["org_name"] %> <%= render "layouts/meta_tags" %> diff --git a/app/views/layouts/dashboard.html.erb b/app/views/layouts/dashboard.html.erb index cf33f6f4f..ffdfc9439 100644 --- a/app/views/layouts/dashboard.html.erb +++ b/app/views/layouts/dashboard.html.erb @@ -1,5 +1,5 @@ - + data-current-user-id="<%= current_user&.id %>"> <%= render "layouts/common_head", default_title: setting["org_name"] %> <%= render "layouts/meta_tags" %> diff --git a/app/views/layouts/devise.html.erb b/app/views/layouts/devise.html.erb index e9ebac423..cc4db0fca 100644 --- a/app/views/layouts/devise.html.erb +++ b/app/views/layouts/devise.html.erb @@ -1,5 +1,5 @@ - lang="<%= I18n.locale %>"> +> <%= render "layouts/common_head", default_title: setting["org_name"] %> <%= render "layouts/meta_tags" %> diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index 6691fe4fb..c5dcf1fd6 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -1,5 +1,5 @@ - +> <%= t("mailers.title") %> diff --git a/app/views/layouts/management.html.erb b/app/views/layouts/management.html.erb index 182c12f4a..5bb9f525a 100644 --- a/app/views/layouts/management.html.erb +++ b/app/views/layouts/management.html.erb @@ -1,6 +1,5 @@ - lang="<%= I18n.locale %>"> - +> <%= render "layouts/common_head", default_title: "Management" %> <%= stylesheet_link_tag "print", media: "print" %> diff --git a/spec/components/layout/common_html_attributes_component_spec.rb b/spec/components/layout/common_html_attributes_component_spec.rb new file mode 100644 index 000000000..2d6fc3238 --- /dev/null +++ b/spec/components/layout/common_html_attributes_component_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +describe Layout::CommonHTMLAttributesComponent do + let(:component) { Layout::CommonHTMLAttributesComponent.new } + + 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 + + context "RTL languages" do + let!(:default_enforce) { I18n.enforce_available_locales } + + before do + I18n.enforce_available_locales = false + allow(I18n).to receive(:available_locales).and_return(%i[ar en es]) + end + + after { I18n.enforce_available_locales = default_enforce } + + it "includes the dir attribute" do + I18n.with_locale(:ar) { render_inline component } + + expect(page.text).to eq 'dir="rtl" lang="ar"' + end + end +end