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