orders_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. class OrdersController < ApplicationController
  18. skip_before_filter :authorize, :only => [:new, :create]
  19. # GET /orders
  20. # GET /orders.xml
  21. def index
  22. @orders = Order.paginate :page=>params[:page], :order=>'created_at desc',
  23. :per_page => 10
  24. respond_to do |format|
  25. format.html # index.html.erb
  26. format.xml { render :xml => @orders }
  27. end
  28. end
  29. # GET /orders/1
  30. # GET /orders/1.xml
  31. def show
  32. @order = Order.find(params[:id])
  33. respond_to do |format|
  34. format.html # show.html.erb
  35. format.xml { render :xml => @order }
  36. end
  37. end
  38. # GET /orders/new
  39. # GET /orders/new.xml
  40. def new
  41. @cart = current_cart
  42. if @cart.line_items.empty?
  43. redirect_to store_url, :notice => "Your cart is empty"
  44. return
  45. end
  46. @order = Order.new
  47. respond_to do |format|
  48. format.html # new.html.erb
  49. format.xml { render :xml => @order }
  50. end
  51. end
  52. # GET /orders/1/edit
  53. def edit
  54. @order = Order.find(params[:id])
  55. end
  56. # POST /orders
  57. # POST /orders.xml
  58. def create
  59. @order = Order.new(params[:order])
  60. @order.add_line_items_from_cart(current_cart)
  61. respond_to do |format|
  62. if @order.save
  63. Cart.destroy(session[:cart_id])
  64. session[:cart_id] = nil
  65. Notifier.order_received(@order).deliver
  66. format.html { redirect_to(store_url, :notice =>
  67. I18n.t('.thanks')) }
  68. format.xml { render :xml => @order, :status => :created,
  69. :location => @order }
  70. else
  71. format.html { render :action => "new" }
  72. format.xml { render :xml => @order.errors,
  73. :status => :unprocessable_entity }
  74. end
  75. end
  76. end
  77. # PUT /orders/1
  78. # PUT /orders/1.xml
  79. def update
  80. @order = Order.find(params[:id])
  81. respond_to do |format|
  82. if @order.update_attributes(params[:order])
  83. format.html { redirect_to(@order, :notice => 'Order was successfully updated.') }
  84. format.xml { head :ok }
  85. else
  86. format.html { render :action => "edit" }
  87. format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
  88. end
  89. end
  90. end
  91. # DELETE /orders/1
  92. # DELETE /orders/1.xml
  93. def destroy
  94. @order = Order.find(params[:id])
  95. @order.destroy
  96. respond_to do |format|
  97. format.html { redirect_to(orders_url) }
  98. format.xml { head :ok }
  99. end
  100. end
  101. end