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 stock_items/edit modal component #5543

Merged
merged 9 commits into from
Dec 12, 2023
Prev Previous commit
Next Next commit
Introude new edit and update action for stock item records
Co-Authored-By: Elia Schito <[email protected]>
  • Loading branch information
rainerdema and elia committed Dec 11, 2023
commit 3f588efadbbf9ee658834a7e1f7dbdd566e7bd32
44 changes: 38 additions & 6 deletions admin/app/controllers/solidus_admin/stock_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module SolidusAdmin
class StockItemsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search
before_action :load_stock_items, only: [:index, :edit, :update]
before_action :load_stock_item, only: [:edit, :update]

search_scope(:all, default: true) { _1 }
search_scope(:back_orderable) { _1.where(backorderable: true) }
Expand All @@ -11,16 +13,46 @@ class StockItemsController < SolidusAdmin::BaseController
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),
respond_to do |format|
format.html { render component('stock_items/index').new(page: @page) }
end
end

def edit
end

def update
quantity_adjustment = params[:quantity_adjustment].to_i
@stock_item.assign_attributes(stock_item_params.except(:page, :q))
@stock_item.stock_movements.build(quantity: quantity_adjustment, originator: current_solidus_admin_user)

if @stock_item.save
redirect_to solidus_admin.stock_items_path, status: :see_other
else
end
end

private

def load_stock_items
@stock_items = apply_search_to(
Spree::StockItem.reorder(nil),
param: :q,
)

set_page_and_extract_portion_from(stock_items)
set_page_and_extract_portion_from(@stock_items, ordered_by: {
variant_id: :desc,
stock_location_id: :desc,
id: :desc,
})
end

respond_to do |format|
format.html { render component('stock_items/index').new(page: @page) }
end
def load_stock_item
@stock_item = Spree::StockItem.find(params[:id])
end

def stock_item_params
params.require(:stock_item).permit(:backorderable, :page, :q)
end
end
end
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
admin_resources :tax_categories, only: [:index, :destroy]
admin_resources :tax_rates, only: [:index, :destroy]
admin_resources :payment_methods, only: [:index, :destroy], sortable: true
admin_resources :stock_items, only: [:index]
admin_resources :stock_items, only: [:index, :edit, :update]
admin_resources :shipping_methods, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:index, :destroy]
admin_resources :stock_locations, only: [:index, :destroy]
Expand Down