Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Migrate stock items to the new table system

Damian Legawiecdbb2acce3011
diff --git a/admin/app/controllers/spree/admin/stock_items_controller.rb b/admin/app/controllers/spree/admin/stock_items_controller.rb
index 53ed102186..c04bc85a58 100644
--- a/admin/app/controllers/spree/admin/stock_items_controller.rb
+++ b/admin/app/controllers/spree/admin/stock_items_controller.rb
@@ -2,6 +2,7 @@ module Spree
module Admin
class StockItemsController < ResourceController
include ProductsBreadcrumbConcern
+ include TableConcern
before_action :add_breadcrumbs
diff --git a/admin/app/controllers/spree/admin/stock_locations_controller.rb b/admin/app/controllers/spree/admin/stock_locations_controller.rb
index 21df98d734..b8021089fc 100644
--- a/admin/app/controllers/spree/admin/stock_locations_controller.rb
+++ b/admin/app/controllers/spree/admin/stock_locations_controller.rb
@@ -13,6 +13,12 @@ module Spree
redirect_to spree.admin_stock_locations_path
end
+ # GET /admin/stock_locations/select_options
+ def select_options
+ @stock_locations = Spree::StockLocation.active.ransack(name_cont: params[:q]).result.limit(50).order(:name)
+ render json: @stock_locations.map { |sl| { id: sl.id, name: sl.display_name } }
+ end
+
private
def scope
diff --git a/admin/app/views/spree/admin/stock_items/index.html.erb b/admin/app/views/spree/admin/stock_items/index.html.erb
index 05432ca096..7a85fe9664 100644
--- a/admin/app/views/spree/admin/stock_items/index.html.erb
+++ b/admin/app/views/spree/admin/stock_items/index.html.erb
@@ -12,4 +12,4 @@
<%= render_admin_partials(:stock_items_header_partials) %>
-<%= render 'spree/admin/shared/index_table', new_object_url: nil %>
\ No newline at end of file
+<%= render_table @collection, :stock_items, edit_object_url: nil %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/tables/_table.html.erb b/admin/app/views/spree/admin/tables/_table.html.erb
index 4cfc647036..3f3a2615f4 100644
--- a/admin/app/views/spree/admin/tables/_table.html.erb
+++ b/admin/app/views/spree/admin/tables/_table.html.erb
@@ -1,4 +1,4 @@
-<%# locals: (collection:, table:, table_key:, selected_columns: nil, frame_name: nil, bulk_operations: false, sortable: false, export_type: nil) %>
+<%# locals: (collection:, table:, table_key:, selected_columns: nil, frame_name: nil, bulk_operations: false, sortable: false, export_type: nil, edit_object_url: :default) %>
<% frame_name ||= controller_name %>
<% columns = table.visible_columns(selected_columns, self) %>
<% current_sort = params.dig(:q, :s) %>
@@ -43,7 +43,8 @@
columns: columns,
table: table,
bulk_operations: bulk_operations,
- sortable: sortable %>
+ sortable: sortable,
+ link_to_edit: edit_object_url != nil %>
<%= render 'spree/admin/shared/index_table_options', collection: collection %>
<% else %>
diff --git a/admin/app/views/spree/admin/tables/_table_body.html.erb b/admin/app/views/spree/admin/tables/_table_body.html.erb
index 2240761117..143af2f55f 100644
--- a/admin/app/views/spree/admin/tables/_table_body.html.erb
+++ b/admin/app/views/spree/admin/tables/_table_body.html.erb
@@ -1,4 +1,4 @@
-<%# locals: (collection:, columns:, table:, bulk_operations: false, sortable: false) %>
+<%# locals: (collection:, columns:, table:, bulk_operations: false, sortable: false, link_to_edit: true) %>
<% has_bulk_actions = bulk_operations && table.bulk_operations_enabled? %>
<% has_row_actions = table.row_actions? %>
<% if has_bulk_actions %>
@@ -12,21 +12,21 @@
<thead>
<tr>
<% if sortable %>
- <th scope="col" class="w-5"></th>
+ <th scope="col" class="w-5 rounded-none"></th>
<% end %>
- <th scope="col" class="pr-0 w-5">
+ <th scope="col" class="pr-0 w-5 rounded-none">
<%= bulk_operations_select_all_checkbox %>
</th>
<% columns.each do |column| %>
- <th scope="col" class="<%= column_header_class(column) %>">
+ <th class="rounded-none" scope="col" class="<%= column_header_class(column) %>">
<%= column.resolve_label %>
</th>
<% end %>
<% if has_row_actions %>
- <th scope="col" class="w-10"></th>
+ <th scope="col" class="w-10 rounded-none"></th>
<% end %>
</tr>
</thead>
@@ -46,7 +46,8 @@
table: table,
bulk_operations: has_bulk_actions,
sortable: sortable,
- row_actions: has_row_actions
+ row_actions: has_row_actions,
+ link_to_edit: link_to_edit
} %>
</tbody>
</table>
@@ -59,17 +60,17 @@
<thead>
<tr>
<% if sortable %>
- <th scope="col" class="w-5"></th>
+ <th scope="col" class="w-5 rounded-none"></th>
<% end %>
<% columns.each do |column| %>
- <th scope="col" class="<%= column_header_class(column) %>">
+ <th class="rounded-none" scope="col" class="<%= column_header_class(column) %>">
<%= column.resolve_label %>
</th>
<% end %>
<% if has_row_actions %>
- <th scope="col" class="w-10"></th>
+ <th scope="col" class="w-10 rounded-none"></th>
<% end %>
</tr>
</thead>
@@ -89,7 +90,8 @@
table: table,
bulk_operations: false,
sortable: sortable,
- row_actions: has_row_actions
+ row_actions: has_row_actions,
+ link_to_edit: link_to_edit
} %>
</tbody>
</table>
diff --git a/admin/app/views/spree/admin/tables/_table_row.html.erb b/admin/app/views/spree/admin/tables/_table_row.html.erb
index 8247ad5d3c..88909e4f9b 100644
--- a/admin/app/views/spree/admin/tables/_table_row.html.erb
+++ b/admin/app/views/spree/admin/tables/_table_row.html.erb
@@ -1,4 +1,4 @@
-<%# locals: (record:, columns:, table:, bulk_operations: false, sortable: false, row_actions: true) %>
+<%# locals: (record:, columns:, table:, bulk_operations: false, sortable: false, row_actions: true, link_to_edit: true) %>
<tr id="<%= dom_id record %>" class="group"<% if sortable %> data-sortable-update-url="<%= edit_object_url(record) %>"<% end %>>
<% if sortable %>
<td class="w-5 move-handle text-center">
@@ -14,7 +14,7 @@
<% columns.each_with_index do |column, index| %>
<td class="<%= column_cell_class(column) %>" id="<%= dom_id record, column.key %>">
- <% if index == 0 %>
+ <% if index == 0 && link_to_edit %>
<%= link_to edit_object_url(record),
class: 'flex items-center w-full no-underline hover:text-primary',
data: { 'turbo-frame': '_top' } do %>
diff --git a/admin/app/views/spree/admin/tables/columns/_stock_item_backorderable.html.erb b/admin/app/views/spree/admin/tables/columns/_stock_item_backorderable.html.erb
new file mode 100644
index 0000000000..3ebdd8d237
--- /dev/null
+++ b/admin/app/views/spree/admin/tables/columns/_stock_item_backorderable.html.erb
@@ -0,0 +1,14 @@
+<%# locals: (record:, column:, value:) %>
+<% if can?(:update, record) %>
+ <%= form_for record,
+ url: spree.admin_stock_item_path(record),
+ method: :put,
+ html: { data: { controller: 'auto-submit' }, class: 'flex items-center justify-center' } do |f| %>
+ <div class="custom-control form-checkbox">
+ <%= f.check_box :backorderable, class: 'custom-control-input', id: "stock_item_backorderable_#{record.id}", data: { action: 'auto-submit#submit' } %>
+ <label class="custom-control-label" for="stock_item_backorderable_<%= record.id %>"><span></span></label>
+ </div>
+ <% end %>
+<% else %>
+ <%= active_badge(record.backorderable?) %>
+<% end %>
diff --git a/admin/app/views/spree/admin/tables/columns/_stock_item_count_on_hand.html.erb b/admin/app/views/spree/admin/tables/columns/_stock_item_count_on_hand.html.erb
new file mode 100644
index 0000000000..1ad647b094
--- /dev/null
+++ b/admin/app/views/spree/admin/tables/columns/_stock_item_count_on_hand.html.erb
@@ -0,0 +1,14 @@
+<%# locals: (record:, column:, value:) %>
+<% if can?(:update, record) %>
+ <%= turbo_frame_tag record do %>
+ <%= form_for record,
+ url: spree.admin_stock_item_path(record),
+ method: :put,
+ html: { class: 'flex' } do |f| %>
+ <%= f.number_field :count_on_hand, class: 'form-input mr-2', required: true, min: 0 %>
+ <%= turbo_save_button_tag Spree.t('actions.update'), class: 'btn btn-primary' %>
+ <% end %>
+ <% end %>
+<% else %>
+ <%= record.count_on_hand %>
+<% end %>
diff --git a/admin/app/views/spree/admin/tables/columns/_stock_item_location.html.erb b/admin/app/views/spree/admin/tables/columns/_stock_item_location.html.erb
new file mode 100644
index 0000000000..69b0e7c5a9
--- /dev/null
+++ b/admin/app/views/spree/admin/tables/columns/_stock_item_location.html.erb
@@ -0,0 +1,6 @@
+<%# locals: (record:, column:, value:) %>
+<% if can?(:manage, record.stock_location) %>
+ <%= link_to record.stock_location.display_name, spree.edit_admin_stock_location_path(record.stock_location), data: { turbo_frame: '_top' } %>
+<% else %>
+ <%= record.stock_location.display_name %>
+<% end %>
diff --git a/admin/config/initializers/spree_admin_tables.rb b/admin/config/initializers/spree_admin_tables.rb
index bba80d1935..18256f3de2 100644
--- a/admin/config/initializers/spree_admin_tables.rb
+++ b/admin/config/initializers/spree_admin_tables.rb
@@ -882,4 +882,64 @@ Rails.application.config.after_initialize do
operators: %i[eq],
search_url: '/admin/users/select_options.json',
method: ->(gc) { gc.user&.email }
+
+ # Register Stock Items table
+ Spree.admin.tables.register(:stock_items, model_class: Spree::StockItem, search_param: :variant_product_name_cont, row_actions: false, new_resource: false)
+
+ # Variant with image (custom partial)
+ Spree.admin.tables.stock_items.add :variant,
+ label: :variant,
+ type: :custom,
+ sortable: false,
+ filterable: true,
+ default: true,
+ position: 10,
+ ransack_attribute: 'variant_product_name',
+ partial: 'spree/admin/variants/variant',
+ partial_locals: ->(record) { { variant: record.variant } }
+
+ # Stock location
+ Spree.admin.tables.stock_items.add :stock_location,
+ label: :stock_location,
+ type: :custom,
+ filter_type: :autocomplete,
+ sortable: false,
+ filterable: true,
+ default: true,
+ position: 20,
+ ransack_attribute: 'stock_location_id',
+ operators: %i[eq],
+ search_url: '/admin/stock_locations/select_options.json',
+ partial: 'spree/admin/tables/columns/stock_item_location'
+
+ # Backorderable (inline editable checkbox)
+ Spree.admin.tables.stock_items.add :backorderable,
+ label: :backorderable,
+ type: :custom,
+ sortable: false,
+ filterable: false,
+ default: true,
+ position: 30,
+ partial: 'spree/admin/tables/columns/stock_item_backorderable'
+
+ # Count on hand (inline editable number field)
+ Spree.admin.tables.stock_items.add :count_on_hand,
+ label: :count_on_hand,
+ type: :custom,
+ sortable: true,
+ filterable: true,
+ default: true,
+ position: 40,
+ partial: 'spree/admin/tables/columns/stock_item_count_on_hand'
+
+ # SKU (filter-only)
+ Spree.admin.tables.stock_items.add :sku,
+ label: :sku,
+ type: :string,
+ sortable: false,
+ filterable: true,
+ displayable: false,
+ default: false,
+ position: 50,
+ ransack_attribute: 'variant_sku'
end
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index b441971486..34cb8cc675 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -47,6 +47,7 @@ Spree::Core::Engine.add_routes do
get '/tags/select_options' => 'tags#select_options', as: :tags_select_options, defaults: { format: :json }
get '/countries/select_options' => 'countries#select_options', as: :countries_select_options, defaults: { format: :json }
get '/users/select_options' => 'users#select_options', as: :users_select_options, defaults: { format: :json }
+ get '/stock_locations/select_options' => 'stock_locations#select_options', as: :stock_locations_select_options, defaults: { format: :json }
# media library
resources :assets, only: [:create, :edit, :update, :destroy] do