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.

Improve code for section type paths.

Matthew Kennedyca2b684f7bae
diff --git a/backend/app/helpers/spree/admin/cms_page_helper.rb b/backend/app/helpers/spree/admin/cms_page_helper.rb
index f373f8c13e..3ddcc35fa5 100644
--- a/backend/app/helpers/spree/admin/cms_page_helper.rb
+++ b/backend/app/helpers/spree/admin/cms_page_helper.rb
@@ -56,7 +56,7 @@ module Spree
formatted_types = []
Spree::CmsPage::PAGE_TYPES.each do |type|
- last_word = type.split('::', 4).last
+ last_word = type.split('::', 10).last
readable_type = last_word.gsub(/(?<=[a-z])(?=[A-Z])/, ' ')
formatted_types << [readable_type, type]
end
@@ -65,12 +65,12 @@ module Spree
end
def humanize_cms_type(obj)
- last_word = obj.type.split('::', 4).last
+ last_word = obj.type.split('::', 10).last
last_word.gsub(/(?<=[a-z])(?=[A-Z])/, ' ')
end
def parametize_cms_type(obj)
- last_word = obj.type.split('::', 4).last
+ last_word = obj.type.split('::', 10).last
last_word.parameterize(separator: '_')
end
end
diff --git a/backend/app/helpers/spree/admin/cms_section_helper.rb b/backend/app/helpers/spree/admin/cms_section_helper.rb
index 5f3dc7c400..f1981020ac 100644
--- a/backend/app/helpers/spree/admin/cms_section_helper.rb
+++ b/backend/app/helpers/spree/admin/cms_section_helper.rb
@@ -5,7 +5,7 @@ module Spree
formatted_types = []
Spree::CmsSection::TYPES.each do |type|
- last_word = type.split('::', 4).last
+ last_word = type.split('::', 10).last
readable_type = last_word.gsub(/(?<=[a-z])(?=[A-Z])/, ' ')
formatted_types << [readable_type, type]
end
diff --git a/backend/app/helpers/spree/admin/menu_helper.rb b/backend/app/helpers/spree/admin/menu_helper.rb
index 752cb1d1fd..bbd6718e85 100644
--- a/backend/app/helpers/spree/admin/menu_helper.rb
+++ b/backend/app/helpers/spree/admin/menu_helper.rb
@@ -25,7 +25,7 @@ module Spree
Spree::MenuItem::LINKED_RESOURCE_TYPE.each do |resource_type|
formatted = if Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? resource_type
- str = resource_type.split('::', 3).last
+ str = resource_type.split('::', 10).last
if str.starts_with?('Cms')
str.slice(3, 100)
else
diff --git a/backend/app/views/spree/admin/cms_pages/sections/_featured_atricle.html.erb b/backend/app/views/spree/admin/cms_pages/sections/_featured_atricle.html.erb
deleted file mode 100644
index 6742da5baa..0000000000
--- a/backend/app/views/spree/admin/cms_pages/sections/_featured_atricle.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-<div class="<%= width %>" data-section-id="<%= section.id %>">
- <div class="row m-0 p-0 py-3 mb-3 border bg-white">
- <div class="col" style="width:30px; max-width: 70px; text-align: center;">
- <span class="move-handle"><%= svg_icon(name: 'sort.svg', width: '20', height: '20') %></span>
- </div>
- <div class="col text-center">
- <div>Name: <%= section.name %></div>
- <div>Type: <%= section.type %></div>
- <div class="m-auto" style="width:50px;">
- <% if section.icon.present? %>
- <div class="cms_section_preview_image_holder">
- <%= image_tag main_app.url_for(section.icon.attachment) %>
- </div>
- <% end %>
- </div>
- </div>
- <div class="col actions" style="width:30px; max-width: 70px; text-align: center;">
- <%= link_to_edit(section, no_text: true, url: spree.edit_admin_cms_page_cms_section_path(@cms_page, section)) %>
- <%= link_to_delete(section, no_text: true, url: spree.admin_cms_page_cms_section_path(@cms_page, section)) if can?(:destroy, section) %>
- </div>
- </div>
-</div>
diff --git a/backend/app/views/spree/admin/cms_pages/sections/_hero.html.erb b/backend/app/views/spree/admin/cms_pages/sections/_hero.html.erb
deleted file mode 100644
index 0cc7c6d60b..0000000000
--- a/backend/app/views/spree/admin/cms_pages/sections/_hero.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-<div class="<%= width %>" data-section-id="<%= section.id %>">
- <div class="row m-0 p-0 py-3 mb-3 border bg-white">
- <div class="col" style="width:30px; max-width: 70px; text-align: center;">
- <span class="move-handle"><%= svg_icon(name: 'sort.svg', width: '20', height: '20') %></span>
- </div>
- <div class="col text-center">
- <div>Name: <%= section.name %></div>
- <div>Type: <%= humanize_cms_type(section) %></div>
- <div class="m-auto" style="width:50px;">
- <% if section.icon.present? %>
- <div class="cms_section_preview_image_holder">
- <%= image_tag main_app.url_for(section.icon.attachment) %>
- </div>
- <% end %>
- </div>
- </div>
- <div class="col actions" style="width:30px; max-width: 70px; text-align: center;">
- <%= link_to_edit(section, no_text: true, url: spree.edit_admin_cms_page_cms_section_path(@cms_page, section)) %>
- <%= link_to_delete(section, no_text: true, url: spree.admin_cms_page_cms_section_path(@cms_page, section)) if can?(:destroy, section) %>
- </div>
- </div>
-</div>
diff --git a/backend/app/views/spree/admin/cms_pages/sections/_promo.html.erb b/backend/app/views/spree/admin/cms_pages/sections/_promo.html.erb
deleted file mode 100644
index 6829f78149..0000000000
--- a/backend/app/views/spree/admin/cms_pages/sections/_promo.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-<div class="<%= width %>" data-section-id="<%= section.id %>">
- <div class="row m-0 p-0 py-3 mb-3 border bg-white">
- <div class="col p-0 cms_section_action_col">
- <span class="move-handle"><%= svg_icon(name: 'sort.svg', width: '20', height: '20') %></span>
- </div>
- <div class="col text-center p-0">
- <div>Name: <%= section.name %></div>
- <div>Type: <%= section.type %></div>
- <div class="m-auto" style="width:50px;">
- <% if section.icon.present? %>
- <div class="cms_section_preview_image_holder">
- <%= image_tag main_app.url_for(section.icon.attachment) %>
- </div>
- <% end %>
- </div>
- </div>
- <div class="col p-0 actions cms_section_action_col">
- <%= link_to_edit(section, no_text: true, url: spree.edit_admin_cms_page_cms_section_path(@cms_page, section)) %>
- <%= link_to_delete(section, no_text: true, url: spree.admin_cms_page_cms_section_path(@cms_page, section)) if can?(:destroy, section) %>
- </div>
- </div>
-</div>
diff --git a/backend/app/views/spree/admin/cms_pages/sections/_text_block.html.erb b/backend/app/views/spree/admin/cms_pages/sections/_text_block.html.erb
deleted file mode 100644
index 73e319cc46..0000000000
--- a/backend/app/views/spree/admin/cms_pages/sections/_text_block.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-<div class="<%= width %>" data-section-id="<%= section.id %>">
- <div class="row m-0 p-0 py-3 mb-3 border bg-white">
- <div class="col p-0 cms_section_action_col">
- <span class="move-handle"><%= svg_icon(name: 'sort.svg', width: '20', height: '20') %></span>
- </div>
- <div class="col text-center p-0">
- <div>Name: <%= section.name %></div>
- <div>Type: <%= section.type %></div>
- <div class="m-auto">
- <% if section.icon.present? %>
- <div class="cms_section_preview_image_holder">
- <%= image_tag main_app.url_for(section.icon.attachment) %>
- </div>
- <% end %>
- </div>
- </div>
- <div class="col p-0 actions cms_section_action_col">
- <%= link_to_edit(section, no_text: true, url: spree.edit_admin_cms_page_cms_section_path(@cms_page, section)) %>
- <%= link_to_delete(section, no_text: true, url: spree.admin_cms_page_cms_section_path(@cms_page, section)) if can?(:destroy, section) %>
- </div>
- </div>
-</div>
diff --git a/backend/app/views/spree/admin/cms_sections/_form.html.erb b/backend/app/views/spree/admin/cms_sections/_form.html.erb
index 665db9debb..488ba712c9 100644
--- a/backend/app/views/spree/admin/cms_sections/_form.html.erb
+++ b/backend/app/views/spree/admin/cms_sections/_form.html.erb
@@ -57,7 +57,7 @@
<% if @cms_section.type.present? %>
<div class="row">
<div class="col-12">
- <%= render "spree/admin/cms_sections/types/#{@cms_section.type.parameterize(separator: '_')}", f: f %>
+ <%= render "spree/admin/cms_sections/types/#{spree_resource_path(@cms_section)}", f: f %>
</div>
</div>
<% end %>
diff --git a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_carousel.html.erb b/backend/app/views/spree/admin/cms_sections/types/_carousel.html.erb
similarity index 100%
rename from backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_carousel.html.erb
rename to backend/app/views/spree/admin/cms_sections/types/_carousel.html.erb
diff --git a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_categorybannersquare.html.erb b/backend/app/views/spree/admin/cms_sections/types/_category_banner_square.html.erb
similarity index 100%
rename from backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_categorybannersquare.html.erb
rename to backend/app/views/spree/admin/cms_sections/types/_category_banner_square.html.erb
diff --git a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_featuredarticle.html.erb b/backend/app/views/spree/admin/cms_sections/types/_featured_article.html.erb
similarity index 100%
rename from backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_featuredarticle.html.erb
rename to backend/app/views/spree/admin/cms_sections/types/_featured_article.html.erb
diff --git a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_hero.html.erb b/backend/app/views/spree/admin/cms_sections/types/_hero.html.erb
similarity index 100%
rename from backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_hero.html.erb
rename to backend/app/views/spree/admin/cms_sections/types/_hero.html.erb
diff --git a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_promo.html.erb b/backend/app/views/spree/admin/cms_sections/types/_promo.html.erb
similarity index 100%
rename from backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_promo.html.erb
rename to backend/app/views/spree/admin/cms_sections/types/_promo.html.erb
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index 00b0e0c75c..b8f98150ff 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -18,6 +18,12 @@ module Spree
end.sort_by { |c| c.name.parameterize }
end
+ def spree_resource_path(resource)
+ last_word = resource.class.name.split('::', 10).last
+
+ last_word.underscore.humanize.parameterize(separator: '_')
+ end
+
def display_price(product_or_variant)
product_or_variant.
price_in(current_currency).
diff --git a/frontend/app/helpers/spree/cms_pages_helper.rb b/frontend/app/helpers/spree/cms_pages_helper.rb
index e39152823b..fea5193001 100644
--- a/frontend/app/helpers/spree/cms_pages_helper.rb
+++ b/frontend/app/helpers/spree/cms_pages_helper.rb
@@ -5,8 +5,6 @@ module Spree
end
def build_section(section)
- preview_type = section.type.parameterize(separator: '_')
-
if section.full_width_on_small
case section.width
when 'Half'
@@ -47,10 +45,10 @@ module Spree
end
end
- render "spree/shared/cms/sections/#{preview_type}", section: section,
- width: css_width,
- boundary: boundary,
- element: element
+ render "spree/shared/cms/sections/#{spree_resource_path(section)}", section: section,
+ width: css_width,
+ boundary: boundary,
+ element: element
end
end
end
diff --git a/frontend/app/views/spree/home/index.html.erb b/frontend/app/views/spree/home/index.html.erb
index 56e57a5ae6..b2b6072f7f 100644
--- a/frontend/app/views/spree/home/index.html.erb
+++ b/frontend/app/views/spree/home/index.html.erb
@@ -3,6 +3,8 @@
<% if @homepage %>
<div class="container">
<div id="featurePageContent" class="row">
+
+
<% @homepage.cms_sections.each do |section| %>
<%= build_section(section) %>
<% end %>
diff --git a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_carousel.html.erb b/frontend/app/views/spree/shared/cms/sections/_carousel.html.erb
similarity index 100%
rename from frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_carousel.html.erb
rename to frontend/app/views/spree/shared/cms/sections/_carousel.html.erb
diff --git a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_categorybannersquare.html.erb b/frontend/app/views/spree/shared/cms/sections/_category_banner_square.html.erb
similarity index 100%
rename from frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_categorybannersquare.html.erb
rename to frontend/app/views/spree/shared/cms/sections/_category_banner_square.html.erb
diff --git a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_featuredarticle.html.erb b/frontend/app/views/spree/shared/cms/sections/_featured_article.html.erb
similarity index 100%
rename from frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_featuredarticle.html.erb
rename to frontend/app/views/spree/shared/cms/sections/_featured_article.html.erb
diff --git a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_hero.html.erb b/frontend/app/views/spree/shared/cms/sections/_hero.html.erb
similarity index 100%
rename from frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_hero.html.erb
rename to frontend/app/views/spree/shared/cms/sections/_hero.html.erb
diff --git a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_promo.html.erb b/frontend/app/views/spree/shared/cms/sections/_promo.html.erb
similarity index 100%
rename from frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_promo.html.erb
rename to frontend/app/views/spree/shared/cms/sections/_promo.html.erb