Class: OrdersController

Inherits:
ApplicationController show all
Defined in:
lib/forge/app/controllers/orders_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#app_init

Instance Method Details

#add_to_cartObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/forge/app/controllers/orders_controller.rb', line 5

def add_to_cart
  unless @cart_order
    new_cart_order
  end
  product = Product.find(params[:product_id])
  if params[:quantity].to_i > 0 && @cart_order.add(product, params[:quantity])
    respond_to do |format|
      message = "#{product.title} was added to your cart."
      format.html { flash[:notice] = message and redirect_to :back }
      format.json { render :json => {:status => "Success", :message => message} }
    end
  else
    respond_to do |format|
      message = "There as a problem adding #{product.title} to your cart. Ensure you specified a quantity."
      format.html { flash[:warning] = message and redirect_to :back }
      format.json { render :json => {:status => "Failure", :message => message }}
    end
  end
end

#cancelObject



98
99
100
101
# File 'lib/forge/app/controllers/orders_controller.rb', line 98

def cancel
  @page_title = "Cancelled"
  new_cart_order
end

#checkout_getObject



62
63
64
65
66
67
# File 'lib/forge/app/controllers/orders_controller.rb', line 62

def checkout_get
  @billing_address = @cart_order.billing_address || Address.new
  @shipping_address = @cart_order.shipping_address || Address.new
  @use_billing_for_shipping = (@cart_order.billing_address == @cart_order.shipping_address)
  render :template => "orders/checkout"
end

#checkout_postObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/forge/app/controllers/orders_controller.rb', line 69

def checkout_post
  load_and_save_address("billing_address")
  if (params[:use_billing_for_shipping] == "1")
    @shipping_address = @billing_address
    @cart_order.shipping_address = @shipping_address
    @cart_order.save
  else
    load_and_save_address("shipping_address")
  end

  if @billing_address.valid? && @shipping_address.valid?
    redirect_to_payment
  else
    render :template => "orders/checkout"
  end

end

#get_cartObject

get_cart - for ajaxy-ness



58
59
60
# File 'lib/forge/app/controllers/orders_controller.rb', line 58

def get_cart
  render :partial => "cart", :locals => {:order => @cart_order}
end


87
88
89
90
91
92
93
94
95
96
# File 'lib/forge/app/controllers/orders_controller.rb', line 87

def paid
  @order = Order.find(params[:id])
  if params[:key] && params[:key] == @order.key
    @page_title = "Thank You"
    new_cart_order
  else
    flash[:warning] = "You are not authorized to view this page."
    redirect_to "/" and return false
  end
end

#remove_from_cartObject



25
26
27
28
29
30
31
32
33
# File 'lib/forge/app/controllers/orders_controller.rb', line 25

def remove_from_cart
  line_item = @cart_order.line_items.find(params[:line_item_id])
  line_item.destroy
  respond_to do |format|
    format.html { flash[:notice] = "#{line_item.product.title} was removed from your cart." and redirect_to :back }
    # TODO: format.js
    format.js { }
  end
end

#updateObject

updates all items in the cart - called via regular, non-AJAX actions including checkout



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

def update
  @cart_order.attributes = params[:order]
  # TODO: anything necessary for coupon application - perhaps this can go in the model
  if @cart_order.save
    flash[:notice] = "Your cart was updated successfully."
    if params[:checkout]
      redirect_to_payment
    else
      redirect_to :back
    end
  else
    flash[:warning] = "There was a problem updating your cart." and redirect_to :back
  end
end

#update_line_itemObject

updates a single line item - only called via AJAX renders a cart total



53
54
55
# File 'lib/forge/app/controllers/orders_controller.rb', line 53

def update_line_item

end