Commit b0ceaebb by Tan Phat Nguyen

Add function User Management, cart, orders with specific user

parent 754cff60
...@@ -5,16 +5,20 @@ class ApplicationController < ActionController::Base ...@@ -5,16 +5,20 @@ class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
def current_cart include ApplicationHelper
  • @phatnt You should move current_cart back to application controller. And use helper_method to expose it to view like:

    helper_method :current_cart

Please register or sign in to reply
  • current_cart is removed in application controller, move it to application_helper

Please register or sign in to reply
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
private private
def after_sign_in_path_for(resource_or_scope)
if current_user
current_user.add_line_items_from_cart(current_cart)
current_user.save
current_cart.destroy unless current_user.cart == current_cart
end
session[:cart_id] = current_user.cart.id
super
end
def record_not_found def record_not_found
flash[:danger] = 'Record Not found.' flash[:danger] = 'Record Not found.'
redirect_to root_path redirect_to root_path
......
class CartsController < ApplicationController class CartsController < ApplicationController
def show def show
flash.now[:warning] = 'Cart is empty.' unless current_cart.line_items.any?
end end
def destroy def destroy
......
class OrdersController < ApplicationController class OrdersController < ApplicationController
before_action :authenticate_user!
before_action :set_order, only: [:show, :edit, :update, :destroy] before_action :set_order, only: [:show, :edit, :update, :destroy]
def index def index
@orders = Order.page(params[:page]) @orders = current_user.orders.page(params[:page])
flash.now[:warning] = 'You have not order item yet.' unless @orders.any?
end end
def show def show
...@@ -20,7 +22,6 @@ class OrdersController < ApplicationController ...@@ -20,7 +22,6 @@ class OrdersController < ApplicationController
def create def create
@order = Order.new(order_params) @order = Order.new(order_params)
@order.add_line_items_from_cart(current_cart) @order.add_line_items_from_cart(current_cart)
if @order.save if @order.save
current_cart.destroy current_cart.destroy
session[:cart_id] = nil session[:cart_id] = nil
......
...@@ -11,7 +11,12 @@ module ApplicationHelper ...@@ -11,7 +11,12 @@ module ApplicationHelper
def current_cart def current_cart
Cart.find(session[:cart_id]) Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
if current_user
current_user.cart = Cart.create
cart = current_user.cart
else
cart = Cart.create cart = Cart.create
end
session[:cart_id] = cart.id session[:cart_id] = cart.id
cart cart
end end
......
class Cart < ActiveRecord::Base class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy has_many :line_items, :dependent => :destroy
belongs_to :user
def add_product(product_id) def add_product(product_id)
current_item = line_items.where(product_id: product_id).first current_item = line_items.where(product_id: product_id).first
...@@ -14,6 +15,16 @@ class Cart < ActiveRecord::Base ...@@ -14,6 +15,16 @@ class Cart < ActiveRecord::Base
current_item current_item
end end
def combine_item_in_user_cart(product_id, quantity)
current_item = line_items.where(product_id: product_id).first
if current_item
current_item.quantity += quantity
else
current_item = LineItem.new(quantity: quantity, product_id: product_id)
end
current_item
end
def total_price def total_price
line_items.to_a.sum { |item| item.total_price } line_items.to_a.sum { |item| item.total_price }
end end
......
...@@ -4,6 +4,7 @@ class Order < ActiveRecord::Base ...@@ -4,6 +4,7 @@ class Order < ActiveRecord::Base
PAYMENT_TYPES = [ 'Check', 'Credit card', 'Purchase order' ] PAYMENT_TYPES = [ 'Check', 'Credit card', 'Purchase order' ]
has_many :line_items, dependent: :destroy has_many :line_items, dependent: :destroy
belongs_to :user
validates :name, :address, :email, :pay_type, presence: true validates :name, :address, :email, :pay_type, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES validates :pay_type, inclusion: PAYMENT_TYPES
...@@ -15,6 +16,7 @@ class Order < ActiveRecord::Base ...@@ -15,6 +16,7 @@ class Order < ActiveRecord::Base
item.cart_id = nil item.cart_id = nil
line_items << item line_items << item
end end
self.user_id = cart.user_id
end end
def total_price def total_price
......
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_one :cart, dependent: :destroy
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
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
def add_line_items_from_cart(old_cart)
if cart
old_cart.line_items.each do |item|
current_item = cart.combine_item_in_user_cart(item.product_id, item.quantity)
cart.line_items << current_item
end
else
self.cart = old_cart
end
end
end end
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><%= link_to "Order History", '#' %></li> <li><%= link_to "Order History", '#' %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li> <li><%= link_to "Settings", edit_user_registration_path %></li>
<li><%= link_to "Orders History", orders_path %></li>
<li class="divider"></li> <li class="divider"></li>
<li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li> <li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li>
</ul> </ul>
......
...@@ -9,10 +9,15 @@ ...@@ -9,10 +9,15 @@
</table> </table>
<div class='row'> <div class='row'>
<div class='col-md-5'></div> <div class='col-md-5'></div>
<% if current_cart.line_items.any? %>
<div class='col-md-1'> <div class='col-md-1'>
<%= link_to 'Checkout', new_order_path, class: 'btn btn-default' %> <%= link_to 'Checkout', new_order_path, class: 'btn btn-default' %>
</div> </div>
<div class='col-md-1'> <div class='col-md-1'>
<%= link_to 'Empty Cart', current_cart, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default' %> <%= link_to 'Empty Cart', current_cart, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default' %>
</div> </div>
<% end %>
<div class='col-md-1'>
<%= link_to 'Buy continue', root_path, class: 'btn btn-default' %>
</div>
</div> </div>
class CombineItemsInCart < ActiveRecord::Migration class CombineItemsInCart < ActiveRecord::Migration
def change def change
puts 'runs CombineItemsInCart'
Cart.all.each do |cart| Cart.all.each do |cart|
sums = cart.line_items.group(:product_id).sum(:quantity) sums = cart.line_items.group(:product_id).sum(:quantity)
......
class AddUserIdToCarts < ActiveRecord::Migration
def change
add_column :carts, :user_id, :integer
end
end
class AddUserIdToOrders < ActiveRecord::Migration
def change
add_column :orders, :user_id, :integer
end
end
...@@ -11,11 +11,12 @@ ...@@ -11,11 +11,12 @@
# #
# 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: 20141113084938) do ActiveRecord::Schema.define(version: 20141117032233) do
create_table "carts", force: true do |t| create_table "carts", force: true 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
end end
create_table "categories", force: true do |t| create_table "categories", force: true do |t|
...@@ -40,6 +41,7 @@ ActiveRecord::Schema.define(version: 20141113084938) do ...@@ -40,6 +41,7 @@ ActiveRecord::Schema.define(version: 20141113084938) do
t.string "pay_type", limit: 255 t.string "pay_type", limit: 255
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
end end
create_table "products", force: true do |t| create_table "products", force: true do |t|
......
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