Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/home_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def shop_products(limit = shop_products_limit)
Product.left_joins(:order_products)
.select("products.*, COALESCE(SUM(order_products.quantity), 0) AS total_sold")
.group("products.id")
.order(Arel.sql("total_sold DESC"))
.order(Arel.sql("COALESCE(SUM(order_products.quantity), 0) DESC"))
.limit(safe_limit)
else
Product.order(created_at: :asc).limit(safe_limit)
Expand Down
2 changes: 1 addition & 1 deletion app/views/home_pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<% if home_page.shop_bloc_type? %>
<div class="container">
<h2 class="text-center mb-4"><%= home_page.title %></h2>
<% products = home_page.shop_products %>
<% products = home_page.shop_products.to_a %>
<% if products.any? %>
<% product_count = products.size %>
<% grid_classes =
Expand Down
18 changes: 18 additions & 0 deletions spec/models/home_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
let!(:product_1) { Product.create!(title: "Produit 1") }
let!(:product_2) { Product.create!(title: "Produit 2") }
let!(:product_3) { Product.create!(title: "Produit 3") }
let!(:user) do
User.create!(
email: "buyer@example.com",
password: "password123",
password_confirmation: "password123",
cgu_accepted: true
)
end

before do
product_1.update_columns(created_at: Time.zone.local(2026, 1, 1, 9, 0, 0), updated_at: Time.zone.local(2026, 1, 1, 9, 0, 0))
Expand All @@ -38,5 +46,15 @@

expect(home_page.shop_products(0).size).to eq(1)
end

it "orders top sellers by sold quantity" do
order = Order.create!(user: user, status: "pending")
OrderProduct.create!(order: order, product: product_2, quantity: 1)
OrderProduct.create!(order: order, product: product_3, quantity: 4)

home_page = described_class.create!(bloc_type: :shop, shop_scope: :top_sellers, shop_products_limit: 2)

expect(home_page.shop_products.pluck(:id)).to eq([product_3.id, product_2.id])
end
end
end
Loading