Commit ba3ecacb by Nguyen Quoc Kien

Cars: finish

parent 43e93f27
......@@ -8,14 +8,13 @@ class ApplicationController < ActionController::Base
private
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@session = session
if current_user
@cart = Cart.create(user_id: current_user.id)
@user_id = current_user.id
else
@cart = Cart.create()
@user_id = "guess"
end
session[:cart_id] = @cart.id
@session[@user_id] ||= {}
end
def configure_permitted_parameters
......
class CartProductsController < ApplicationController
before_action :set_cart, only: [:create]
before_action :check_quantity?, only: [:create]
def create
product = Product.find(params[:product_id])
@cart_product = @cart.add_product(product.id, product.price)
if check_quantity?
add_product_to_cart(product.id.to_i, params[:quantity].to_i )
respond_to do |format|
if @cart_product.save
format.html { redirect_to @cart_product.cart }
format.json { render json: @cart_product,
status: :created, location: @cart_product }
format.html { redirect_to cart_path(id: @user_id),
notice: 'Products add to cart' }
format.json { head :no_content }
end
else
format.html { render action: "new" }
format.json { render json: @cart_product.errors,
status: :unprocessable_entity }
respond_to do |format|
format.html { redirect_to cart_path(id: @user_id),
notice: 'Errors: Quantity' }
format.json { head :no_content }
end
end
end
def destroy
@cart_product = CartProduct.find(params[:id])
@cart_product.destroy
redirect_to @cart_product.cart
session[params[:id]].delete(params[:product_id])
redirect_to cart_path(id: params[:id])
end
private
def add_product_to_cart(product_id, number)
number ||= 1
i = 0
@session[@user_id].each do |key, value|
if (key == product_id.to_s)
@session[@user_id][key] = number +value
i = 1
break
end
end
if (i == 0)
@session[@user_id][product_id] = number
end
end
def check_quantity?
if ( params[:quantity].to_i && params[:quantity].to_i > 0 )
return true
else false
end
end
end
class CartsController < ApplicationController
before_action :find_card, only: [ :update ]
def show
@cart = Cart.find(session[:cart_id])
total_price = @cart.total_price
@cart.update(total_price: total_price)
#before_action :find_card, only: [ :create ]
#before_action :check_phone, only: [ :create ]
def update
end
def edit
@cart = Cart.find(session[:cart_id])
def new
@cart = Cart.new
end
def update
@cart = Cart.find(session[:cart_id])
@user = User.find(@cart.user_id)
@user.update(address: params['cart']['address'], phone: params['cart']['phone'])
@cart.update(cart_params)
@cart.update(status: "checkout")
def create
total = 0
@cart = Cart.new(cart_params)
@cart.save
if current_user
@cart.update(user_id: current_user.id, status: "Checkout")
user_id = current_user.id
else
@cart.update(user_id: "", status: "Checkout")
user_id = 'guess'
end
if @cart.save
session[user_id].each do |key, value|
@product = Product.find(key)
@cart_product = CartProduct.new(cart_id: @cart.id, product_id: key.to_i, number: value.to_i, price: @product.price)
@cart_product.save
total += @product.price * value.to_f
end
@cart.update( total_price: total)
OrderNotifier.received(@cart).deliver
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to products_path,
notice: 'Email to send' }
format.json { head :no_content }
end
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
session[user_id] = nil
else
respond_to do |format|
format.html { redirect_to products_path,
notice: 'Your cart is currently empty' }
format.html { redirect_to carts_path,
notice: 'Errors' }
format.json { head :no_content }
end
end
end
private
def cart_params
params.require(:cart).permit(:full_name, :email, :address, :phone)
def destroy
session[params[:id]] = nil
redirect_to cart_path(params[:id])
end
def find_card
@cart = Cart.find(params[:id])
def index
if current_user
user_id = current_user.id
else
user_id = 'guess'
end
redirect_to cart_path(user_id)
end
def add_product_to_cart(product_id, quantity)
quantity ||= 1
product_id = product_id.to_s
current_quantity = cart_products_hash.fetch(product_id, {}).fetch('quantity', 0)
quantity += current_quantity
cart_products_hash[product_id] = { 'quantity' => quantity }
end
private
def remove_product_from_cart(product_id)
product_id = product_id.to_s
cart_products_hash.delete(product_id)
def cart_params
params.require(:cart).permit(:full_name, :email, :address, :phone)
end
def cart_hash
@session['cart']
def find_card
@cart = Cart.find(params[:id])
end
def cart_products_hash
@session['cart']['products']
def check_phone
params[:phone].is_a?
end
end
......@@ -11,9 +11,13 @@ class ProductsController < ApplicationController
@categories = Category.all
end
def new
end
private
def find_product
@product = Product.find(params[:id])
end
end
class Cart < ActiveRecord::Base
has_many :cart_products, dependent: :destroy
def add_product(product_id, price)
current_item = cart_products.find_by(product_id: product_id)
if current_item
current_item.number += 1
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :full_name, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 }
before_save :downcase_email
def downcase_email
self.email = email.downcase
end
def check_phone
if self.phone.is_a
return true
else
current_item = cart_products.build(product_id: product_id, price: price)
return false
end
current_item
end
def total_price
cart_products.to_a.sum { |item| item.total_price }
end
end
......@@ -3,7 +3,7 @@
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>Đăng ký thông tin nhận hàng</h1>
<%= form_for(@cart) do |f| %>
<%= form_for @cart do |f| %>
<%= f.label :full_name %>
<%= f.text_field :full_name, class: 'form-control' %>
......
<% provide(:title, "Your Carts") %>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h2>Your Cart</h2>
<table class="table table-hover">
<% if session[params[:id]] != nil && session[params[:id]] != {} %>
<thead>
<tr>
<th>Product name</th>
......@@ -14,30 +12,34 @@
</tr>
</thead>
<tbody>
<% @cart.cart_products.each do |item| %>
<% total = 0 %>
<% session[params[:id]].each do |key, value| %>
<tr>
<td><%= item.product.name %></td>
<td><%= number_to_currency(item.product.price/100.000) %></td>
<td><%= item.number %></td>
<td><%= number_to_currency((item.product.price * item.number)/100.000) %></td>
<td >
<%= link_to 'Delete', cart_product_path(id: item.id), data: { confirm: 'Are you sure?' }, method: :delete,
data: { confirm: 'Are you sure?' } %> |
<%= link_to 'Update', "#" %>
</td>
<td><%= Product.find(key).name %>
<td><%= number_to_currency(Product.find(key).price/100.00) %></td>
<td><%= value %></td>
<td><%= number_to_currency(Product.find(key).price/100.000 * value.to_f) %></td>
<% total += Product.find(key).price/100.000 * value.to_f %>
<td><%= link_to 'Delete', cart_product_path(product_id: key, id: params[:id]), data: { confirm: 'Are you sure?' }, method: :delete,
data: { confirm: 'Are you sure?' } %> </td>
</tr>
<% end %>
<tr>
<td colspan="3">Total: </td>
<td><%= number_to_currency(@cart.total_price/100.000) %></td>
<td>Total</td>
<td></td>
<td></td>
<td> <%= number_to_currency(total) %></td>
</tr>
<tr>
<td><%= link_to 'Back', products_path, class: "btn btn-danger" %></td>
<td> </td>
<td><%= button_to 'Empty cart', @cart, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
<td><%= button_to 'Checkout', edit_cart_path(@cart), method: :get, class: "btn btn-danger" %></td>
</tr>
<td><%= button_to 'Empty cart', cart_path(id: params[:id]), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
<td><%= link_to 'Checkout', new_cart_path, class: "btn btn-danger" %></td>
<td> </td>
</tr>
</tbody>
<% else %>
<h1>Cart Empty</h1>
<% end %>
</table>
\ No newline at end of file
......@@ -17,7 +17,7 @@
<li>
<%= link_to "Categories", categories_path %>
</li>
<li><%= link_to "Carts", "#" %></li>
<li><%= link_to "Carts", carts_path %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
......
<h1>OrderNotifier#shipped</h1>
<p>
<%= @greeting %>, find me in app/views/order_notifier/shipped.html.erb
<%= @greeting %>, 45
</p>
......@@ -6,9 +6,14 @@
<h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3>
</div>
<p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p>
<p>Quanlity: <%= text_field :number, class: 'form-control' %>
<%= button_to "Add To cart", cart_products_path(product_id: product), :class => "btn btn-primary", :style => "width: 100px" %> <br/><%= link_to "More Info", product, :class => "btn btn-default" %>
<%= form_tag cart_products_path do %>
<p>
<%= hidden_field_tag :product_id, product.id %>
Quantity:<b> <%= number_field_tag :quantity, "1", class: 'form-control' %></b><br/>
<%= submit_tag "Add To cart", :class => "btn btn-primary", :style => "width: 100px" %>
</p>
<% end %>
<%= link_to "More Info", product, :class => "btn btn-default" %>
</div>
</div>
</div>
......@@ -5,4 +5,5 @@
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
</p>
<p><%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-lg btn-primary" %></p>
<p><%= link_to "Admin", admins_path, class: "btn btn-lg btn-primary" %></p>
</header>
Rails.application.routes.draw do
devise_for :users
get 'carts/index'
devise_for :users
root to: "static_pages#home"
get 'help' => 'static_pages#help'
......@@ -11,6 +11,7 @@ Rails.application.routes.draw do
resources :products
resources :carts
resources :cart_products, only: [:create, :destroy]
resources :admins
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
......
......@@ -3,7 +3,7 @@ class CreateCarts < ActiveRecord::Migration
create_table :carts do |t|
t.integer :user_id
t.decimal :total_price, :default => 0
t.string :status, :default => "Prosess"
t.string :status
t.string :full_name
t.integer :phone
t.string :email
......
......@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
create_table "carts", force: :cascade do |t|
t.integer "user_id", limit: 4
t.decimal "total_price", precision: 10, default: 0
t.string "status", limit: 255, default: "Prosess"
t.string "status", limit: 255
t.string "full_name", limit: 255
t.integer "phone", limit: 4
t.string "email", limit: 255
......
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