Class: Spree::Api::V1::ShipmentsController
Instance Attribute Summary
#current_api_user
Instance Method Summary
collapse
#content_type, #map_nested_attributes_keys, #permitted_line_item_attributes
included
Instance Method Details
#add ⇒ Object
60
61
62
63
64
65
66
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 60
def add
quantity = params[:quantity].to_i
@shipment.order.contents.add(variant, quantity, {shipment: @shipment})
respond_with(@shipment, default_template: :show)
end
|
#create ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 22
def create
@order = Spree::Order.find_by!(number: params.fetch(:shipment).fetch(:order_id))
authorize! :read, @order
authorize! :create, Shipment
quantity = params[:quantity].to_i
@shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))
@order.contents.add(variant, quantity, {shipment: @shipment})
@shipment.save!
respond_with(@shipment.reload, default_template: :show)
end
|
#mine ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 9
def mine
if current_api_user.persisted?
@shipments = Spree::Shipment
.reverse_chronological
.joins(:order)
.where(spree_orders: {user_id: current_api_user.id})
.includes(mine_includes)
.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
else
render "spree/api/errors/unauthorized", status: :unauthorized
end
end
|
#ready ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 42
def ready
unless @shipment.ready?
if @shipment.can_ready?
@shipment.ready!
else
render 'spree/api/v1/shipments/cannot_ready_shipment', status: 422 and return
end
end
respond_with(@shipment, default_template: :show)
end
|
#remove ⇒ Object
68
69
70
71
72
73
74
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 68
def remove
quantity = params[:quantity].to_i
@shipment.order.contents.remove(variant, quantity, {shipment: @shipment})
@shipment.reload if @shipment.persisted?
respond_with(@shipment, default_template: :show)
end
|
#ship ⇒ Object
53
54
55
56
57
58
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 53
def ship
unless @shipment.shipped?
@shipment.ship!
end
respond_with(@shipment, default_template: :show)
end
|
#transfer_to_location ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 76
def transfer_to_location
@stock_location = Spree::StockLocation.find(params[:stock_location_id])
unless @quantity > 0
unprocessable_entity('ArgumentError')
return
end
@original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
render json: {success: true, message: Spree.t(:shipment_transfer_success)}, status: 201
end
|
#transfer_to_shipment ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 88
def transfer_to_shipment
@target_shipment = Spree::Shipment.friendly.find(params[:target_shipment_number])
if @quantity < 0 || @target_shipment == @original_shipment
unprocessable_entity('ArgumentError')
return
end
@original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
render json: {success: true, message: Spree.t(:shipment_transfer_success)}, status: 201
end
|
#update ⇒ Object
35
36
37
38
39
40
|
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 35
def update
@shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).friendly.find(params[:id])
@shipment.update_attributes_and_order(shipment_params)
respond_with(@shipment.reload, default_template: :show)
end
|