Skip to content

Commit 7f43c55

Browse files
committed
fix: don't raise an exception and use the index page if for some reasons, the params[:path] is empty
1 parent 2235e14 commit 7f43c55

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

app/services/maglev/extract_locale.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ def call
2222
protected
2323

2424
def extract_locale
25-
segments = params[:path].split('/')
25+
path = params[:path] || 'index'
26+
segments = path.split('/')
2627

27-
return [default_locale, params[:path]] unless locales.include?(segments[0]&.to_sym)
28+
return [default_locale, path] unless locales.include?(segments[0]&.to_sym)
2829

2930
[segments.shift, segments.empty? ? 'index' : segments.join('/')]
3031
end

spec/services/maglev/extract_locale_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
let(:params) { { path: 'index' } }
1010
let(:locales) { %i[en fr] }
1111

12+
context 'the path is nil' do
13+
let(:params) { { path: nil } }
14+
15+
it 'uses the default locale' do
16+
expect(Maglev::I18n).to receive(:'current_locale=').with(:en)
17+
subject
18+
end
19+
20+
it "sets the path to 'index'" do
21+
subject
22+
expect(params[:path]).to eq 'index'
23+
end
24+
end
25+
1226
context "the path doesn't contain a locale" do
1327
it 'uses the default locale' do
1428
expect(Maglev::I18n).to receive(:'current_locale=').with(:en)

0 commit comments

Comments
 (0)