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 link method from Spree::MenuItem to a concern.

Matthew Kennedy9268b9d6254e
diff --git a/core/app/models/concerns/spree/link.rb b/core/app/models/concerns/spree/link.rb
new file mode 100644
index 0000000000..efe876a970
--- /dev/null
+++ b/core/app/models/concerns/spree/link.rb
@@ -0,0 +1,30 @@
+module Spree
+ module Link
+ extend ActiveSupport::Concern
+
+ def link
+ case linked_resource_type
+ when 'Spree::Taxon'
+ return if linked_resource.nil?
+
+ if defined?(SpreeFrontend)
+ Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)
+ else
+ "/#{Spree::Config[:storefront_taxons_path]}/#{linked_resource.permalink}"
+ end
+ when 'Spree::Product'
+ return if linked_resource.nil?
+
+ if defined?(SpreeFrontend)
+ Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)
+ else
+ "/#{Spree::Config[:storefront_products_path]}/#{linked_resource.slug}"
+ end
+ when 'Home Page'
+ '/'
+ when 'URL'
+ destination
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 03a3d9f2aa..4d2c72fe1c 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -1,5 +1,7 @@
module Spree
class MenuItem < Spree::Base
+ include Spree::Link
+
acts_as_nested_set dependent: :destroy
belongs_to :menu, touch: true
@@ -40,37 +42,8 @@ module Spree
end
end
- def link
- case linked_resource_type
- when 'Spree::Taxon'
- return if linked_resource.nil?
-
- if frontend_available?
- Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)
- else
- "/#{Spree::Config[:storefront_taxons_path]}/#{linked_resource.permalink}"
- end
- when 'Spree::Product'
- return if linked_resource.nil?
-
- if frontend_available?
- Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)
- else
- "/#{Spree::Config[:storefront_products_path]}/#{linked_resource.slug}"
- end
- when 'Home Page'
- '/'
- when 'URL'
- destination
- end
- end
-
private
- def frontend_available?
- Spree::Core::Engine.frontend_available?
- end
-
def reset_link_attributes
if linked_resource_type_changed? || item_type == 'Container'
self.linked_resource_id = nil