Commit 61d87463 by Tran Hoang Viet

VietTH: Rename 'cart' to 'order'

parent 00eca6c2
module CartsManagement extend ActiveSupport::Concern module OrdersManagement extend ActiveSupport::Concern
included do included do
def add_to_cart product def add_to_cart product
cart_item = { order_item = {
'id' => product.id, 'id' => product.id,
'title' => product.title, 'title' => product.title,
'price' => product.price, 'price' => product.price,
'quantity' => 1 'quantity' => 1
} }
unless session['cart_info']['items'].include?(cart_item) unless session['cart_info']['items'].include?(order_item)
session['cart_info']['items'].push(cart_item) session['cart_info']['items'].push(order_item)
session['cart_info']['total'] += 1 session['cart_info']['total'] += 1
end end
end end
......
class CartsController < ApplicationController class OrdersController < ApplicationController
include CartsManagement include OrdersManagement
before_action :authenticate_user! before_action :authenticate_user!
def index def index
@cart = Cart.new @order = Order.new
end end
def create def create
if cart_service.create(cart_params) if order_service.create(order_params)
clear_cart clear_cart
redirect_to root_path redirect_to root_path
else else
...@@ -17,12 +17,12 @@ class CartsController < ApplicationController ...@@ -17,12 +17,12 @@ class CartsController < ApplicationController
end end
private private
def cart_params def order_params
params.require(:cart).permit(:total, cart_items_attributes: [:product_id, :quantity]) params.require(:order).permit(:total, order_items_attributes: [:product_id, :quantity])
end end
def cart_service def order_service
@cart_service ||= CartService.new(current_user, params) @order_service ||= OrderService.new(current_user, params)
end end
end end
\ No newline at end of file
class ProductsController < ApplicationController class ProductsController < ApplicationController
include CartsManagement include OrdersManagement
before_action :set_product, only: [:show, :add_cart] before_action :set_product, only: [:show, :add_cart]
before_action :set_categories, only: [:new] before_action :set_categories, only: [:new]
......
class CartMailer < ActionMailer::Base
default from: 'demo@example.com'
def send_checkout(email, cart)
@cart = cart
@cart_items = @cart.cart_items.includes(:product)
mail(to: email, subject: "Order number #{cart.id}")
end
end
\ No newline at end of file
class OrderMailer < ActionMailer::Base
default from: 'demo@example.com'
def send_checkout(email, order)
@order = order
@order_items = @order.order_items.includes(:product)
mail(to: email, subject: "Order number #{order.id}")
end
end
\ No newline at end of file
class Cart < ActiveRecord::Base
belongs_to :user
has_many :cart_items, dependent: :destroy
accepts_nested_attributes_for :cart_items
end
class Order < ActiveRecord::Base
belongs_to :user
has_many :order_items, dependent: :destroy
accepts_nested_attributes_for :order_items
end
class CartItem < ActiveRecord::Base class OrderItem < ActiveRecord::Base
belongs_to :cart belongs_to :order
belongs_to :product belongs_to :product
end end
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_many :carts has_many :orders
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
......
class CartService < BaseService class OrderService < BaseService
def create(cart_params) def create(order_params)
cart = current_user.carts.build(cart_params) order = current_user.orders.build(order_params)
product_ids = [] product_ids = []
quantites = {} quantites = {}
cart_params['cart_items_attributes'].each do |item| order_params['order_items_attributes'].each do |item|
id = item.last['product_id'] id = item.last['product_id']
product_ids << id product_ids << id
quantites[id] = item.last['quantity'] quantites[id] = item.last['quantity']
...@@ -15,10 +15,10 @@ class CartService < BaseService ...@@ -15,10 +15,10 @@ class CartService < BaseService
total = products.inject(0) do |sum, product| total = products.inject(0) do |sum, product|
sum + (product.price.to_i * quantites[product.id.to_s].to_i) sum + (product.price.to_i * quantites[product.id.to_s].to_i)
end end
cart.total = total order.total = total
if cart.save if order.save
send_checkout_email(cart) send_checkout_email(order)
{status: true} {status: true}
else else
{status: false} {status: false}
...@@ -26,8 +26,8 @@ class CartService < BaseService ...@@ -26,8 +26,8 @@ class CartService < BaseService
end end
private private
def send_checkout_email(cart) def send_checkout_email(order)
CartMailer.send_checkout(current_user.email, cart).deliver_later OrderMailer.send_checkout(current_user.email, order).deliver_later
end end
end end
\ No newline at end of file
#cart-info #cart-info
%h3.title Category %h3.title Category
= "Total: #{pluralize(session_cart[:total], 'item')}" = "Total: #{pluralize(session_cart[:total], 'item')}"
= link_to 'Checkout', carts_path, class: 'btn btn-default pull-right' = link_to 'Checkout', orders_path, class: 'btn btn-default pull-right'
\ No newline at end of file \ No newline at end of file
%p= "Order ##{@cart.id}" %p= "Order ##{@order.id}"
%p= "Total: #{@cart.total}" %p= "Total: #{@order.total}"
%table %table
%thead %thead
...@@ -9,13 +9,13 @@ ...@@ -9,13 +9,13 @@
%td Price %td Price
%td Quantity %td Quantity
%tbody %tbody
- @cart_items.each_with_index do |cart_item, index| - @order_items.each_with_index do |order_item, index|
%tr %tr
%td.number.text-right %td.number.text-right
= index + 1 = index + 1
%td.title %td.title
= link_to cart_item.product.title, product_url(cart_item.product) = link_to order_item.product.title, product_url(order_item.product)
%td.price %td.price
= cart_item.product.price = order_item.product.price
%td.quantity %td.quantity
= cart_item.quantity = order_item.quantity
\ No newline at end of file \ No newline at end of file
#checkout-cart #checkout-cart
= form_for @cart do |f| = form_for @order do |f|
%table.table %table.table
%thead %thead
%tr %tr
...@@ -8,17 +8,17 @@ ...@@ -8,17 +8,17 @@
%td Price %td Price
%td Quantity %td Quantity
%tbody %tbody
- session_cart[:items].each_with_index do |cart_item, index| - session_cart[:items].each_with_index do |order_item, index|
%tr %tr
%td.number.text-right %td.number.text-right
= index + 1 = index + 1
%td.title %td.title
= link_to cart_item[:title], product_path(cart_item[:id]) = link_to order_item[:title], product_path(order_item[:id])
= hidden_field_tag "cart[cart_items_attributes][#{index}][product_id]", cart_item[:id] = hidden_field_tag "order[order_items_attributes][#{index}][product_id]", order_item[:id]
%td.price %td.price
= cart_item[:price] = order_item[:price]
%td.quantity %td.quantity
= text_field_tag "cart[cart_items_attributes][#{index}][quantity]", cart_item[:quantity] = text_field_tag "order[order_items_attributes][#{index}][quantity]", order_item[:quantity]
%tfoot %tfoot
%tr %tr
%td.text-right{colspan: 4} %td.text-right{colspan: 4}
......
...@@ -14,5 +14,5 @@ Rails.application.routes.draw do ...@@ -14,5 +14,5 @@ Rails.application.routes.draw do
resources :categories resources :categories
resources :carts resources :orders
end end
\ No newline at end of file
class CreateCarts < ActiveRecord::Migration class CreateOrders < ActiveRecord::Migration
def change def change
create_table :carts do |t| create_table :orders do |t|
t.timestamps null: false t.timestamps null: false
t.references :user, index: true, null: false t.references :user, index: true, null: false
t.decimal :total, default: 0 t.decimal :total, default: 0
......
class CreateCartItems < ActiveRecord::Migration class CreateOrderItems < ActiveRecord::Migration
def change def change
create_table :cart_items do |t| create_table :order_items do |t|
t.timestamps null: false t.timestamps null: false
t.references :cart, index: true t.references :order, index: true
t.references :product, index: true t.references :product, index: true
t.integer :quantity, default: 1 t.integer :quantity, default: 1
t.decimal :price, default: 0 t.decimal :price, default: 0
......
...@@ -13,34 +13,34 @@ ...@@ -13,34 +13,34 @@
ActiveRecord::Schema.define(version: 20150706090027) do ActiveRecord::Schema.define(version: 20150706090027) do
create_table "cart_items", force: :cascade do |t| create_table "categories", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "node_id", limit: 255
t.string "title", limit: 255
t.integer "category_type", limit: 4, default: 0
end
create_table "order_items", force: :cascade do |t|
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 "cart_id", limit: 4 t.integer "order_id", limit: 4
t.integer "product_id", limit: 4 t.integer "product_id", limit: 4
t.integer "quantity", limit: 4, default: 1 t.integer "quantity", limit: 4, default: 1
t.decimal "price", precision: 10, default: 0 t.decimal "price", precision: 10, default: 0
end end
add_index "cart_items", ["cart_id"], name: "index_cart_items_on_cart_id", using: :btree add_index "order_items", ["order_id"], name: "index_order_items_on_order_id", using: :btree
add_index "cart_items", ["product_id"], name: "index_cart_items_on_product_id", using: :btree add_index "order_items", ["product_id"], name: "index_order_items_on_product_id", using: :btree
create_table "carts", force: :cascade do |t| create_table "orders", force: :cascade do |t|
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 "user_id", limit: 4, null: false t.integer "user_id", limit: 4, null: false
t.decimal "total", precision: 10, default: 0 t.decimal "total", precision: 10, default: 0
end end
add_index "carts", ["user_id"], name: "index_carts_on_user_id", using: :btree add_index "orders", ["user_id"], name: "index_orders_on_user_id", using: :btree
create_table "categories", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "node_id", limit: 255
t.string "title", limit: 255
t.integer "category_type", limit: 4, default: 0
end
create_table "products", force: :cascade do |t| create_table "products", force: :cascade do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
......
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