Skip to content

Commit

Permalink
Add scopes to the products/index page
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 30, 2023
1 parent 8e96c5a commit 3288ca9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def filters
def scopes
[
{ name: :all, label: t('.scopes.all'), default: true },
{ name: :in_stock, label: t('.scopes.in_stock') },
{ name: :out_of_stock, label: t('.scopes.out_of_stock') },
{ name: :available, label: t('.scopes.available') },
{ name: :discontinued, label: t('.scopes.discontinued') },
{ name: :deleted, label: t('.scopes.deleted') },
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ en:
with_deleted: Include deleted
scopes:
all: All
available: Available
discontinued: Discontinued
in_stock: In stock
out_of_stock: Out of stock
deleted: Deleted
4 changes: 4 additions & 0 deletions admin/app/controllers/solidus_admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class ProductsController < SolidusAdmin::BaseController

search_scope(:all, default: true)
search_scope(:deleted) { _1.with_discarded.discarded }
search_scope(:discontinued) { _1.where(discontinue_on: ...Time.current) }
search_scope(:available) { _1.available }
search_scope(:in_stock) { _1.where(id: Spree::Variant.in_stock.distinct.select(:product_id)) }
search_scope(:out_of_stock) { _1.where.not(id: Spree::Variant.in_stock.distinct.select(:product_id)) }

def index
products = apply_search_to(
Expand Down

0 comments on commit 3288ca9

Please sign in to comment.