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

Applies a promotion code to the user’s most recent order This is a temporary API method until we move to next Spree release which has this logic already in this commit.

github.com/spree/spree/commit/72a5b74c47af975fc3492580415a4cdc2dc02c0c

Source references:

github.com/spree/spree/blob/master/frontend/app/controllers/spree/store_controller.rb#L13 github.com/spree/spree/blob/2-1-stable/frontend/app/controllers/spree/orders_controller.rb#L100



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/spree/api/orders_controller.rb', line 88

def apply_coupon_code
  find_order
  authorize! :update, @order, order_token
  @order.coupon_code = params[:coupon_code]
  @order.save

  # https://github.com/spree/spree/blob/2-1-stable/core/lib/spree/promo/coupon_applicator.rb
  result = Spree::Promo::CouponApplicator.new(@order).apply

  result[:coupon_applied?]  ||= false

  # Move flash.notice fields into success if applied
  # An error message is in result[:error]
  if result[:coupon_applied?] && result[:notice]
    result[:success] = result[:notice]
  end

  # Need to turn hash result into object for RABL
  # https://github.com/nesquena/rabl/wiki/Rendering-hash-objects-in-rabl
  @coupon_result = OpenStruct.new(result)
  render status: @coupon_result.coupon_applied? ? 200 : 422
end

#cancelObject



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

def cancel
  find_order
  authorize! :update, @order, params[:token]
  @order.cancel!
  render :show
end

#createObject



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

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

#emptyObject



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

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

#indexObject



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

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

#mineObject



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

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

#showObject



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

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



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

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