Class: Spree::Admin::InventoryLocksController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/spree/admin/inventory_locks_controller.rb

Overview

Withholds units of one product from public sale.

Reached only from a private sale — there is no tab or sidebar entry. create is the POST target for the per-variant inputs on Spree::Admin::PrivateSalesController#edit, and index is the date-range screen that page links to for permanent-stock products, where one variant has an inventory item per date and a single input cannot express the range.

All the accounting lives in InventoryItems::MoveToLocked — this controller resolves the product, calls it, and turns its named failures into flash messages.

Instance Method Summary collapse

Instance Method Details

#createObject

POST /admin/products/:product_id/inventory_locks

One action for both directions: a positive quantity locks, a negative one releases. Applied across a date range for permanent-stock products, where each date is its own inventory item — "lock 5" means 5 per date, not 5 across the range.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/spree/admin/inventory_locks_controller.rb', line 35

def create
  items = target_inventory_items
  return redirect_with_error(:invalid_date_range) if items.nil?
  return redirect_with_error(:no_inventory_item) if items.empty?

  moved, failed = items.map { |item| move(item) }.partition(&:success?)
  return redirect_with_error(failed.first.error.value) if moved.empty?

  # Each date is moved independently and nothing can undo one that already went through —
  # Redis has no rollback. So a mixed outcome says how many landed: a bare error would read as
  # "nothing changed" while some dates are already locked.
  if failed.empty?
    flash[:success] = t('.moved', count: moved.size)
  else
    reason = error_sentence(failed.first.error.value)
    flash[:error] = t('.partially_moved', count: moved.size, total: items.size, reason: reason)
  end

  redirect_back_to_caller
end

#indexObject

GET /admin/products/:product_id/inventory_locks



19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/spree/admin/inventory_locks_controller.rb', line 19

def index
  # Only active items can be locked — a past date cannot be sold, so withholding from it
  # protects nothing (MoveToLocked refuses it outright).
  # Paginated like every other admin index here: a permanent-stock product has one row per
  # variant per date, so an unpaginated page is thousands of rows.
  @inventory_items = @product.inventory_items.active
                             .includes(variant: { option_values: :option_type })
                             .order(:inventory_date)
                             .page(params[:page]).per(50)
end