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.

Featured posts section (#12882)

Damian Kaczmarczyk58a77b2a93f2
diff --git a/admin/app/views/spree/admin/page_sections/forms/_featured_posts.html.erb b/admin/app/views/spree/admin/page_sections/forms/_featured_posts.html.erb
index 26ea927c22..3f4e5ec1bf 100644
--- a/admin/app/views/spree/admin/page_sections/forms/_featured_posts.html.erb
+++ b/admin/app/views/spree/admin/page_sections/forms/_featured_posts.html.erb
@@ -21,11 +21,6 @@
<%= f.select :preferred_description_alignment, options_for_select([ ["Left", "left"], ["Center", "center"], ["Right", "right"] ], @page_section.preferred_description_alignment ), {}, data: { action: 'auto-submit#submit' }, class: "custom-select" %>
</div>
-<div class="form-group">
- <%= f.label :preferred_posts_to_show, Spree.t('admin.page_builder.posts_to_show') %>
- <%= f.select :preferred_posts_to_show, options_for_select([["Newest", "newest"], ["Most popular", "most_popular"]], @page_section.preferred_posts_to_show), {}, data: { action: 'auto-submit#submit' }, class: "custom-select" %>
-</div>
-
<div class="form-group">
<div data-controller="better-slider">
<label><%= Spree.t('admin.page_builder.maximum_posts_to_show') %>: <span data-better-slider-target="currentValueLabel"></span></label>
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index 9004328546..d7eb089711 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -144,7 +144,6 @@ en:
maximum_products_to_show: Maximum products to show
nav_centered: Nav centered
not_set: Not set
- posts_to_show: Posts to show
separated: Separated
show_more_button: Show explore category button
show_taxon_image: Show taxon image
diff --git a/core/app/models/spree/page_sections/featured_posts.rb b/core/app/models/spree/page_sections/featured_posts.rb
index a7e740344a..38e70d7c8c 100644
--- a/core/app/models/spree/page_sections/featured_posts.rb
+++ b/core/app/models/spree/page_sections/featured_posts.rb
@@ -19,6 +19,10 @@ module Spree
'news'
end
+ def posts
+ Spree::Post.published.by_newest.limit(preferred_max_posts_to_show)
+ end
+
private
def make_posts_to_show_valid
diff --git a/core/app/models/spree/post.rb b/core/app/models/spree/post.rb
index 0e1723985c..3e1a294e61 100644
--- a/core/app/models/spree/post.rb
+++ b/core/app/models/spree/post.rb
@@ -54,6 +54,7 @@ module Spree
# Scopes
#
scope :published, -> { where(published_at: [..Time.current]) }
+ scope :by_newest, -> { order(created_at: :desc) }
delegate :name, to: :author, prefix: true, allow_nil: true
delegate :title, to: :post_category, prefix: true, allow_nil: true
diff --git a/core/lib/spree/testing_support/factories/page_section_factory.rb b/core/lib/spree/testing_support/factories/page_section_factory.rb
index 640f268bde..be8a590319 100644
--- a/core/lib/spree/testing_support/factories/page_section_factory.rb
+++ b/core/lib/spree/testing_support/factories/page_section_factory.rb
@@ -22,5 +22,7 @@ FactoryBot.define do
factory :video_page_section, class: Spree::PageSections::Video
factory :image_with_text_page_section, class: Spree::PageSections::ImageWithText
+
+ factory :featured_posts_page_section, class: Spree::PageSections::FeaturedPosts
end
end
diff --git a/core/spec/models/spree/page_sections/featured_posts_spec.rb b/core/spec/models/spree/page_sections/featured_posts_spec.rb
new file mode 100644
index 0000000000..fbf981ee73
--- /dev/null
+++ b/core/spec/models/spree/page_sections/featured_posts_spec.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+RSpec.describe Spree::PageSections::FeaturedPosts do
+ describe '#posts' do
+ subject(:result) { section.posts }
+
+ let!(:posts) { create_list(:post, 3, published_at: nil) }
+ let!(:published_posts) { create_list(:post, 3, published_at: Time.current) }
+ let(:section) { build(:featured_posts_page_section, preferred_max_posts_to_show: 2) }
+
+ it 'returns newsest posts with limit' do
+ expect(result).not_to include(*posts)
+ expect(result).to eq(published_posts.sort_by(&:published_at).reverse!.first(2))
+ end
+ end
+end
diff --git a/storefront/app/views/themes/default/spree/page_sections/_featured_posts.html.erb b/storefront/app/views/themes/default/spree/page_sections/_featured_posts.html.erb
new file mode 100644
index 0000000000..36d1e57706
--- /dev/null
+++ b/storefront/app/views/themes/default/spree/page_sections/_featured_posts.html.erb
@@ -0,0 +1,56 @@
+<div style='<%= section_styles(section) %>' class='animate-fadeIn'>
+ <div class='page-container'>
+ <% heading_size =
+ case section.preferred_heading_size
+ when "small"
+ "text-base font-medium"
+ when "medium"
+ "text-lg lg:text-xl font-medium"
+ when "large"
+ "text-xl lg:text-2xl font-medium"
+ end %>
+ <% if section.preferred_heading.present? %>
+ <div class='mb-8 flex flex-col'>
+ <h3
+ class='
+ <%= heading_size %> featured-taxon--title font-medium
+ text-<%= section.preferred_heading_alignment %>
+ '
+ data-title="<%= section.preferred_heading.downcase %>"
+ style='<%= section_heading_styles(section) %>'
+ >
+ <%= section.preferred_heading %>
+ </h3>
+
+ <% if section.description.present? %>
+ <div class='pt-4 text-<%= section.preferred_description_alignment %>'>
+ <%= section.description %>
+ </div>
+ <% end %>
+ </div>
+ <% end %>
+ <div class='flex h-full relative'>
+ <div
+ class='
+ swiper-container overflow-hidden flex-1 pr-8 lg:pr-0 -mx-4 lg:mx-0 flex flex-col
+ lg:flex-col-reverse
+ '
+ data-controller='carousel'
+ data-carousel-options-value='{ "slidesPerView": 1, "centeredSlides": false, "spaceBetween": 16, "grabCursor": true, "breakpoints": { "768": { "slidesPerView": 3, "spaceBetween": 24 } }, "navigation": { "nextEl": ".swiper-custom-button-next-<%= section.id %>", "prevEl": ".swiper-custom-button-prev-<%= section.id %>" } }'
+ >
+ <div class='swiper-wrapper px-4 lg:px-0 h-auto'>
+ <%= render partial: "spree/posts/post",
+ collection: section.posts,
+ cached: spree_base_cache_scope,
+ as: :post %>
+ </div>
+ <%= button_tag class: "absolute p-2 bg-white rounded-full z-10 border border-accent left-0 disabled:hidden hover:border-primary ml-2 lg:ml-0 swiper-custom-button-prev-#{section.id} block top-[100px]", aria: { label: "Previous posts" }, style: "transform: translate(-50%, -50%);" do %>
+ <%= render "spree/shared/icons/chevron" %>
+ <% end %>
+ <%= button_tag class: "absolute p-2 bg-white rounded-full z-10 border border-accent right-0 disabled:hidden hover:border-primary mr-2 lg:mr-0 swiper-custom-button-next-#{section.id} block top-[100px]", aria: { label: "Next posts" }, style: "transform: translate(50%, -50%);" do %>
+ <%= render "spree/shared/icons/chevron_right" %>
+ <% end %>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/storefront/app/views/themes/default/spree/posts/_post.html.erb b/storefront/app/views/themes/default/spree/posts/_post.html.erb
index 39f64bb243..21f28f641e 100644
--- a/storefront/app/views/themes/default/spree/posts/_post.html.erb
+++ b/storefront/app/views/themes/default/spree/posts/_post.html.erb
@@ -1,13 +1,22 @@
-<div id="<%= dom_id(post) %>">
- <%= link_to spree.post_path(post), class: 'block hover:opacity-70 transition-opacity' do %>
- <div class="h-[200px] w-full bg-gray-100">
- <% if post.image.attached? && post.image.variable? %>
- <%= spree_image_tag(post.image, width: 400, height: 200, class: 'h-full w-full object-cover object-center', loading: :lazy, alt: post.title) %>
- <% end %>
- </div>
+<div class="swiper-slide w-full">
+ <div id="<%= dom_id(post) %>">
+ <%= link_to spree.post_path(post), class: 'block hover:opacity-70 transition-opacity', data: { turbo_frame: '_top' } do %>
+ <div class="h-[200px] w-full bg-gray-100">
+ <% if post.image.attached? && post.image.variable? %>
+ <%= spree_image_tag(
+ post.image,
+ width: 400,
+ height: 200,
+ class: "h-full w-full object-cover object-center",
+ loading: :lazy,
+ alt: post.title,
+ ) %>
+ <% end %>
+ </div>
- <h1 class="uppercase text-lg font-medium mt-4"><%= post.title %></h1>
- <div class="mt-2 text-sm"><%= local_time(post.published_at, '%B %e, %Y') %></div>
- <div class="mt-4"><%= post.shortened_description %></div>
- <% end %>
+ <h1 class="uppercase text-lg font-medium mt-4"><%= post.title %></h1>
+ <div class="mt-2 text-sm"><%= local_time(post.published_at, "%B %e, %Y") %></div>
+ <div class="mt-4"><%= post.shortened_description %></div>
+ <% end %>
+ </div>
</div>
diff --git a/storefront/spec/controllers/spree/page_sections_controller_spec.rb b/storefront/spec/controllers/spree/page_sections_controller_spec.rb
index 4b61d8cae5..e709a4b5bf 100644
--- a/storefront/spec/controllers/spree/page_sections_controller_spec.rb
+++ b/storefront/spec/controllers/spree/page_sections_controller_spec.rb
@@ -21,6 +21,17 @@ RSpec.describe Spree::PageSectionsController, type: :controller do
it 'renders the show template' do
expect(response).to render_template(:show)
end
+
+ context 'when section is a featured posts section' do
+ render_views
+
+ let(:section) { create(:featured_posts_page_section, preferred_max_posts_to_show: 2) }
+ let!(:posts) { create_list(:post, 3, published_at: Time.current) }
+
+ it 'renders the show template' do
+ expect(response).to render_template(:show)
+ end
+ end
end
context 'when section does not exist' do