Commit a1294599 by Hoang Phuc Do

Merge branch into dhp_cart

# Conflicts:
#	app/assets/javascripts/shop-14.js.erb
#	app/controllers/application_controller.rb
#	app/controllers/carts_controller.rb
#	app/helpers/carts_helper.rb
#	app/views/carts/_cart_table.html.erb
#	config/database.yml
parents e78b364a 55214d21
......@@ -6,11 +6,7 @@
});
$('.qty-input').TouchSpin();
$('.qty-input').on('change', function() {
$.ajax({
url: '/cart/update',
method: 'POST',
data: {qty: $(this).val(), product_id: $(this).data('product-id')}
});
$(this).closest('form').submit();
});
}
}).apply(this, [jQuery]);
\ No newline at end of file
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
# rescue_from ActiveRecord::RecordNotFound, :with => :render_404
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
include CartsHelper
before_action :set_cart
before_action :configure_permitted_parameters, if: :devise_controller?
......
class CartsController < ApplicationController
include CartsHelper
include LineItemsHelper
before_action :set_cart
before_action :get_line_items
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def index
@line_items = {}
@cart.each do |product_id, product_attrs|
@line_items[product_id] = { product: Product.find(product_id),
quantity: product_attrs['quantity'].to_i }
end
end
def delete_line_item
remove_from_cart(params[:id])
product_title = Product.select(:title).find(params[:id]).title
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product_title} was sucessfully removed from your cart" }
format.js
end
end
# DELETE /cart/remove
def destroy
session[:cart] = nil
respond_to do |format|
format.html { redirect_to root_url }
format.json { head :no_content }
end
end
destroy_cart_session
def update
update_cart_item(update_params[:product_id], update_params[:qty])
line_items = {}
session[:cart].each do |product_id, product_attrs|
line_items[product_id] = { product: Product.find(product_id),
quantity: product_attrs['quantity'].to_i }
end
respond_to do |format|
format.html { redirect_to cart_index_url }
format.html { redirect_to root_url }
end
end
private
def update_params
params.permit(:product_id, :qty)
end
def invalid_cart
redirect_to root_url
end
......
class LineItemsController < ApplicationController
include CartsHelper
before_action :set_cart, only: [:create]
before_action :set_cart, only: [:create, :update, :destroy]
def show
redirect_to product_url(params[:id])
end
# POST /line_items
def create
product = Product.find(params[:product_id])
add_to_cart(product, params[:product_vqty].to_i)
add_to_cart(product.id.to_s, params[:product_vqty].to_i)
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully added to your cart" }
format.js
format.json { render 'show' }
end
end
# PATCH/PUT /line_items/1
def update
update_cart_item(*update_params.to_h.values)
respond_to do |format|
format.html { redirect_to cart_index_url }
end
end
# DELETE /line_items/1
def destroy
remove_from_cart(params[:id])
product_title = Product.select(:title).find(params[:id]).title
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product_title} was sucessfully removed from your cart" }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def update_params
params.permit(:id, :qty_input)
end
end
\ No newline at end of file
class OrdersController < ApplicationController
include CartsHelper
include LineItemsHelper
before_action :authenticate_user!, only: :new
before_action :set_cart, only: [:new, :create]
before_action :get_line_items, only: :new
before_action :ensure_cart_is_not_empty, only: :new
# GET /orders/new
def new
@order = Order.new
end
# POST /orders
def create
@order = current_user.orders.create
create_line_items_for_order(@order)
......@@ -16,12 +20,10 @@ class OrdersController < ApplicationController
respond_to do |format|
if @order.save
destroy_cart_session
OrderMailer.order_detail(current_user).deliver_now
OrderMailer.order_detail(current_user, @order).deliver_now
format.html { redirect_to root_url }
format.json { render :show }
else
format.html { redirect_to root_url }
format.json { render json: @order.errors }
end
end
end
......@@ -35,6 +37,6 @@ class OrdersController < ApplicationController
private
def ensure_cart_is_not_empty
redirect_to root_url if @cart.empty?
redirect_to root_url if @cart.blank?
end
end
\ No newline at end of file
module CartsHelper
def set_cart
return @cart = session[:cart] unless session[:cart].nil?
session[:cart] = {}
session[:cart] ||= {}
@cart = session[:cart]
end
def add_to_cart(product, quantity)
cart_key = product.id.to_s
return remove_from_cart(cart_key) if quantity.zero?
if @cart.key?(cart_key)
@cart[cart_key][:quantity.to_s] += quantity
def add_to_cart(product_id, quantity)
return remove_from_cart(product_id) if quantity.zero?
if @cart.key?(product_id)
@cart[product_id][:quantity.to_s] += quantity
else
@cart[cart_key] = { quantity: quantity }
@cart[product_id] = { quantity: quantity }
end
end
......@@ -33,20 +31,14 @@ module CartsHelper
end
def cart_total_price
total_price = 0
@cart.each do |product_id, product_attrs|
@cart.sum do |product_id, product_attrs|
product_price = Product.select(:price).find(product_id).price
total_price += (product_price * product_attrs['quantity'].to_i)
product_price * product_attrs['quantity'].to_i
end
total_price
end
def cart_total_items
if session[:cart].nil?
total = 0
else
total = session[:cart].sum { |product_id, product_attrs| product_attrs[:quantity.to_s] || product_attrs[:quantity] }
end
total
return 0 if @cart.nil?
@cart.sum { |_, product_attrs| product_attrs[:quantity.to_s] || product_attrs[:quantity] }
end
end
module LineItemsHelper
def get_line_items
@line_items = {}
@cart.each do |product_id, product_attrs|
@line_items[product_id] = { product: Product.find(product_id),
quantity: product_attrs['quantity'].to_i }
end
end
end
\ No newline at end of file
class OrderMailer < ApplicationMailer
def order_detail(user)
def order_detail(user, order)
@user = user
@order = order
mail to: user.email, subject: "Order detail"
end
......
......@@ -2,9 +2,7 @@ class Order < ApplicationRecord
has_many :line_items, dependent: :destroy
belongs_to :user
def add_line_items_from_cart(cart)
cart.each do |product_id, product_attrs|
line_items.create
end
def total_price
line_items.to_a.sum { |item| item.product.price * item.quantity }
end
end
\ No newline at end of file
<tr>
<td colspan="6">
<%= link_to 'Continue shopping', root_url, class: 'btn btn-default hover-primary btn-continue' %>
<%= button_to 'Clear Shopping Cart', remove_cart_path, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default hover-primary btn-clear' %>
</td>
</tr>
\ No newline at end of file
......@@ -11,33 +11,10 @@
</thead>
<tbody>
<% @line_items.each do |id, line_item| %>
<tr>
<td class="product-action-td">
<%= link_to fa_icon('times'), remove_line_item_cart_path(id), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<td class="product-image-td"></td>
<td class="product-name-td">
<h2 class="product-name">
<%= link_to line_item[:product].title, product_url(line_item[:product]) %>
</h2>
</td>
<td><%= number_to_currency(line_item[:product].price) %></td>
<td>
<%= form_tag do %>
<div class="qty-holder">
<%= number_field_tag :qty_input, line_item[:quantity], data: { product_id: id}, class: 'qty-input' %>
</div>
<% end %>
</td>
<td><%= number_to_currency(line_item[:product].price * line_item[:quantity]) %></td>
</tr>
<%= render partial: 'line_item', locals: { id: id, line_item: line_item } %>
<% end %>
</tbody>
<tfoot>
<tr>
<td colspan="6">
<%= button_to 'Clear Shopping Cart', remove_cart_path, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default hover-primary btn-clear' %>
</td>
</tr>
<%= render 'cart_actions' %>
</tfoot>
</table>
\ No newline at end of file
<tr>
<td class="product-action-td">
<%= link_to fa_icon('times'), line_item_path(id), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<td class="product-image-td"></td>
<td class="product-name-td">
<h2 class="product-name">
<%= link_to line_item[:product].title, product_url(line_item[:product]) %>
</h2>
</td>
<td><%= number_to_currency(line_item[:product].price) %></td>
<td>
<%= form_tag line_item_path(id), method: :put do %>
<div class="qty-holder">
<%= number_field_tag :qty_input, line_item[:quantity], data: { product_id: id}, class: 'qty-input' %>
</div>
<% end %>
</td>
<td><%= number_to_currency(line_item[:product].price * line_item[:quantity]) %></td>
</tr>
\ No newline at end of file
<tr>
<td class="product-action-td">
<%= link_to fa_icon('times'), line_item, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<td class="product-image-td"></td>
<td class="product-name-td">
<h2 class="product-name">
<%= link_to line_item.product.title, product_url(line_item.product) %>
<%= link_to line_item[:product].title, product_url(line_item[:product]) %>
</h2>
</td>
<td><%= number_to_currency(line_item.product.price) %></td>
<td><%= line_item.quantity %></td>
<td><%= number_to_currency(line_item.total_price) %></td>
<td><%= number_to_currency(line_item[:product].price) %></td>
<td><%= line_item[:quantity]%></td>
<td><%= number_to_currency(line_item[:product].price * line_item[:quantity]) %></td>
</tr>
\ No newline at end of file
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
body {
}
.ExternalClass table {
border-collapse: separate;
}
a, a:link, a:visited {
text-decoration: none;
color: #00788a
}
a:hover {
text-decoration: underline;
}
h2, h2 a, h2 a:visited, h3, h3 a, h3 a:visited, h4, h5, h6 {
color: #000 !important
}
.ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td {
line-height: 100%
}
.ExternalClass * {
line-height: 100%
}
</style>
</head>
<body>
<h1><%= @user.email %></h1>
<p>
Your order has been created
</p>
<body style="margin: 0; padding: 0;">
<table width="100%" cellpadding="10" cellspacing="0" border="0" bgcolor="#CCCCCC" align="center">
<tr>
<td>
<table width="600" align="center" cellpadding="10" cellspacing="0" border="0" bgcolor="#FFFFFF" style="border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 12px; margin: 0 auto;">
<tr>
<td>
<table align="center" width="580" border="0" cellpadding="10" cellspacing="0" class="html-email" style="border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 12px;">
<tr>
<td width="580" colspan="2" style="border: 1px solid #CCCCCC;">
<strong>Hello <%= @user.email %>,</strong>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table align="center" width="580" border="0" cellpadding="10" cellspacing="0" class="html-email" style="border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 12px;">
<tr>
<td align="left" style="border: 1px solid #CCCCCC;">
Your order number: <br/>
<strong><%= @order.id %></strong>
</td>
<td align="left" style="border: 1px solid #CCCCCC;">
Your order total: <br/>
<strong><%= number_to_currency(@order.total_price) %></strong>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table align="center" class="html-email" width="580" cellspacing="0" cellpadding="5" border="0" style="border-collapse: collapse; font-size: 12px;">
<tr align="left" class="sectiontableheader">
<th align="left" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">SKU</th>
<th align="center" bgcolor="#EEEEEE" colspan="2" style="border: 1px solid #CCCCCC;">Product Name</th>
<th align="center" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">Qty</th>
<th align="center" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">Price</th>
<th align="center" bgcolor="#EEEEEE" style="border: 1px solid #CCCCCC;">Total</th>
</tr>
<% @order.line_items.each do |item| %>
<tr valign="top">
<td align="left" style="border: 1px solid #CCCCCC;">
<%= item.product.id %>
</td>
<td align="center" colspan="2" style="border: 1px solid #CCCCCC;">
<%= item.product.title %>
</td>
<td align="right" style="border: 1px solid #CCCCCC;">
<%= item.quantity %>
</td>
<td align="right" class="priceCol" style="border: 1px solid #CCCCCC; white-space: nowrap;">
<span class='priceColor2'><%= number_to_currency(item.product.price) %></span>
</td>
<td align="right" class="priceCol" style="border: 1px solid #CCCCCC; white-space: nowrap;">
<span class='priceColor2'><%= number_to_currency(item.product.price * item.quantity) %></span>
</td>
</tr>
<% end %>
<tr>
<td align="right" class="pricePad" colspan="5" style="border: 1px solid #CCCCCC;"><strong>Total</strong></td>
<td align="right" style="border: 1px solid #CCCCCC; white-space: nowrap;">
<strong><%= number_to_currency(@order.total_price) %></strong>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
</html>
......@@ -16,11 +16,29 @@
</div>
</div>
</div>
<div class="cart-table-wrap">
<table class="cart-table">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>Unit Price</th>
<th>Qty</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<% @line_items.each do |id, line_item| %>
<%= render partial: 'line_items/line_item', locals: {id: id, line_item: line_item} %>
<% end %>
</tbody>
</table>
</div>
<div class="form-col">
<div class="checkout-review-action">
<h5>Grand Total <span><%= number_to_currency(cart_total_price) %></span></h5>
<%= form_for @order do |f| %>
<%= f.submit 'Place Order now', class: 'btn btn-primary' %>
<%= f.submit 'Place Order now', class: 'btn btn-primary' %>
<% end %>
</div>
</div>
......
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: 123123
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: dhp_venshop_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: dhp_venshop_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: dhp_venshop_production
username: dhp_venshop
password: <%= ENV['DHP_VENSHOP_DATABASE_PASSWORD'] %>
......@@ -6,8 +6,6 @@ Rails.application.routes.draw do
resources :orders, only: [:new, :create, :show]
resources :line_items
resources :cart, only: :index, controller: 'carts'
delete '/cart/remove/line_item/:id', to: 'carts#delete_line_item', as: 'remove_line_item_cart'
delete '/cart/remove', to: 'carts#destroy', as: 'remove_cart'
post 'cart/update', to: 'carts#update', as: 'update_cart'
devise_for :users
end
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