Skip to content

fix: params[:path] can be nil sometimes #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/services/maglev/extract_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def call
protected

def extract_locale
segments = params[:path].split('/')
path = params[:path] || 'index'
segments = path.split('/')

return [default_locale, params[:path]] unless locales.include?(segments[0]&.to_sym)
return [default_locale, path] unless locales.include?(segments[0]&.to_sym)

[segments.shift, segments.empty? ? 'index' : segments.join('/')]
end
Expand Down
14 changes: 14 additions & 0 deletions spec/services/maglev/extract_locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
let(:params) { { path: 'index' } }
let(:locales) { %i[en fr] }

context 'the path is nil' do
let(:params) { { path: nil } }

it 'uses the default locale' do
expect(Maglev::I18n).to receive(:'current_locale=').with(:en)
subject
end

it "sets the path to 'index'" do
subject
expect(params[:path]).to eq 'index'
end
end

context "the path doesn't contain a locale" do
it 'uses the default locale' do
expect(Maglev::I18n).to receive(:'current_locale=').with(:en)
Expand Down