Skip to content

Commit

Permalink
Merge pull request #5531 from solidusio/elia/admin/product-scopes
Browse files Browse the repository at this point in the history
[admin] Add scopes to the products page
elia authored Nov 30, 2023
2 parents a90a498 + f8b6f4f commit 5fef8c0
Showing 10 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<%= page_header_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add_product'),
text: t('.add'),
href: spree.new_admin_product_path,
icon: "add-line",
) %>
Original file line number Diff line number Diff line change
@@ -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
@@ -79,7 +83,7 @@ def columns
def image_column
{
col: { class: "w-[72px]" },
header: tag.span('aria-label': t('.product_image'), role: 'text'),
header: tag.span('aria-label': t('.image'), role: 'text'),
data: ->(product) do
image = product.gallery.images.first or return

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
product_image: 'Image'
add_product: 'Add Product'
image: 'Image'
add: 'Add new'
batch_actions:
delete: 'Delete'
discontinue: 'Discontinue'
@@ -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
14 changes: 12 additions & 2 deletions admin/app/components/solidus_admin/products/status/component.rb
Original file line number Diff line number Diff line change
@@ -3,11 +3,21 @@
class SolidusAdmin::Products::Status::Component < SolidusAdmin::BaseComponent
STATUSES = {
available: :green,
discontinued: :red
discontinued: :yellow,
deleted: :red,
}.freeze

def self.from_product(product)
new(status: product.available? ? :available : :discontinued)
status =
if product.deleted?
:deleted
elsif product.discontinued?
:discontinued
else
:available
end

new(status: status)
end

def initialize(status:)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
en:
available: 'Available'
discontinued: 'Discontinued'
deleted: 'Deleted'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
promotion_image: 'Image'
add: 'Add Promotion'
add: 'Add new'
batch_actions:
delete: 'Delete'
scopes:
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
promotion_image: 'Image'
add: 'Add Promotion'
add: 'Add new'
batch_actions:
delete: 'Delete'
discontinue: 'Discontinue'
6 changes: 5 additions & 1 deletion admin/app/controllers/solidus_admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -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(
@@ -28,7 +32,7 @@ def edit
end

def show
@product = Spree::Product.friendly.find(params[:id])
@product = Spree::Product.with_discarded.friendly.find(params[:id])

respond_to do |format|
format.html { render component('products/show').new(product: @product) }
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@

render_inline described_class.from_product(product)

expect(rendered_content).to have_text("Discontinued")
within('tbody') { expect(rendered_content).to have_text("Discontinued") }
end
end
end
11 changes: 7 additions & 4 deletions admin/spec/features/products_spec.rb
Original file line number Diff line number Diff line change
@@ -59,10 +59,13 @@
click_button "Activate"

expect(page).to have_content("Products were successfully activated.", wait: 5)
expect(page).to have_content("Just a product")
expect(page).to have_content("Another product")
expect(page).not_to have_content("Discontinued")
expect(page).to have_content("Available").twice
within('tbody') do
expect(page).to have_content("Just a product")
expect(page).to have_content("Another product")
expect(page).not_to have_content("Discontinued")
expect(page).to have_content("Available").twice
end

expect(page).to be_axe_clean
end
end

0 comments on commit 5fef8c0

Please sign in to comment.