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.

Ensure an empty string is returned for a blank href.

Matthew Kennedyf16160e95ace
diff --git a/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb b/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb
index 6551d4cbca..3f46b2ad64 100644
--- a/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb
+++ b/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb
@@ -6,7 +6,7 @@
<div class="d-flex align-items-center overflow-hidden w-100">
<div class="overflow-hidden">
<%= item.name %>
- <% if item.link %>
+ <% if item.link.present? %>
<span class="badge badge-light badge-light-muted-text">-> <%= item.link %></span>
<% end %>
<small class="form-text text-muted mt-0">
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 8d4a2746ea..91f3f0e50b 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -43,7 +43,7 @@ module Spree
def link
case linked_resource_type
when 'Spree::Taxon'
- return if linked_resource.nil?
+ return '' if linked_resource.nil?
if frontend_available?
Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)
@@ -51,7 +51,7 @@ module Spree
"/#{Spree::Config[:storefront_taxon_path]}/#{linked_resource.permalink}"
end
when 'Spree::Product'
- return if linked_resource.nil?
+ return '' if linked_resource.nil?
if frontend_available?
Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)
@@ -61,7 +61,7 @@ module Spree
when 'Home Page'
'/'
when 'URL'
- destination
+ destination || ''
end
end
diff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rb
index 52ac5e3c67..a8038bbafb 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -107,10 +107,11 @@ describe Spree::MenuItem, type: :model do
describe '#link' do
let(:product) { create(:product) }
let(:taxon) { create(:taxon) }
- let(:item_url) { create(:menu_item, name: 'URL To Random Site', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'URL', destination: 'https://some-other-website.com') }
- let(:item_home) { create(:menu_item, name: 'Home', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'Home Page') }
- let(:item_product) { create(:menu_item, name: product.name, item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'Spree::Product') }
- let(:item_taxon) { create(:menu_item, name: taxon.name, item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'Spree::Taxon') }
+ let(:item_url) { create(:menu_item, name: 'URL To Random Site', item_type: 'Link', menu: menu, linked_resource_type: 'URL', destination: 'https://some-other-website.com') }
+ let(:item_empty_url) { create(:menu_item, name: 'URL To Random Site', item_type: 'Link', menu: menu, linked_resource_type: 'URL', destination: nil) }
+ let(:item_home) { create(:menu_item, name: 'Home', item_type: 'Link', menu: menu, linked_resource_type: 'Home Page') }
+ let(:item_product) { create(:menu_item, name: product.name, item_type: 'Link', menu: menu, linked_resource_type: 'Spree::Product') }
+ let(:item_taxon) { create(:menu_item, name: taxon.name, item_type: 'Link', menu: menu, linked_resource_type: 'Spree::Taxon') }
it 'returns correct taxon path' do
item_taxon.update(linked_resource: taxon)
@@ -118,11 +119,11 @@ describe Spree::MenuItem, type: :model do
expect(item_taxon.link).to eql "/t/#{taxon.permalink}"
end
- it 'returns nil for destination when taxon is removed' do
+ it 'returns empty string for destination when taxon is removed' do
item_taxon.update(linked_resource: taxon)
item_taxon.update(linked_resource_id: nil)
- expect(item_taxon.destination).to be nil
+ expect(item_taxon.link).to eq ''
end
it 'returns correct product path' do
@@ -131,20 +132,24 @@ describe Spree::MenuItem, type: :model do
expect(item_product.link).to eql "/products/#{product.slug}"
end
- it 'returns nil for destination when product is removed' do
+ it 'returns empty string for destination when product is removed' do
item_product.update(linked_resource: product)
item_product.update(linked_resource_id: nil)
- expect(item_product.link).to be nil
+ expect(item_product.link).to eq ''
end
it 'returns correct root path' do
- expect(item_home.link).to eql '/'
+ expect(item_home.link).to eq '/'
end
it 'returns correct URL path' do
expect(item_url.link).to eql 'https://some-other-website.com'
end
+
+ it 'returns empty string when URL is nil' do
+ expect(item_empty_url.link).to eq ''
+ end
end
describe '#paremeterize_code' do