Class: Spree::Admin::ReservedStockItemsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/spree/admin/reserved_stock_items_controller.rb', line 19

def create
  validator = Spree::Api::V1::Validators::ReserveStockParamsValidator.new(permitted_resource_params)

  if validator.validate
    variant = Spree::Variant.find(permitted_resource_params[:variant_id])
    original_stock_location = Spree::StockLocation.find(permitted_resource_params[:original_stock_location_id])
    @reserved_stock_item = Spree::Stock::Reserver.new.reserve(
      variant,
      original_stock_location,
      @user,
      permitted_resource_params[:quantity].to_i,
      Time.zone.parse(permitted_resource_params[:expires_at])
    )
    flash[:success] = flash_message_for(@reserved_stock_item, :successfully_created)
    redirect_to admin_user_reserved_stock_items_path(@user)
  else
    flash[:error] = "#{Spree.t("admin.reserved_stock_item.unable_to_create")}: #{validator.errors}"
    render :new
  end
rescue => e
  flash[:error] = "#{Spree.t("admin.reserved_stock_item.unable_to_create")}: #{e.message}"
  render :new
end

#indexObject



14
15
16
17
# File 'app/controllers/spree/admin/reserved_stock_items_controller.rb', line 14

def index
  @reserved_stock_items = @user.reserved_stock_items
  @variant_display_attributes = self.class.variant_display_attributes
end

#restockObject



43
44
45
46
47
48
49
50
51
# File 'app/controllers/spree/admin/reserved_stock_items_controller.rb', line 43

def restock
  variant = Spree::ReservedStockItem.find(params[:id]).variant
  Spree::Stock::Reserver.new.restock(variant, @user)
  flash[:success] = flash_message_for(@reserved_stock_item, :successfully_restocked)
  redirect_to admin_user_reserved_stock_items_path(@user)
rescue => e
  flash[:error] = "#{Spree.t("admin.reserved_stock_item.unable_to_restock")}: #{e.message}"
  redirect_to admin_user_reserved_stock_items_path(@user)
end