Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[admin] Add scopes to the products page #5531

Merged
merged 4 commits into from
Nov 30, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
) %>
Expand Down
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 All @@ -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

Expand Down
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'
Expand All @@ -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
Expand Up @@ -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:)
Expand Down
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:
Expand Down
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'
Expand Down
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 All @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Loading