Commit 4d9686ef by Hoang Phuc Do

Merge remote-tracking branch 'vspv/dhp_order' into dhp_order

# Conflicts:
#	app/controllers/carts_controller.rb
#	app/controllers/orders_controller.rb
#	app/controllers/product_items_controller.rb
#	app/helpers/carts_helper.rb
#	app/models/order.rb
#	app/services/cart.rb
#	app/views/carts/_product_item.html.erb
#	db/schema.rb
parents 7694e136 74200a58
class CartsController < ApplicationController class CartsController < ApplicationController
include CartsHelper include CartsHelper
before_action :set_cart
before_action :set_product_item, before_action :set_product_item,
only: [:add_product_item, :update_product_item, :remove_product_item] only: [:add_product_item, :update_product_item, :remove_product_item]
before_action :build_empty_quantity_notice, if: :quantity_equal_zero?, before_action :build_empty_quantity_notice, if: :quantity_equal_zero?,
only: :add_product_item only: :add_product_item
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
# GET /cart # GET /cart
...@@ -24,7 +25,6 @@ class CartsController < ApplicationController ...@@ -24,7 +25,6 @@ class CartsController < ApplicationController
else else
render_warning_notice('Product is out of stock') render_warning_notice('Product is out of stock')
end end
end end
# PUT/PATCH /cart/update/1 # PUT/PATCH /cart/update/1
......
...@@ -2,8 +2,14 @@ class OrdersController < ApplicationController ...@@ -2,8 +2,14 @@ class OrdersController < ApplicationController
include CartsHelper include CartsHelper
before_action :authenticate_user!, only: :new before_action :authenticate_user!, only: :new
before_action :set_cart, only: [:new, :create]
before_action :set_order_items, only: :new before_action :set_order_items, only: :new
# GET /orders/1
def show
@order = Order.find(params[:id])
end
# GET /orders/new # GET /orders/new
def new def new
redirect_to root_url if @cart.blank? redirect_to root_url if @cart.blank?
......
class Order < ApplicationRecord class Order < ApplicationRecord
has_many :product_items, dependent: :destroy has_many :product_items, dependent: :destroy
belongs_to :user belongs_to :user
after_save :set_default_order_status
enum order_status: { pending: 0, done: 1 }
def total_price def total_price
product_items.to_a.sum { |item| item.product.price * item.quantity } product_items.to_a.sum { |item| item.product.price * item.quantity }
end end
def set_default_order_status
update_attribute(:order_status, 0)
end
end end
\ No newline at end of file
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<td class="product-image-td"></td> <td class="product-image-td"></td>
<td class="product-name-td"> <td class="product-name-td">
<h2 class="product-name"> <h2 class="product-name">
<%= link_to product_item[:product].title, product_url(product_item[:product]) %> <%= link_to product_item[:product].title, product_item_url(product_item[:product]) %>
</h2> </h2>
</td> </td>
<td><%= number_to_currency(product_item[:product].price) %></td> <td><%= number_to_currency(product_item[:product].price) %></td>
......
<div class="cart-table-wrap">
<table class="table table-bordered table-hover">
<thead>
<tr>
<td colspan="2" class="text-left">
Order Detail - <span><%= order.order_status %></span>
</td>
</tr>
<tbody>
<tr>
<td><strong>Order ID:</strong> #<%= order.id %></td>
<td><strong>Date created:</strong> <%= order.created_at.strftime('%d-%M-%Y %H:%m') %></td>
</tr>
<tr>
<td><strong>Email:</strong> <%= order.user.email %></td>
<td><strong>Total:</strong> <%= number_to_currency(order.total_price) %></td>
</tr>
</tbody>
</thead>
</table>
<div class="cart-table-wrap">
<table class="cart-table">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>Unit Price</th>
<th>Qty</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<% order.product_items.each do |product_item| %>
<%= render partial: 'product_item', locals: {product_item: product_item} %>
<% end %>
</tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<td><strong>Total</strong></td>
<td><strong><%= number_to_currency(order.total_price) %></strong></td>
</tr>
</tfoot>
</table>
</div>
</div>
\ No newline at end of file
class AddOrderStatusToOrders < ActiveRecord::Migration[5.1]
def change
add_column :orders, :order_status, :integer
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170615035827) do ActiveRecord::Schema.define(version: 20170619091633) do
create_table "categories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "categories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "title" t.string "title"
...@@ -21,6 +21,7 @@ ActiveRecord::Schema.define(version: 20170615035827) do ...@@ -21,6 +21,7 @@ ActiveRecord::Schema.define(version: 20170615035827) do
t.bigint "user_id" t.bigint "user_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "order_status"
t.index ["user_id"], name: "index_orders_on_user_id" t.index ["user_id"], name: "index_orders_on_user_id"
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment