Class: Spree::Api::StockItemsController
Instance Attribute Summary
#current_api_user
Instance Method Summary
collapse
#map_nested_attributes_keys, #set_jsonp_format
included
Instance Method Details
#create ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'app/controllers/spree/api/stock_items_controller.rb', line 18
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
|
#destroy ⇒ Object
56
57
58
59
60
61
|
# File 'app/controllers/spree/api/stock_items_controller.rb', line 56
def destroy
authorize! :delete, StockItem
@stock_item = StockItem.find(params[:id])
@stock_item.destroy
respond_with(@stock_item, status: 204)
end
|
#index ⇒ Object
6
7
8
9
10
|
# File 'app/controllers/spree/api/stock_items_controller.rb', line 6
def index
authorize! :read, StockItem
@stock_items = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
respond_with(@stock_items)
end
|
#show ⇒ Object
12
13
14
15
16
|
# File 'app/controllers/spree/api/stock_items_controller.rb', line 12
def show
authorize! :read, StockItem
@stock_item = scope.find(params[:id])
respond_with(@stock_item)
end
|
#update ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'app/controllers/spree/api/stock_items_controller.rb', line 36
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
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
|