Class: Spree::OrdersController

Inherits:
StoreController show all
Defined in:
app/controllers/spree/orders_controller.rb

Instance Method Summary collapse

Methods inherited from StoreController

#cart_link, #unauthorized

Instance Method Details

#accurate_titleObject



96
97
98
99
100
101
102
# File 'app/controllers/spree/orders_controller.rb', line 96

def accurate_title
  if @order && @order.completed?
    t('spree.order_number', number: @order.number)
  else
    t('spree.shopping_cart')
  end
end

#editObject

Shows the current incomplete order from the session



40
41
42
43
44
45
46
47
48
# File 'app/controllers/spree/orders_controller.rb', line 40

def edit
  @order = current_order(build_order_if_necessary: true)
  authorize! :edit, @order, cookies.signed[:guest_token]
  associate_user
  if params[:id] && @order.number != params[:id]
    flash[:error] = t('spree.cannot_edit_orders')
    redirect_to cart_path
  end
end

#emptyObject



87
88
89
90
91
92
93
94
# File 'app/controllers/spree/orders_controller.rb', line 87

def empty
  if @order = current_order
    authorize! :update, @order, cookies.signed[:guest_token]
    @order.empty!
  end

  redirect_to spree.cart_path
end

#populateObject

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/spree/orders_controller.rb', line 51

def populate
  @order = current_order(create_order_if_necessary: true)
  authorize! :update, @order, cookies.signed[:guest_token]

  variant  = Spree::Variant.find(params[:variant_id])
  quantity = params[:quantity].present? ? params[:quantity].to_i : 1

  # 2,147,483,647 is crazy. See issue https://github.com/spree/spree/issues/2695.
  if !quantity.between?(1, 2_147_483_647)
    @order.errors.add(:base, t('spree.please_enter_reasonable_quantity'))
  else
    begin
      @line_item = @order.contents.add(variant, quantity)
    rescue ActiveRecord::RecordInvalid => error
      @order.errors.add(:base, error.record.errors.full_messages.join(", "))
    end
  end

  respond_with(@order) do |format|
    format.html do
      if @order.errors.any?
        flash[:error] = @order.errors.full_messages.join(", ")
        redirect_back_or_default(spree.root_path)
        return
      else
        redirect_to cart_path
      end
    end
  end
end

#populate_redirectObject



82
83
84
85
# File 'app/controllers/spree/orders_controller.rb', line 82

def populate_redirect
  flash[:error] = t('spree.populate_get_error')
  redirect_to spree.cart_path
end

#showObject



15
16
17
18
# File 'app/controllers/spree/orders_controller.rb', line 15

def show
  @order = Spree::Order.find_by!(number: params[:id])
  authorize! :show, @order, cookies.signed[:guest_token]
end

#updateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/spree/orders_controller.rb', line 20

def update
  authorize! :update, @order, cookies.signed[:guest_token]
  if @order.contents.update_cart(order_params)
    @order.next if params.key?(:checkout) && @order.cart?

    respond_with(@order) do |format|
      format.html do
        if params.key?(:checkout)
          redirect_to checkout_state_path(@order.checkout_steps.first)
        else
          redirect_to cart_path
        end
      end
    end
  else
    respond_with(@order)
  end
end