Class: Spree::Api::StockItemsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/api/stock_items_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/spree/api/stock_items_controller.rb', line 20

def create
  authorize! :create, StockItem
  @stock_item = scope.new(stock_item_params)

  Spree::StockItem.transaction do
    if @stock_item.save
      adjust_stock_item_count_on_hand(count_on_hand_adjustment)
      respond_with(@stock_item, status: 201, default_template: :show)
    else
      invalid_resource!(@stock_item)
    end
  end
end

#destroyObject



52
53
54
55
56
# File 'app/controllers/spree/api/stock_items_controller.rb', line 52

def destroy
  @stock_item = Spree::StockItem.accessible_by(current_ability, :destroy).find(params[:id])
  @stock_item.discard
  respond_with(@stock_item, status: 204)
end

#indexObject



10
11
12
13
# File 'app/controllers/spree/api/stock_items_controller.rb', line 10

def index
  @stock_items = paginate(scope.ransack(params[:q]).result)
  respond_with(@stock_items)
end

#showObject



15
16
17
18
# File 'app/controllers/spree/api/stock_items_controller.rb', line 15

def show
  @stock_item = scope.find(params[:id])
  respond_with(@stock_item)
end

#updateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/spree/api/stock_items_controller.rb', line 34

def update
  @stock_item = Spree::StockItem.accessible_by(current_ability, :update).find(params[:id])
  @stock_location = @stock_item.stock_location

  adjustment = count_on_hand_adjustment
  params[:stock_item].delete(:count_on_hand)
  adjustment -= @stock_item.count_on_hand if params[:stock_item][:force]

  Spree::StockItem.transaction do
    if @stock_item.update(stock_item_params)
      adjust_stock_item_count_on_hand(adjustment)
      respond_with(@stock_item, status: 200, default_template: :show)
    else
      invalid_resource!(@stock_item)
    end
  end
end