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 Linked resource.

Matthew Kennedya0abc9a4a469
diff --git a/backend/app/views/spree/admin/cms_sections/edit.html.erb b/backend/app/views/spree/admin/cms_sections/edit.html.erb
index 708a93dad7..b47d8777aa 100644
--- a/backend/app/views/spree/admin/cms_sections/edit.html.erb
+++ b/backend/app/views/spree/admin/cms_sections/edit.html.erb
@@ -5,7 +5,7 @@
<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @cms_section } %>
-<%= form_for [:admin, @cms_page, @cms_section] do |f| %>
+<%= form_for [:admin, @cms_page, @cms_section], as: :cms_section, url: spree.admin_cms_page_cms_section_path(@cms_page, @cms_section) do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<%= render partial: 'spree/admin/shared/edit_resource_links' %>
<% end %>
diff --git a/core/app/models/concerns/spree/display_link.rb b/core/app/models/concerns/spree/display_link.rb
new file mode 100644
index 0000000000..ab28e755e0
--- /dev/null
+++ b/core/app/models/concerns/spree/display_link.rb
@@ -0,0 +1,28 @@
+module Spree
+ module DisplayLink
+ 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
diff --git a/core/app/models/spree/cms_section.rb b/core/app/models/spree/cms_section.rb
index b939db6cb9..28cc6df4cb 100644
--- a/core/app/models/spree/cms_section.rb
+++ b/core/app/models/spree/cms_section.rb
@@ -1,5 +1,7 @@
module Spree
class CmsSection < Spree::Base
+ include Spree::DisplayLink
+
acts_as_list scope: :cms_page
belongs_to :cms_page
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 8e9fe8c399..0b29396d34 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::DisplayLink
+
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
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 9898ed2c25..146d919a18 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -14,6 +14,7 @@ module Spree
has_many :products, through: :classifications
has_many :menu_items, as: :linked_resource
+ has_many :cms_sections, as: :linked_resource
has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon', dependent: :destroy
has_many :prototypes, through: :prototype_taxons, class_name: 'Spree::Prototype'