Class: OrdersController

Inherits:
Spree::BaseController show all
Defined in:
app/controllers/orders_controller.rb

Instance Method Summary collapse

Methods included from SpreeBase

included

Methods inherited from ActionController::Base

#respond_with

Instance Method Details

#accurate_titleObject



61
62
63
# File 'app/controllers/orders_controller.rb', line 61

def accurate_title
  @order && @order.completed? ? "#{Order.human_name} #{@order.number}" : I18n.t(:shopping_cart)
end

#editObject

Shows the current incomplete order from the session



22
23
24
# File 'app/controllers/orders_controller.rb', line 22

def edit
  @order = current_order(true)
end

#emptyObject



53
54
55
56
57
58
59
# File 'app/controllers/orders_controller.rb', line 53

def empty
  if @order = current_order
    @order.line_items.destroy_all
  end
  
  respond_with(@order) { |format| format.html { redirect_to cart_path } }
end

#populateObject

Adds a new item to the order (creating a new order if none already exists)

Parameters can be passed using the following possible parameter configurations:

  • Single variant/quantity pairing

:variants => {variant_id => quantity}

  • Multiple products at once

:products => {product_id => variant_id, product_id => variant_id}, :quantity => quantity :products => {product_id => variant_id, product_id => variant_id}}, :quantity => {variant_id => quantity, variant_id => quantity}



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/orders_controller.rb', line 36

def populate
  @order = current_order(true)

  params[:products].each do |product_id,variant_id|
    quantity = params[:quantity].to_i if !params[:quantity].is_a?(Hash)
    quantity = params[:quantity][variant_id].to_i if params[:quantity].is_a?(Hash)
    @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
  end if params[:products]

  params[:variants].each do |variant_id, quantity|
    quantity = quantity.to_i
    @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
  end if params[:variants]

  respond_with(@order) { |format| format.html { redirect_to cart_path } }
end

#showObject



7
8
9
# File 'app/controllers/orders_controller.rb', line 7

def show
  @order = Order.find_by_number(params[:id])
end

#updateObject



11
12
13
14
15
16
17
18
19
# File 'app/controllers/orders_controller.rb', line 11

def update
  @order = current_order
  if @order.update_attributes(params[:order])
    @order.line_items = @order.line_items.select {|li| li.quantity > 0 }
    respond_with(@order) { |format| format.html { redirect_to cart_path } }
  else
    respond_with(@order) 
  end
end