Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix `default_url_options` when store does not have `default_locale` (#12629)

Damian Kaczmarczykd317f0af0cf5
diff --git a/admin/app/views/spree/admin/stores/new.html.erb b/admin/app/views/spree/admin/stores/new.html.erb
index a53199f785..4859f52def 100644
--- a/admin/app/views/spree/admin/stores/new.html.erb
+++ b/admin/app/views/spree/admin/stores/new.html.erb
@@ -24,6 +24,11 @@
<%= f.collection_select :default_country_iso, Spree::Country.order(:name), :iso, :name, { }, { data: { controller: 'autocomplete-select', enable_button_target: 'input', action: 'store-form#updateCurrency' } } %>
</div>
+ <div class="form-group">
+ <%= f.label :default_locale %>
+ <%= f.select :default_locale, options_from_collection_for_select(all_locales_options, :last, :first, current_store.default_locale || I18n.locale), { }, { data: { controller: 'autocomplete-select', enable_button_target: 'input', action: 'store-form#updateCurrency' } } %>
+ </div>
+
<div class="form-group">
<%= f.label :default_currency, Spree.t(:currency) %>
<%= f.currency_select :default_currency, preferred_currencies, {}, { data: { controller: 'autocomplete-select', enable_button_target: 'input', store_form_target: 'currency' } } %>
diff --git a/admin/spec/controllers/spree/admin/stores_controller_spec.rb b/admin/spec/controllers/spree/admin/stores_controller_spec.rb
index 498f4147b1..a6baf01ef9 100644
--- a/admin/spec/controllers/spree/admin/stores_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/stores_controller_spec.rb
@@ -18,7 +18,8 @@ describe Spree::Admin::StoresController do
{
name: 'New UK Store',
default_currency: 'GBR',
- default_country_iso: 'GB'
+ default_country_iso: 'GB',
+ default_locale: 'en'
}
end
@@ -31,6 +32,7 @@ describe Spree::Admin::StoresController do
expect(store.name).to eq('New UK Store')
expect(store.default_currency).to eq('GBR')
expect(store.default_country).to eq(uk_country)
+ expect(store.default_locale).to eq('en')
end
end
@@ -41,7 +43,8 @@ describe Spree::Admin::StoresController do
{
name: 'New Store Name',
default_currency: 'GBR',
- default_country_iso: 'GB'
+ default_country_iso: 'GB',
+ default_locale: 'pl'
}
end
@@ -57,6 +60,7 @@ describe Spree::Admin::StoresController do
expect(store.reload.name).to eq('New Store Name')
expect(store.default_currency).to eq('GBR')
expect(store.default_country).to eq(uk_country)
+ expect(store.default_locale).to eq('pl')
end
context 'when removing assets' do
diff --git a/storefront/app/controllers/concerns/spree/locale_urls.rb b/storefront/app/controllers/concerns/spree/locale_urls.rb
index fbe324f752..76fdf2a931 100644
--- a/storefront/app/controllers/concerns/spree/locale_urls.rb
+++ b/storefront/app/controllers/concerns/spree/locale_urls.rb
@@ -9,7 +9,11 @@ module Spree
private
def default_url_options
- locale = current_locale == current_store.default_locale ? nil : current_locale
+ locale = if current_store.default_locale.nil? || current_locale == current_store.default_locale
+ nil
+ else
+ current_locale
+ end
super.merge(locale: locale, currency: currency_param)
end
diff --git a/storefront/spec/controllers/concerns/spree/locale_urls_spec.rb b/storefront/spec/controllers/concerns/spree/locale_urls_spec.rb
new file mode 100644
index 0000000000..b09d1f0bda
--- /dev/null
+++ b/storefront/spec/controllers/concerns/spree/locale_urls_spec.rb
@@ -0,0 +1,87 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe LocaleUrls, type: :controller do
+ controller(ActionController::Base) do
+ include Spree::LocaleUrls
+ include Spree::Core::ControllerHelpers::Locale
+
+ def index
+ render plain: 'index'
+ end
+ end
+
+ before do
+ allow(controller).to receive(:current_store).and_return(@default_store)
+ allow(controller).to receive(:current_locale).and_return('en')
+ allow(controller).to receive(:currency_param).and_return(nil)
+ allow(controller).to receive(:supported_locales).and_return(['en', 'fr', 'es'])
+ end
+
+ describe '#default_url_options' do
+ context 'when current locale is the same as default store locale' do
+ before do
+ allow(controller).to receive(:current_locale).and_return('en')
+ @default_store.update(default_locale: 'en')
+ end
+
+ it 'does not include locale in URL options' do
+ expect(controller.send(:default_url_options)[:locale]).to be_nil
+ end
+ end
+
+ context 'when current locale is different from default store locale' do
+ before do
+ allow(controller).to receive(:current_locale).and_return('fr')
+ @default_store.update(default_locale: 'en')
+ end
+
+ it 'includes locale in URL options' do
+ expect(controller.send(:default_url_options)[:locale]).to eq('fr')
+ end
+ end
+
+ context 'when default_locale is nil' do
+ before do
+ allow(controller).to receive(:current_locale).and_return('fr')
+ @default_store.update(default_locale: nil)
+ end
+
+ it 'does not include locale in URL options' do
+ expect(controller.send(:default_url_options)[:locale]).to be_nil
+ end
+ end
+
+ it 'includes currency in URL options' do
+ allow(controller).to receive(:currency_param).and_return('USD')
+ expect(controller.send(:default_url_options)[:currency]).to eq('USD')
+ end
+ end
+
+ describe '#redirect_to_default_locale' do
+ context 'when locale param is present but not supported' do
+ it 'redirects to URL without locale' do
+ allow(controller).to receive(:supported_locale?).with('xx').and_return(false)
+ expect(controller).to receive(:url_for).with({ 'locale' => nil, 'action' => 'index', 'controller' => 'anonymous' }).and_return('/anonymous/index')
+ get :index, params: { locale: 'xx' }
+ expect(response).to redirect_to('/anonymous/index')
+ end
+ end
+
+ context 'when locale param is blank' do
+ it 'does not redirect' do
+ get :index
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context 'when locale param is supported' do
+ it 'does not redirect' do
+ allow(controller).to receive(:supported_locale?).with('fr').and_return(true)
+ get :index, params: { locale: 'fr' }
+ expect(response).to have_http_status(:ok)
+ end
+ end
+ end
+ end
+end