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

Methods inherited from BaseController

#map_nested_attributes_keys, #set_jsonp_format

Methods included from ControllerSetup

included

Instance Method Details

#createObject



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

def create
  authorize! :create, StockItem

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

  @stock_item = scope.new(params[:stock_item])
  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



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

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

#indexObject



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

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

#showObject



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

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

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

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

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