Class: Spree::Api::OrdersController

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

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Methods inherited from BaseController

#map_nested_attributes_keys, #permitted_line_item_attributes, #set_jsonp_format

Methods included from ControllerSetup

included

Instance Method Details

#apply_coupon_codeObject



79
80
81
82
83
84
85
86
# File 'app/controllers/spree/api/orders_controller.rb', line 79

def apply_coupon_code
  find_order
  authorize! :update, @order, order_token
  @order.coupon_code = params[:coupon_code]
  @handler = PromotionHandler::Coupon.new(@order).apply
  status = @handler.successful? ? 200 : 422
  render "spree/api/promotions/handler", :status => status
end

#cancelObject



17
18
19
20
21
22
# File 'app/controllers/spree/api/orders_controller.rb', line 17

def cancel
  find_order
  authorize! :update, @order, params[:token]
  @order.cancel!
  respond_with(@order, :default_template => :show)
end

#createObject



24
25
26
27
28
# File 'app/controllers/spree/api/orders_controller.rb', line 24

def create
  authorize! :create, Order
  @order = Spree::Core::Importer::Order.import(current_api_user, order_params)
  respond_with(@order, default_template: :show, status: 201)
end

#emptyObject



30
31
32
33
34
35
36
# File 'app/controllers/spree/api/orders_controller.rb', line 30

def empty
  find_order
  authorize! :update, @order, order_token
  @order.empty!
  @order.update!
  render text: nil, status: 200
end

#indexObject



38
39
40
41
42
# File 'app/controllers/spree/api/orders_controller.rb', line 38

def index
  authorize! :index, Order
  @orders = Order.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  respond_with(@orders)
end

#mineObject



71
72
73
74
75
76
77
# File 'app/controllers/spree/api/orders_controller.rb', line 71

def mine
  if current_api_user.persisted?
    @orders = current_api_user.orders.reverse_chronological.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  else
    render "spree/api/errors/unauthorized", status: :unauthorized
  end
end

#showObject



44
45
46
47
48
49
50
# File 'app/controllers/spree/api/orders_controller.rb', line 44

def show
  find_order
  authorize! :show, @order, order_token
  method = "before_#{@order.state}"
  send(method) if respond_to?(method, true)
  respond_with(@order)
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/spree/api/orders_controller.rb', line 52

def update
  find_order(true)
  authorize! :update, @order, order_token
  # Parsing line items through as an update_attributes call in the API will result in
  # many line items for the same variant_id being created. We must be smarter about this,
  # hence the use of the update_line_items method, defined within order_decorator.rb.
  order_params.delete("line_items_attributes")
  if @order.update_attributes(order_params)

    deal_with_line_items if params[:order][:line_items]

    @order.line_items.reload
    @order.update!
    respond_with(@order, default_template: :show)
  else
    invalid_resource!(@order)
  end
end