Skip to content
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

Redirect to referrer instead of root on locale set #743

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion app/controllers/spree/locale_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ def index
end

def set
redirect_to root_path(locale: params[:switch_to_locale])
if request.referrer
path = spree.routes.recognize_path(request.referrer)
path[:locale] = params[:switch_to_locale]
redirect_to url_for(path)
else
redirect_to root_path(locale: params[:switch_to_locale])
end
end
end
end
18 changes: 18 additions & 0 deletions spec/controllers/locales_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@
end
end
end

RSpec.describe Spree::LocaleController, type: :controller do
routes { Spree::Core::Engine.routes }

before do
reset_spree_preferences
SpreeI18n::Config.available_locales = [:en, :es]
end

describe "#set" do
it 'redirects to referrer' do
root = request.url
request.env['HTTP_REFERER'] = "#{root}/products/some-product"
post :set, params: { switch_to_locale: :es }
expect(response).to redirect_to("#{root}/es/products/some-product")
end
end
end