Commit ce5b42b0 by Truong Ba Dieu

Fix pull request

parent f2b33c51
...@@ -6,17 +6,18 @@ class HomeController < ApplicationController ...@@ -6,17 +6,18 @@ class HomeController < ApplicationController
before_action :get_recommend before_action :get_recommend
def index def index
params[:per_page] ||= 5 @products = Product.order(release_date: :desc).page(params[:page])
params[:page] ||= 1
@products = Product.page(params[:page]).per(params[:per_page])
end end
def search def search
add_breadcrumb "Search"
add_breadcrumb "Search", :search_path add_breadcrumb "Search", :search_path
@products = Product.search(params) @products = Product.search(params)
end end
def cart def cart
add_breadcrumb "Cart"
render layout: "cart"
end end
......
...@@ -2,12 +2,9 @@ class OrdersController < ApplicationController ...@@ -2,12 +2,9 @@ class OrdersController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
layout "cart" layout "cart"
def edit
end
def update def update
current_order.update_attributes(update_order_params) current_order.update_attributes(update_order_params)
redirect_to edit_order_path(current_order), notice: "Update cart successfully" redirect_to cart_path(), notice: "Update cart successfully"
end end
def add_to_cart def add_to_cart
......
...@@ -3,9 +3,12 @@ class LineItem < ActiveRecord::Base ...@@ -3,9 +3,12 @@ class LineItem < ActiveRecord::Base
belongs_to :product belongs_to :product
validate :check_product_stock validate :check_product_stock
after_save :trigger_recalculate after_save :trigger_recalculate
before_destroy :trigger_calculate_order before_destroy :trigger_calculate_order
private
def trigger_recalculate def trigger_recalculate
order.recalculate order.recalculate
change_square = quantity - quantity_was change_square = quantity - quantity_was
......
...@@ -7,12 +7,12 @@ class Order < ActiveRecord::Base ...@@ -7,12 +7,12 @@ class Order < ActiveRecord::Base
accepts_nested_attributes_for :line_items accepts_nested_attributes_for :line_items
def recalculate def recalculate
puts "=================================== #{line_items.inject(0) {|sum, item| sum + item.quantity*item.price }}"
count = line_items.sum(:quantity) count = line_items.sum(:quantity)
total_price = 0 self.update_columns(
line_items.each do |item| item_count: count,
total_price += item.quantity*item.price item_total: line_items.inject(0) {|sum, item| sum + item.quantity*item.price }
end )
self.update_columns(item_count: count, item_total: total_price)
end end
def checkout def checkout
......
...@@ -14,10 +14,9 @@ class Product < ActiveRecord::Base ...@@ -14,10 +14,9 @@ class Product < ActiveRecord::Base
scope :recommend, -> { where(recommend: true) } scope :recommend, -> { where(recommend: true) }
def self.search(params) def self.search(params)
Product.order("release_date DESC").page(params[:page]) Product.order(release_date: :desc).page(params[:page])
end end
def can_buy?(quantity) def can_buy?(quantity)
stock >= quantity.to_i stock >= quantity.to_i
end end
......
...@@ -4,9 +4,8 @@ class CartService ...@@ -4,9 +4,8 @@ class CartService
params[:quantity] ||= 1 params[:quantity] ||= 1
product = Product.find(params[:product_id]) product = Product.find(params[:product_id])
if product.can_buy?(params[:quantity]) if product.can_buy?(params[:quantity])
line_item = current_order.line_items.where(product_id: product.id).first_or_create line_item = current_order.line_items.where(product_id: product.id, price: product.price).first_or_create
line_item.quantity += params[:quantity].to_i line_item.quantity += params[:quantity].to_i
line_item.price = product.price
if line_item.save if line_item.save
return true, "Add to cart successfully" return true, "Add to cart successfully"
else else
......
...@@ -18,7 +18,7 @@ nav.navbar.navbar-inverse ...@@ -18,7 +18,7 @@ nav.navbar.navbar-inverse
ul.nav.navbar-nav.navbar-right ul.nav.navbar-nav.navbar-right
- if current_user.present? - if current_user.present?
li li
= link_to "Cart (#{current_order.item_count})", edit_order_path(current_order) = link_to "Cart (#{current_order.item_count})", cart_path
li li
= current_user.decorate.edit_profile_link = current_user.decorate.edit_profile_link
li li
......
...@@ -6,10 +6,11 @@ Rails.application.routes.draw do ...@@ -6,10 +6,11 @@ Rails.application.routes.draw do
# You can have the root of your site routed with "root" # You can have the root of your site routed with "root"
root 'home#index' root 'home#index'
get '/search' => "home#search", as: :search get '/search' => "home#search", as: :search
get '/cart' => "home#cart", as: :cart
resources :categories, :only => [:show] resources :categories, :only => [:show]
resources :products, :only => [:new, :create, :show] resources :products, :only => [:new, :create, :show]
resources :orders, :only => [:edit, :update, :show] do resources :orders, :only => [:update, :show] do
collection do collection do
get "checkout" => "orders#checkout", as: :checkout get "checkout" => "orders#checkout", as: :checkout
post "add_to_cart" => "orders#add_to_cart", as: :add_to_cart post "add_to_cart" => "orders#add_to_cart", as: :add_to_cart
......
class ChangeTypeFloatToDecimal < ActiveRecord::Migration
def up
change_column :orders, :item_total, :decimal, :precision => 12, :scale => 2
change_column :line_items, :price, :decimal, :precision => 12, :scale => 2
change_column :products, :price, :decimal, :precision => 12, :scale => 2
end
def down
change_column :orders, :item_total, :float
change_column :line_items, :item_total, :float
change_column :products, :item_total, :float
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,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: 20150715093529) do ActiveRecord::Schema.define(version: 20150716093626) do
create_table "categories", force: :cascade do |t| create_table "categories", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 255
...@@ -20,36 +20,36 @@ ActiveRecord::Schema.define(version: 20150715093529) do ...@@ -20,36 +20,36 @@ ActiveRecord::Schema.define(version: 20150715093529) do
create_table "line_items", force: :cascade do |t| create_table "line_items", force: :cascade do |t|
t.integer "product_id", limit: 4 t.integer "product_id", limit: 4
t.integer "order_id", limit: 4 t.integer "order_id", limit: 4
t.integer "quantity", limit: 4, default: 0 t.integer "quantity", limit: 4, default: 0
t.float "price", limit: 24 t.decimal "price", precision: 12, scale: 2
end end
create_table "orders", force: :cascade do |t| create_table "orders", force: :cascade do |t|
t.string "pid", limit: 255 t.string "pid", limit: 255
t.integer "item_count", limit: 4, default: 0 t.integer "item_count", limit: 4, default: 0
t.float "item_total", limit: 24, default: 0.0 t.decimal "item_total", precision: 12, scale: 2, default: 0.0
t.integer "state", limit: 4, default: 1 t.integer "state", limit: 4, default: 1
t.datetime "completed_at" t.datetime "completed_at"
t.integer "user_id", limit: 4 t.integer "user_id", limit: 4
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "products", force: :cascade do |t| create_table "products", force: :cascade do |t|
t.string "pid", limit: 255 t.string "pid", limit: 255
t.string "title", limit: 1024, default: "", null: false t.string "title", limit: 1024, default: "", null: false
t.string "author", limit: 255 t.string "author", limit: 255
t.string "publisher", limit: 255 t.string "publisher", limit: 255
t.string "studio", limit: 255 t.string "studio", limit: 255
t.float "price", limit: 24 t.decimal "price", precision: 12, scale: 2
t.string "currency", limit: 255 t.string "currency", limit: 255
t.integer "category_id", limit: 4 t.integer "category_id", limit: 4
t.integer "user_id", limit: 4 t.integer "user_id", limit: 4
t.string "image_uid", limit: 255 t.string "image_uid", limit: 255
t.datetime "release_date" t.datetime "release_date"
t.datetime "public_date" t.datetime "public_date"
t.boolean "recommend", limit: 1, default: false t.boolean "recommend", limit: 1, default: false
t.integer "stock", limit: 4, default: 10 t.integer "stock", limit: 4, default: 10
end end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade 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