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.

Move I18n.local to front end. Set menu default_scope to return descending order on the created_at for the oldest menu edge case fallback.

Matthew Kennedy338f670d5827
diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erb
index 7839e48c88..0d67930f12 100644
--- a/backend/app/views/spree/admin/menus/index.html.erb
+++ b/backend/app/views/spree/admin/menus/index.html.erb
@@ -78,9 +78,7 @@
<%= locale_full_name(menu.locale) %>
</td>
<td class="text-center">
- <% if default_menu_for_store?(menu) %>
- <%= svg_icon name: "tick.svg", width: '20', height: '20' %>
- <% end %>
+ <%= active_badge(default_menu_for_store?(menu)) %>
</td>
<td class="actions" data-hook="admin_menus_index_row_actions">
<span class="d-flex justify-content-end">
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index d622999c97..0e3bd80375 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -22,14 +22,16 @@ module Spree
has_one :root, -> { where(parent_id: nil) }, class_name: 'Spree::MenuItem', dependent: :destroy
+ default_scope { order(created_at: :desc) }
+
scope :by_store, ->(store) { where(store: store) }
scope :by_locale, ->(locale) { where(locale: locale) }
self.whitelisted_ransackable_attributes = %w[name location locale store_id]
MENU_LOCATIONS_PARAMETERIZED.each do |location_name|
- define_singleton_method("for_#{location_name}") do |store|
- menu = find_by(location: location_name, locale: I18n.locale.to_s) ||
+ define_singleton_method("for_#{location_name}") do |locale, store|
+ menu = find_by(location: location_name, locale: locale.to_s) ||
store.default_menu(location_name) ||
find_by(location: location_name)
if menu.present?
diff --git a/core/spec/models/spree/menu_spec.rb b/core/spec/models/spree/menu_spec.rb
index 1bdfdf0956..e35f50aa9a 100644
--- a/core/spec/models/spree/menu_spec.rb
+++ b/core/spec/models/spree/menu_spec.rb
@@ -37,26 +37,20 @@ describe Spree::Menu, type: :model do
let!(:menu_fr) { create(:menu, name: 'Main Menu FR', store: fr_store, locale: 'fr') }
context 'when the I18n.locale is set to a none default laguage and a menu is available' do
- before { I18n.locale = :en }
-
it 'returns a menu in the correct locale' do
- expect(described_class.for_header(fr_store).name).to eql('Main Menu EN')
+ expect(described_class.for_header('en', fr_store).name).to eql('Main Menu EN')
end
end
context 'when the I18n.locale is set to a none default laguage and a menu is not available' do
- before { I18n.locale = :de }
-
it 'returns a menu in the current stores default locale if one is available' do
- expect(described_class.for_header(fr_store).name).to eql('Main Menu FR')
+ expect(described_class.for_header('de',fr_store).name).to eql('Main Menu FR')
end
end
context 'when the I18n.locale is set to the default laguage and a menu is available' do
- before { I18n.locale = :fr }
-
it 'returns a the default menu' do
- expect(described_class.for_header(fr_store).name).to eql('Main Menu FR')
+ expect(described_class.for_header('fr', fr_store).name).to eql('Main Menu FR')
end
end
end
diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rb
index 557ce18143..9d417377d7 100644
--- a/frontend/app/helpers/spree/navigation_helper.rb
+++ b/frontend/app/helpers/spree/navigation_helper.rb
@@ -57,7 +57,7 @@ module Spree
method_name = "for_#{location}"
if available_menus.respond_to?(method_name) && Spree::Menu::MENU_LOCATIONS_PARAMETERIZED.include?(location)
- available_menus.send(method_name, current_store)
+ available_menus.send(method_name, I18n.locale, current_store)
end
end