Commit ce5b42b0 by Truong Ba Dieu

Fix pull request

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