Class: Spree::Api::V1::StockItemsController

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

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Methods inherited from BaseController

#content_type, #permitted_line_item_attributes

Methods included from ControllerSetup

included

Instance Method Details

#createObject



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

def create
  authorize! :create, StockItem

  count_on_hand = 0
  if params[:stock_item].key?(:count_on_hand)
    count_on_hand = params[:stock_item][:count_on_hand].to_i
  end

  @stock_item = scope.new(stock_item_params)
  if @stock_item.save
    @stock_item.adjust_count_on_hand(count_on_hand)
    respond_with(@stock_item, status: 201, default_template: :show)
  else
    invalid_resource!(@stock_item)
  end
end

#destroyObject



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

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

#indexObject



7
8
9
10
# File 'app/controllers/spree/api/v1/stock_items_controller.rb', line 7

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

#showObject



12
13
14
15
# File 'app/controllers/spree/api/v1/stock_items_controller.rb', line 12

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
51
# File 'app/controllers/spree/api/v1/stock_items_controller.rb', line 34

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

  count_on_hand = 0
  if params[:stock_item].key?(:count_on_hand)
    count_on_hand = params[:stock_item][:count_on_hand].to_i
    params[:stock_item].delete(:count_on_hand)
  end

  updated = params[:stock_item][:force] ? @stock_item.set_count_on_hand(count_on_hand)
                                        : @stock_item.adjust_count_on_hand(count_on_hand)

  if updated
    respond_with(@stock_item, status: 200, default_template: :show)
  else
    invalid_resource!(@stock_item)
  end
end