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.

Add code? to menu_item.rb

Matthew Kennedy1cc02afa1266
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 1f14b79298..60f71f1c8f 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -36,6 +36,14 @@ module Spree
item_type == 'Container'
end
+ def code?(item_code = nil)
+ if item_code
+ code == item_code
+ else
+ code.present?
+ end
+ end
+
private
def build_path
diff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rb
index 74493a9df7..fbd0acaa19 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -36,7 +36,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '.reset_link_attributes from URL to Taxon' do
+ describe '#reset_link_attributes from URL to Taxon' do
let(:i_a) do
create(:menu_item, name: 'Home', item_type: 'Link', menu: menu,
parent: menu.root, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
@@ -68,7 +68,32 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '.reset_link_attributes from URL to Home Page' do
+ describe '#code?' do
+ let(:coded_item){ create(:menu_item, name: 'Link 1', item_type: 'Container', menu: menu, code: 'some-code') }
+ let(:not_coded_item) { create(:menu_item, name: 'Home', item_type: 'Link', menu: menu) }
+
+ it 'returns true when the menu item has a matching code' do
+ expect(coded_item.code?('some-code')).to be true
+ end
+
+ it 'returns false when the menu item has a code but the code does not match' do
+ expect(coded_item.code?('Some-code')).to be false
+ end
+
+ it 'returns false when the menu item has no code' do
+ expect(not_coded_item.code?('some-code')).to be false
+ end
+
+ it 'returns true if no args are passed, and the item has a code' do
+ expect(coded_item.code?).to be true
+ end
+
+ it 'returns false if no args are passed, and the item has no code' do
+ expect(not_coded_item.code?).to be false
+ end
+ end
+
+ describe '#reset_link_attributes from URL to Home Page' do
let(:i_b) do
create(:menu_item, name: 'Home', item_type: 'Link', menu: menu,
parent: menu.root, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
@@ -83,7 +108,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '.reset_link_attributes from Link to Container' do
+ describe '#reset_link_attributes from Link to Container' do
let(:i_c) do
create(:menu_item, name: 'Home', item_type: 'Link', menu: menu,
parent: menu.root, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
@@ -141,7 +166,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '.paremeterize_code' do
+ describe '#paremeterize_code' do
let(:item) { create(:menu_item, name: 'URL', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'URL', code: 'My Fantastic Code') }
it 'paramatizes a code when one is given' do
@@ -149,7 +174,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '.ensure_item_belongs_to_root' do
+ describe '#ensure_item_belongs_to_root' do
let(:item_x) { create(:menu_item, name: 'URL', item_type: 'Link', menu: menu, linked_resource_type: 'URL', code: 'My Fantastic Code') }
it 'Sets new items parent_id to root.id' do
diff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
index 2f33c1f410..a58f390f98 100644
--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb
+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
@@ -1,7 +1,4 @@
<% if spree_menu.present? %>
- <% category = 'category' %>
- <% promo = 'promo' %>
-
<div class="h-100" role="navigation" aria-label="<%= Spree.t('nav_bar.desktop') %>">
<ul class="nav h-100 main-nav-bar">
<% spree_menu.children.each do |parent| %>
@@ -23,7 +20,7 @@
<% parent.children.each do |child| %>
<!-- Categories -->
- <% if child.container? && child.code == category %>
+ <% if child.container? && child.code?('category') %>
<div class="category-links my-4 mx-2">
<div class="category-links-header text-uppercase">
<%= child.name %>
@@ -41,7 +38,7 @@
<% end %>
<!-- Promos -->
- <% if child.container? && child.code == promo %>
+ <% if child.container? && child.code?('promo') %>
<% if child.children.present? %>
<% child.children.each do |promo_banner| %>
<%= spree_nav_link_tag(promo_banner, { class: "dropdown-item" }) do %>
diff --git a/frontend/app/views/spree/shared/_mobile_navigation.html.erb b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
index 80ac1af841..07ea1b9088 100644
--- a/frontend/app/views/spree/shared/_mobile_navigation.html.erb
+++ b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
@@ -39,7 +39,7 @@
</li>
<% parent.children.each do |child| %>
- <% if child.code == 'category' && child.item_type == 'Container' %>
+ <% if child.container? && child.code?('category') %>
<li class="text-muted mt-2"><%= child.name %></li>
<% if child.children.present? %>
<% child.children.each do |category_link| %>