-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 Stock Items
index component
#5532
Merged
rainerdema
merged 4 commits into
solidusio:main
from
nebulab:rainerd/admin/add-stock-items-index-page
Dec 5, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b362408
Add ransackable association for `Spree::StockItem` search in admin
rainerdema 33cba69
Add `stock_items/index` component with dedicated actions
rainerdema 357d17f
Add conditional focus behavior to search field after filter selection
rainerdema 349502f
Remove extra blank line in filter configuration
rainerdema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,6 @@ def filters | |
] | ||
end | ||
}, | ||
|
||
{ | ||
presentation: t('.filters.shipment_state'), | ||
combinator: 'or', | ||
|
25 changes: 25 additions & 0 deletions
25
admin/app/components/solidus_admin/stock_items/index/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<%= page do %> | ||
<%= page_header do %> | ||
<%= page_header_title title %> | ||
<% end %> | ||
|
||
<%= render component('ui/table').new( | ||
id: stimulus_id, | ||
data: { | ||
class: Spree::StockItem, | ||
rows: @page.records, | ||
prev: prev_page_path, | ||
next: next_page_path, | ||
columns: columns, | ||
batch_actions: batch_actions, | ||
}, | ||
search: { | ||
name: :q, | ||
value: params[:q], | ||
url: solidus_admin.stock_items_path, | ||
searchbar_key: :variant_product_name_or_variant_sku_or_variant_option_values_name_or_variant_option_values_presentation_cont, | ||
filters: filters, | ||
scopes: scopes, | ||
}, | ||
) %> | ||
<% end %> |
153 changes: 153 additions & 0 deletions
153
admin/app/components/solidus_admin/stock_items/index/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::StockItems::Index::Component < SolidusAdmin::BaseComponent | ||
include SolidusAdmin::Layout::PageHelpers | ||
|
||
def initialize(page:) | ||
@page = page | ||
end | ||
|
||
def title | ||
Spree::StockItem.model_name.human.pluralize | ||
end | ||
|
||
def prev_page_path | ||
solidus_admin.url_for(**request.params, page: @page.number - 1, only_path: true) unless @page.first? | ||
end | ||
|
||
def next_page_path | ||
solidus_admin.url_for(**request.params, page: @page.next_param, only_path: true) unless @page.last? | ||
end | ||
|
||
def batch_actions | ||
[] | ||
end | ||
|
||
def scopes | ||
[ | ||
{ label: t('.scopes.all_stock_items'), name: 'all', default: true }, | ||
{ label: t('.scopes.back_orderable'), name: 'back_orderable' }, | ||
{ label: t('.scopes.out_of_stock'), name: 'out_of_stock' }, | ||
{ label: t('.scopes.low_stock'), name: 'low_stock' }, | ||
{ label: t('.scopes.in_stock'), name: 'in_stock' }, | ||
] | ||
end | ||
|
||
def filters | ||
[ | ||
{ | ||
presentation: t('.filters.stock_locations'), | ||
combinator: 'or', | ||
attribute: "stock_location_id", | ||
predicate: "eq", | ||
options: Spree::StockLocation.all.map do |stock_location| | ||
[ | ||
stock_location.name.titleize, | ||
stock_location.id | ||
] | ||
end | ||
}, | ||
{ | ||
presentation: t('.filters.variants'), | ||
combinator: 'or', | ||
attribute: "variant_id", | ||
predicate: "eq", | ||
options: Spree::Variant.all.map do |variant| | ||
[ | ||
variant.descriptive_name, | ||
variant.id | ||
] | ||
end | ||
}, | ||
] | ||
end | ||
|
||
def columns | ||
[ | ||
image_column, | ||
name_column, | ||
sku_column, | ||
variant_column, | ||
stock_location_column, | ||
back_orderable_column, | ||
count_on_hand_column, | ||
] | ||
end | ||
|
||
def image_column | ||
{ | ||
col: { class: "w-[72px]" }, | ||
header: tag.span('aria-label': t('.image'), role: 'text'), | ||
data: ->(stock_item) do | ||
image = stock_item.variant.gallery.images.first or return | ||
|
||
render( | ||
component('ui/thumbnail').new( | ||
src: image.url(:small), | ||
alt: stock_item.variant.name | ||
) | ||
) | ||
end | ||
} | ||
end | ||
|
||
def name_column | ||
{ | ||
header: :name, | ||
data: ->(stock_item) do | ||
content_tag :div, stock_item.variant.name | ||
end | ||
} | ||
end | ||
|
||
def sku_column | ||
{ | ||
header: :sku, | ||
data: ->(stock_item) do | ||
content_tag :div, stock_item.variant.sku | ||
end | ||
} | ||
end | ||
|
||
def variant_column | ||
{ | ||
header: :variant, | ||
data: ->(stock_item) do | ||
content_tag(:div, class: "space-y-0.5") do | ||
safe_join( | ||
stock_item.variant.option_values.sort_by(&:option_type_name).map do |option_value| | ||
render(component('ui/badge').new(name: "#{option_value.option_type_presentation}: #{option_value.presentation}")) | ||
end | ||
) | ||
end | ||
end | ||
} | ||
end | ||
|
||
def stock_location_column | ||
{ | ||
header: :stock_location, | ||
data: ->(stock_item) do | ||
link_to stock_item.stock_location.name, spree.admin_stock_location_stock_movements_path(stock_item.stock_location.id, q: { variant_sku_eq: stock_item.variant.sku }) | ||
end | ||
} | ||
end | ||
|
||
def back_orderable_column | ||
{ | ||
header: :back_orderable, | ||
data: ->(stock_item) do | ||
stock_item.backorderable? ? component('ui/badge').yes : component('ui/badge').no | ||
end | ||
} | ||
end | ||
|
||
def count_on_hand_column | ||
{ | ||
header: :count_on_hand, | ||
data: ->(stock_item) do | ||
content_tag :div, stock_item.count_on_hand | ||
end | ||
} | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
admin/app/components/solidus_admin/stock_items/index/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
en: | ||
filters: | ||
stock_locations: Stock Locations | ||
variants: Variants | ||
scopes: | ||
all_stock_items: All | ||
back_orderable: Back Orderable | ||
out_of_stock: Out Of Stock | ||
low_stock: Low Stock | ||
in_stock: In Stock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
admin/app/controllers/solidus_admin/stock_items_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
class StockItemsController < SolidusAdmin::BaseController | ||
include SolidusAdmin::ControllerHelpers::Search | ||
|
||
search_scope(:all, default: true) { _1 } | ||
search_scope(:back_orderable) { _1.where(backorderable: true) } | ||
search_scope(:out_of_stock) { _1.where('count_on_hand <= 0') } | ||
search_scope(:low_stock) { _1.where('count_on_hand > 0 AND count_on_hand < ?', SolidusAdmin::Config[:low_stock_value]) } | ||
search_scope(:in_stock) { _1.where('count_on_hand > 0') } | ||
|
||
def index | ||
stock_items = apply_search_to( | ||
Spree::StockItem.order(created_at: :desc, id: :desc), | ||
param: :q, | ||
) | ||
|
||
set_page_and_extract_portion_from(stock_items) | ||
|
||
respond_to do |format| | ||
format.html { render component('stock_items/index').new(page: @page) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
en: | ||
solidus_admin: | ||
stock_items: | ||
title: "Stock Items" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe "Stock Items", :js, type: :feature do | ||
before { sign_in create(:admin_user, email: '[email protected]') } | ||
|
||
it "lists stock items and allows navigating through scopes" do | ||
non_backorderable = create(:stock_item, backorderable: false) | ||
backorderable = create(:stock_item, backorderable: true) | ||
out_of_stock = begin | ||
item = create(:stock_item, backorderable: false) | ||
item.reduce_count_on_hand_to_zero | ||
item | ||
end | ||
low_stock = begin | ||
item = create(:stock_item, backorderable: false) | ||
item.set_count_on_hand(SolidusAdmin::Config[:low_stock_value] - 1) | ||
item | ||
end | ||
|
||
visit "/admin/stock_items" | ||
|
||
# `All` default scope | ||
expect(page).to have_content(non_backorderable.variant.sku) | ||
expect(page).to have_content(backorderable.variant.sku) | ||
expect(page).to have_content(out_of_stock.variant.sku) | ||
expect(page).to have_content(low_stock.variant.sku) | ||
|
||
click_on 'Back Orderable' | ||
expect(page).to_not have_content(non_backorderable.variant.sku) | ||
expect(page).to have_content(backorderable.variant.sku) | ||
expect(page).to_not have_content(out_of_stock.variant.sku) | ||
expect(page).to_not have_content(low_stock.variant.sku) | ||
|
||
click_on 'Out Of Stock' | ||
expect(page).to_not have_content(non_backorderable.variant.sku) | ||
expect(page).to_not have_content(backorderable.variant.sku) | ||
expect(page).to have_content(out_of_stock.variant.sku) | ||
expect(page).to_not have_content(low_stock.variant.sku) | ||
|
||
click_on 'Low Stock' | ||
expect(page).to_not have_content(non_backorderable.variant.sku) | ||
expect(page).to_not have_content(backorderable.variant.sku) | ||
expect(page).to_not have_content(out_of_stock.variant.sku) | ||
expect(page).to have_content(low_stock.variant.sku) | ||
|
||
click_on 'In Stock' | ||
expect(page).to have_content(non_backorderable.variant.sku) | ||
expect(page).to have_content(backorderable.variant.sku) | ||
expect(page).to_not have_content(out_of_stock.variant.sku) | ||
expect(page).to have_content(low_stock.variant.sku) | ||
|
||
expect(page).to be_axe_clean | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍👍👍