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 logic from view to helpers.

Matthew Kennedy3feb6284595c
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 8c34cc03f9..1f14b79298 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -32,6 +32,10 @@ module Spree
where(linked_resource_type: resorce.class.name, linked_resource_id: resorce.id).each(&:save!)
end
+ def container?
+ item_type == 'Container'
+ 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 79d74898c4..74493a9df7 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -55,6 +55,19 @@ describe Spree::MenuItem, type: :model do
end
end
+ describe '#container?' do
+ let(:container_item){ create(:menu_item, name: 'Link 1', item_type: 'Container', menu: menu) }
+ let(:link_item) { create(:menu_item, name: 'Home', item_type: 'Link', menu: menu) }
+
+ it 'returns true when the menu item is of type container' do
+ expect(container_item.container?).to be true
+ end
+
+ it 'returns false when the menu item is of type Link' do
+ expect(link_item.container?).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,
diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rb
index 96d452adcd..fbd7e0bf27 100644
--- a/frontend/app/helpers/spree/navigation_helper.rb
+++ b/frontend/app/helpers/spree/navigation_helper.rb
@@ -82,6 +82,24 @@ module Spree
(defined?(should_render_currency_dropdown?) && should_render_currency_dropdown?)
end
+ def spree_nav_link_tag(item, opts = {})
+ if item.new_window
+ target = opts[:target] || '_blank'
+ rel = opts[:rel] || 'noopener noreferrer'
+ end
+
+ if block_given?
+ link_to spree_localized_item_link(item), target: target, rel: rel, class: opts[:class],
+ id: opts[:id], data: opts[:data], aria: opts[:aria] do
+ yield
+ end
+
+ else
+ link_to item.name, spree_localized_item_link(item), target: target, rel: rel, class: opts[:class],
+ id: opts[:id], data: opts[:data], aria: opts[:aria]
+ end
+ end
+
private
def spree_navigation_data_cache_key
diff --git a/frontend/app/views/spree/shared/_footer.html.erb b/frontend/app/views/spree/shared/_footer.html.erb
index 54366ae7d3..1eb29b74a3 100644
--- a/frontend/app/views/spree/shared/_footer.html.erb
+++ b/frontend/app/views/spree/shared/_footer.html.erb
@@ -53,19 +53,15 @@
<% spree_menu('footer').children.each do |parent| %>
<div class="col-3">
<div class="footer-spree-label">
- <% if parent.item_type == 'Container' %>
+ <% if parent.container? %>
<%= parent.name %>
<% else %>
- <%= link_to parent.name, spree_localized_item_link(parent) %>
+ <%= spree_nav_link_tag(parent) %>
<% end %>
</div>
<% parent.children.each do |child| %>
<div class="pt-2">
- <% if child.new_window %>
- <%= link_to child.name, spree_localized_item_link(child), target: '_blank', rel: 'noopener noreferrer' %>
- <% else %>
- <%= link_to child.name, spree_localized_item_link(child) %>
- <% end %>
+ <%= spree_nav_link_tag(child) %>
</div>
<% end %>
</div>
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 c413e391dc..2f33c1f410 100644
--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb
+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
@@ -13,7 +13,7 @@
<% if parent.item_type == "Container" %>
<%= content_tag :span, parent.name, class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr %>
<% else %>
- <%= link_to(parent.name, spree_localized_item_link(parent), class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %>
+ <%= spree_nav_link_tag(parent, { class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr }) %>
<% end %>
<% if has_dropdown %>
@@ -23,7 +23,7 @@
<% parent.children.each do |child| %>
<!-- Categories -->
- <% if child.code == category && child.item_type == 'Container' %>
+ <% if child.container? && child.code == category %>
<div class="category-links my-4 mx-2">
<div class="category-links-header text-uppercase">
<%= child.name %>
@@ -32,7 +32,7 @@
<ul class="pl-0">
<% child.children.each do |category_link| %>
<li>
- <%= link_to category_link.name, spree_localized_item_link(category_link), class: "text-uppercase main-nav-bar-category-links dropdown-item truncate" %>
+ <%= spree_nav_link_tag(category_link, { class: "text-uppercase main-nav-bar-category-links dropdown-item truncate" }) %>
</li>
<% end %>
</ul>
@@ -41,10 +41,10 @@
<% end %>
<!-- Promos -->
- <% if child.code == promo && child.item_type == 'Container' %>
+ <% if child.container? && child.code == promo %>
<% if child.children.present? %>
<% child.children.each do |promo_banner| %>
- <%= link_to spree_localized_item_link(promo_banner), class: "dropdown-item" do %>
+ <%= spree_nav_link_tag(promo_banner, { class: "dropdown-item" }) do %>
<div class="category-image text-center my-4 mx-3">
<% if promo_banner.icon.present? %>
<%= image_tag main_app.url_for(promo_banner.icon.attachment), alt: promo_banner.icon.alt ,title: promo_banner.subtitle %>
diff --git a/frontend/app/views/spree/shared/_mobile_navigation.html.erb b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
index 3a76b0c573..80ac1af841 100644
--- a/frontend/app/views/spree/shared/_mobile_navigation.html.erb
+++ b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
@@ -20,7 +20,7 @@
<ul class="list-unstyled position-relative h-100 mobile-navigation-list">
<% spree_menu.children.each do |parent| %>
<li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">
- <% if parent.item_type == "Container" %>
+ <% if parent.container? %>
<%= content_tag :span, parent.name %>
<% else %>
<%= link_to parent.name, spree_localized_item_link(parent), class: 'w-75' %>
diff --git a/frontend/app/views/spree/shared/_nav_bar.html.erb b/frontend/app/views/spree/shared/_nav_bar.html.erb
index 182a308680..8eb320f6b5 100644
--- a/frontend/app/views/spree/shared/_nav_bar.html.erb
+++ b/frontend/app/views/spree/shared/_nav_bar.html.erb
@@ -1,4 +1,4 @@
-<ul id="nav-bar" class="nav align-items-center d-flex flex-nowrap justify-content-end navbar-right" data-turbolinks-permanent>
+<ul id="nav-bar" class="nav align-items-center d-flex flex-nowrap justify-content-end navbar-right">
<li>
<div class="navbar-right-search-menu">
<button type="button" class="navbar-right-dropdown-toggle search-icons" aria-label="<%= Spree.t('nav_bar.show_search') %>">
@@ -28,7 +28,7 @@
</div>
</li>
<% end %>
- <li id="link-to-cart">
+ <li id="link-to-cart" data-turbo-permanent data-turbolinks-permanent>
<%= render 'spree/shared/cart', class: 'd-inline-block cart-icon', size: 36, item_count: 0 %>
</li>
<%= render partial: 'spree/shared/internationalization_options' %>