Skip to content

Commit

Permalink
Add an index page for stock locations
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Dec 5, 2023
1 parent 84e7bf6 commit 20c500a
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions admin/app/components/solidus_admin/shipping/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def tabs
{
Spree::ShippingMethod => solidus_admin.shipping_methods_path,
Spree::ShippingCategory => solidus_admin.shipping_categories_path,
Spree::StockLocation => solidus_admin.stock_locations_path,
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= render component('shipping').new(current_class: Spree::StockLocation) do |layout| %>
<% layout.with_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_stock_location_path,
icon: "add-line",
class: "align-self-end w-full",
) %>
<% end %>
<%= render component('ui/table').new(
id: stimulus_id,
data: {
class: Spree::StockLocation,
rows: @page.records,
url: ->(stock_location) { spree.edit_admin_stock_location_path(stock_location) },
prev: prev_page_path,
next: next_page_path,
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.stock_locations_path,
searchbar_key: :name_or_description_cont,
filters: filters,
scopes: scopes,
},
) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

class SolidusAdmin::StockLocations::Index::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers

def initialize(page:)
@page = page
end

def title
Spree::StockLocation.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
[
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.stock_locations_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end

def filters
[]
end

def scopes
[]
end

def columns
[
:name,
:code,
:admin_name,
{
header: :state,
data: -> {
color = _1.active? ? :green : :graphite_light
text = _1.active? ? t('.active') : t('.inactive')

component('ui/badge').new(name: text, color: color)
},
},
{
header: :backorderable,
data: -> {
_1.backorderable_default ? component('ui/badge').yes : component('ui/badge').no
}
},
{
header: :default,
data: -> {
_1.default ? component('ui/badge').yes : component('ui/badge').no
}
},
{
header: :stock_movements,
data: -> {
count = _1.stock_movements.count

link_to(
"#{count} #{Spree::StockMovement.model_name.human(count: count).downcase}",
spree.admin_stock_location_stock_movements_path(_1),
class: 'body-link'
)
}
}
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
active: 'Active'
inactive: 'Inactive'
add: 'Add new'
batch_actions:
delete: 'Delete'
40 changes: 40 additions & 0 deletions admin/app/controllers/solidus_admin/stock_locations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module SolidusAdmin
class StockLocationsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
stock_locations = apply_search_to(
Spree::StockLocation.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(stock_locations)

respond_to do |format|
format.html { render component('stock_locations/index').new(page: @page) }
end
end

def destroy
@stock_locations = Spree::StockLocation.where(id: params[:id])

Spree::StockLocation.transaction { @stock_locations.destroy_all }

flash[:notice] = t('.success')
redirect_back_or_to stock_locations_path, status: :see_other
end

private

def load_stock_location
@stock_location = Spree::StockLocation.find_by!(number: params[:id])
authorize! action_name, @stock_location
end

def stock_location_params
params.require(:stock_location).permit(:stock_location_id, permitted_stock_location_attributes)
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/stock_locations.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
stock_locations:
title: "Stock Locations"
destroy:
success: "Stock locations were successfully removed."
1 change: 1 addition & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
admin_resources :stock_items, only: [:index]
admin_resources :shipping_methods, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:index, :destroy]
admin_resources :stock_locations, only: [:index, :destroy]
end

0 comments on commit 20c500a

Please sign in to comment.