Skip to content

Commit 83958e2

Browse files
committed
Add configuration for RTL (right-to-left) HTML 'dir' attribute
1 parent bb30121 commit 83958e2

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

app/helpers/spotlight/main_app_helpers.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ module MainAppHelpers
88
include Spotlight::NavbarHelper
99
include Spotlight::MastheadHelper
1010

11+
def html_tag_attributes
12+
return {} unless rtl_enabled?
13+
14+
rtl_locale? ? { dir: 'rtl' } : {}
15+
end
16+
17+
def rtl_enabled?
18+
Spotlight::Engine.config.rtl_enabled || false
19+
end
20+
21+
def rtl_locale?
22+
Spotlight::Engine.config.rtl_locales.include?(I18n.locale.to_sym)
23+
end
24+
1125
def on_browse_page?
1226
params[:controller] == 'spotlight/browse'
1327
end

lib/generators/spotlight/templates/config/initializers/spotlight_initializer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@
103103
# Spotlight::Engine.config.page_configurations = {
104104
# 'my-local-config': ->(context) { context.my_custom_data_path(context.current_exhibit) }
105105
# }
106+
#
107+
# ==> RTL (right-to-left) configuration
108+
# When enabled, adds dir="rtl" to html_tag_attributes for locales listed in rtl_locales.
109+
# Spotlight::Engine.config.rtl_enabled = true
110+
# Spotlight::Engine.config.rtl_locales = %i[ar]

lib/spotlight/engine.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ def self.blacklight_config
230230
# add could add an available locale which could break things if unexpected.
231231
config.i18n.available_locales = config.i18n_locales.keys
232232

233+
# When enabled, adds dir="rtl" to html_tag_attributes for locales listed in rtl_locales.
234+
config.rtl_enabled = false
235+
config.rtl_locales = %i[ar]
236+
233237
# Copy of JbuilderHandler tweaked to spit out YAML for translation exports
234238
class TranslationYamlHandler
235239
cattr_accessor :default_format

spec/helpers/spotlight/main_app_helpers_spec.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,86 @@
9999
end
100100
end
101101
end
102+
103+
describe '#html_tag_attributes' do
104+
subject { helper.html_tag_attributes }
105+
106+
context 'when rtl_enabled? is false' do
107+
before { allow(helper).to receive(:rtl_enabled?).and_return(false) }
108+
109+
it 'does not set dir' do
110+
expect(subject).not_to have_key(:dir)
111+
end
112+
end
113+
114+
context 'when rtl_enabled? is true' do
115+
before { allow(helper).to receive(:rtl_enabled?).and_return(true) }
116+
117+
context 'with an RTL locale' do
118+
before { allow(helper).to receive(:rtl_locale?).and_return(true) }
119+
120+
it 'sets dir to rtl' do
121+
expect(subject[:dir]).to eq 'rtl'
122+
end
123+
end
124+
125+
context 'with an LTR locale' do
126+
before { allow(helper).to receive(:rtl_locale?).and_return(false) }
127+
128+
it 'does not set dir' do
129+
expect(subject).not_to have_key(:dir)
130+
end
131+
end
132+
end
133+
end
134+
135+
describe '#rtl_enabled?' do
136+
subject { helper.rtl_enabled? }
137+
138+
context 'when rtl_enabled is false' do
139+
before { allow(Spotlight::Engine.config).to receive_messages(rtl_enabled: false) }
140+
141+
it 'returns false' do
142+
expect(subject).to be false
143+
end
144+
end
145+
146+
context 'when rtl_enabled is true' do
147+
before { allow(Spotlight::Engine.config).to receive_messages(rtl_enabled: true) }
148+
149+
it 'returns true' do
150+
expect(subject).to be true
151+
end
152+
end
153+
154+
context 'when rtl_enabled is unset' do
155+
before { allow(Spotlight::Engine.config).to receive_messages(rtl_enabled: nil) }
156+
157+
it 'returns false' do
158+
expect(subject).to be false
159+
end
160+
end
161+
end
162+
163+
describe '#rtl_locale?' do
164+
subject { helper.rtl_locale? }
165+
166+
before { allow(Spotlight::Engine.config).to receive_messages(rtl_locales: %i[ar]) }
167+
168+
context 'when the locale is in the rtl_locales list' do
169+
before { allow(I18n).to receive(:locale).and_return(:ar) }
170+
171+
it 'returns true' do
172+
expect(subject).to be true
173+
end
174+
end
175+
176+
context 'when the locale is not in the rtl_locales list' do
177+
before { allow(I18n).to receive(:locale).and_return(:en) }
178+
179+
it 'returns false' do
180+
expect(subject).to be false
181+
end
182+
end
183+
end
102184
end

0 commit comments

Comments
 (0)