Commit f0d57a97 by Trong Huu Nguyen

Merge branch 'dhp_cart' into 'development'

[Review] Cart feature

See merge request !3
parents 92e3a257 ccd4ffc7
...@@ -44,6 +44,14 @@ gem 'kaminari', '~> 1.0', '>= 1.0.1' ...@@ -44,6 +44,14 @@ gem 'kaminari', '~> 1.0', '>= 1.0.1'
gem 'devise', '~> 4.3' gem 'devise', '~> 4.3'
# Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide. # Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide.
gem 'rubocop', '~> 0.49.1' gem 'rubocop', '~> 0.49.1'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# FontAwesome
gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.2'
# Bootstrap TouchSpin is a mobile and touch friendly input spinner component for Bootstrap
gem 'rails-assets-bootstrap-touchspin', source: 'https://rails-assets.org'
# Simple, efficient background processing for Ruby.
gem 'sidekiq', '~> 5.0', '>= 5.0.2'
# Use Capistrano for deployment # Use Capistrano for deployment
# gem 'capistrano-rails', group: :development # gem 'capistrano-rails', group: :development
...@@ -63,6 +71,8 @@ group :development do ...@@ -63,6 +71,8 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring' gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0' gem 'spring-watcher-listen', '~> 2.0.0'
# When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending.
gem 'letter_opener', '~> 1.4', '>= 1.4.1'
end end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
......
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
remote: https://rails-assets.org/
specs: specs:
actioncable (5.1.1) actioncable (5.1.1)
actionpack (= 5.1.1) actionpack (= 5.1.1)
...@@ -72,6 +73,7 @@ GEM ...@@ -72,6 +73,7 @@ GEM
execjs execjs
coffee-script-source (1.12.2) coffee-script-source (1.12.2)
concurrent-ruby (1.0.5) concurrent-ruby (1.0.5)
connection_pool (2.2.1)
devise (4.3.0) devise (4.3.0)
bcrypt (~> 3.0) bcrypt (~> 3.0)
orm_adapter (~> 0.1) orm_adapter (~> 0.1)
...@@ -83,12 +85,18 @@ GEM ...@@ -83,12 +85,18 @@ GEM
faker (1.6.6) faker (1.6.6)
i18n (~> 0.5) i18n (~> 0.5)
ffi (1.9.18) ffi (1.9.18)
font-awesome-rails (4.7.0.2)
railties (>= 3.2, < 5.2)
globalid (0.4.0) globalid (0.4.0)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
i18n (0.8.4) i18n (0.8.4)
jbuilder (2.7.0) jbuilder (2.7.0)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
multi_json (>= 1.2) multi_json (>= 1.2)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
kaminari (1.0.1) kaminari (1.0.1)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
kaminari-actionview (= 1.0.1) kaminari-actionview (= 1.0.1)
...@@ -101,6 +109,10 @@ GEM ...@@ -101,6 +109,10 @@ GEM
activerecord activerecord
kaminari-core (= 1.0.1) kaminari-core (= 1.0.1)
kaminari-core (1.0.1) kaminari-core (1.0.1)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.4.1)
launchy (~> 2.2)
listen (3.1.5) listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4) rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7) rb-inotify (~> 0.9, >= 0.9.7)
...@@ -129,6 +141,8 @@ GEM ...@@ -129,6 +141,8 @@ GEM
public_suffix (2.0.5) public_suffix (2.0.5)
puma (3.9.1) puma (3.9.1)
rack (2.0.3) rack (2.0.3)
rack-protection (2.0.0)
rack
rack-test (0.6.3) rack-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
rails (5.1.1) rails (5.1.1)
...@@ -143,6 +157,12 @@ GEM ...@@ -143,6 +157,12 @@ GEM
bundler (>= 1.3.0, < 2.0) bundler (>= 1.3.0, < 2.0)
railties (= 5.1.1) railties (= 5.1.1)
sprockets-rails (>= 2.0.0) sprockets-rails (>= 2.0.0)
rails-assets-bootstrap (3.3.7)
rails-assets-jquery (>= 1.9.1, < 4)
rails-assets-bootstrap-touchspin (3.1.2)
rails-assets-bootstrap (>= 3.0.0)
rails-assets-jquery (>= 1.9.0)
rails-assets-jquery (3.2.1)
rails-dom-testing (2.0.3) rails-dom-testing (2.0.3)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
nokogiri (>= 1.6) nokogiri (>= 1.6)
...@@ -160,6 +180,7 @@ GEM ...@@ -160,6 +180,7 @@ GEM
rb-fsevent (0.9.8) rb-fsevent (0.9.8)
rb-inotify (0.9.8) rb-inotify (0.9.8)
ffi (>= 0.5.0) ffi (>= 0.5.0)
redis (3.3.3)
responders (2.4.0) responders (2.4.0)
actionpack (>= 4.2.0, < 5.3) actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3) railties (>= 4.2.0, < 5.3)
...@@ -184,6 +205,11 @@ GEM ...@@ -184,6 +205,11 @@ GEM
childprocess (~> 0.5) childprocess (~> 0.5)
rubyzip (~> 1.0) rubyzip (~> 1.0)
websocket (~> 1.0) websocket (~> 1.0)
sidekiq (5.0.2)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (~> 3.3, >= 3.3.3)
spring (2.0.2) spring (2.0.2)
activesupport (>= 4.2) activesupport (>= 4.2)
spring-watcher-listen (2.0.1) spring-watcher-listen (2.0.1)
...@@ -232,16 +258,21 @@ DEPENDENCIES ...@@ -232,16 +258,21 @@ DEPENDENCIES
coffee-rails (~> 4.2) coffee-rails (~> 4.2)
devise (~> 4.3) devise (~> 4.3)
faker (~> 1.6, >= 1.6.3) faker (~> 1.6, >= 1.6.3)
font-awesome-rails (~> 4.7, >= 4.7.0.2)
jbuilder (~> 2.5) jbuilder (~> 2.5)
jquery-rails
kaminari (~> 1.0, >= 1.0.1) kaminari (~> 1.0, >= 1.0.1)
letter_opener (~> 1.4, >= 1.4.1)
listen (>= 3.0.5, < 3.2) listen (>= 3.0.5, < 3.2)
mini_magick mini_magick
mysql2 (>= 0.3.18, < 0.5) mysql2 (>= 0.3.18, < 0.5)
puma (~> 3.7) puma (~> 3.7)
rails (~> 5.1.1) rails (~> 5.1.1)
rails-assets-bootstrap-touchspin!
rubocop (~> 0.49.1) rubocop (~> 0.49.1)
sass-rails (~> 5.0) sass-rails (~> 5.0)
selenium-webdriver selenium-webdriver
sidekiq (~> 5.0, >= 5.0.2)
spring spring
spring-watcher-listen (~> 2.0.0) spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5) turbolinks (~> 5)
...@@ -250,4 +281,4 @@ DEPENDENCIES ...@@ -250,4 +281,4 @@ DEPENDENCIES
web-console (>= 3.3.0) web-console (>= 3.3.0)
BUNDLED WITH BUNDLED WITH
1.15.0 1.15.1
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg">
<metadata>Copyright (C) 2015 by original authors @ fontello.com</metadata>
<defs>
<font id="minicart-font" horiz-adv-x="1000" >
<font-face font-family="minicart-font" font-weight="400" font-stretch="normal" units-per-em="1000" ascent="850" descent="-150" />
<missing-glyph horiz-adv-x="1000" />
<glyph glyph-name="glyph" unicode="&#xe800;" d="m225 600c0 25 19 45 43 45 25 0 45-20 45-45 0-23-20-43-45-43-24 0-43 20-43 43z m478 0c0 25 20 45 43 45 24 0 43-20 43-45 0-23-19-43-43-43-23 0-43 20-43 43z m-488 121c-65 0-115-55-115-121l-67-566c0-67 51-122 114-122l707 0c62 0 113 55 113 122l-67 566c0 66-50 121-115 121z m553 0c-26 110-135 192-266 192-131 0-240-82-266-192l80 0c24 70 98 123 186 123 88 0 162-53 186-123z" horiz-adv-x="1000" />
</font>
</defs>
</svg>
\ No newline at end of file
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives. // about supported directives.
// //
//= require rails-ujs //= require jquery
//= require jquery_ujs
//= require bootstrap-touchspin
//# require rails-ujs
//= require turbolinks //= require turbolinks
//# require_tree . //# require_tree .
(function($) {
// Vertical Spinner - Touchspin - Product Details Quantity input
if ( $.fn.TouchSpin ) {
$('#product_quantity').TouchSpin({
verticalbuttons: true
});
$('.qty-input').TouchSpin();
$('.qty-input').on('change', function() {
$(this).closest('form').submit();
});
}
}).apply(this, [jQuery]);
\ No newline at end of file
(function($) {
}).apply(this, [jQuery]);
\ No newline at end of file
//= require cart/cart
\ No newline at end of file
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
* *
*# require_tree . *# require_tree .
*= require_self *= require_self
*= require bootstrap-touchspin
*= require custom *= require custom
*= require main_style *= require main_style
*= require skin_14 *= require skin_14
......
// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables" // "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
@import "bootstrap-sprockets"; @import "bootstrap-sprockets";
@import "bootstrap"; @import "bootstrap";
@import "font-awesome";
\ No newline at end of file
body { body {
font-size: 13px; font-size: 13px;
font-family: "Open Sans", Arial, sans-serif;
color: #777;
line-height: 1.5; line-height: 1.5;
background: image-url('shop-14-bg.png') top center repeat-x; background: transparent image-url('shop-14-bg.png') repeat-x top center;
} }
a { a {
color: #000000; color: #000000;
...@@ -139,6 +141,8 @@ html .btn-primary:active:focus { ...@@ -139,6 +141,8 @@ html .btn-primary:active:focus {
#header .header-container { #header .header-container {
padding-top: 28px; padding-top: 28px;
padding-bottom: 28px; padding-bottom: 28px;
position: relative;
display: table;
} }
#header .header-container.header-nav { #header .header-container.header-nav {
padding: 0; padding: 0;
......
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
include CartsHelper
protect_from_forgery with: :exception protect_from_forgery with: :exception
# rescue_from ActiveRecord::RecordNotFound, :with => :render_404
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
before_action :set_cart
before_action :configure_permitted_parameters, if: :devise_controller?
def render_404 def render_404
respond_to do |format| respond_to do |format|
...@@ -9,4 +15,12 @@ class ApplicationController < ActionController::Base ...@@ -9,4 +15,12 @@ class ApplicationController < ActionController::Base
format.any { head :not_found } format.any { head :not_found }
end end
end end
protected
def configure_permitted_parameters
added_attrs = [:username, :first_name, :last_name, :email, :password, :password_confirmation]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
end end
class CartsController < ApplicationController
include CartsHelper
include LineItemsHelper
before_action :set_cart
before_action :get_line_items
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
# DELETE /cart/remove
def destroy
destroy_cart_session
respond_to do |format|
format.html { redirect_to root_url }
end
end
private
def invalid_cart
redirect_to root_url
end
end
\ No newline at end of file
class LineItemsController < ApplicationController
include CartsHelper
before_action :set_cart, only: [:create, :update, :destroy]
# We don not render line item details page
def show
redirect_to product_url(params[:id])
end
# POST /line_items
def create
product = Product.find(params[:product_id])
add_to_cart(product.id.to_s, params[:product_quantity].to_i)
respond_to do |format|
format.html { redirect_to cart_index_url, notice: "#{product.title} was sucessfully added to your cart" }
format.js
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_line_item_from_cart(params[:id])
product = Product.find(params[:id])
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
# GET /orders/new
def new
redirect_to root_url if @cart.blank?
@order = Order.new
end
# POST /orders
def create
@order = current_user.orders.create
create_line_items_for_order(@order)
respond_to do |format|
if @order.save
destroy_cart_session
OrderMailer.send_order_detail_to_user(current_user, @order).deliver_later
format.html { redirect_to root_url, notice: 'Your order is successfully created' }
else
format.html { redirect_to root_url, notice: 'Your order can not be created' }
end
end
end
def create_line_items_for_order(order)
@cart.each do |product_id, product_attrs|
order.line_items.create(product_id: product_id, quantity: product_attrs['quantity'])
end
end
end
\ No newline at end of file
...@@ -3,5 +3,6 @@ class StaticPagesController < ApplicationController ...@@ -3,5 +3,6 @@ class StaticPagesController < ApplicationController
@latest_products = Product.page(params[:page]).per(10) @latest_products = Product.page(params[:page]).per(10)
# @TODO: Get recommended products # @TODO: Get recommended products
@recommended_products = Product.last(6) @recommended_products = Product.last(6)
# session.clear
end end
end end
\ No newline at end of file
module CartsHelper
def set_cart
session[:cart] ||= {}
@cart = session[:cart]
end
def add_to_cart(product_id, quantity)
return if quantity.zero?
if @cart.key?(product_id)
@cart[product_id][:quantity.to_s] += quantity
else
@cart[product_id] = { quantity: quantity }
end
end
def update_cart_item(product_id, quantity)
return unless @cart.key?(product_id)
if quantity.to_i.zero?
remove_line_item_from_cart(product_id)
else
@cart[product_id][:quantity.to_s] = quantity.to_i
end
end
def remove_line_item_from_cart(product_id)
@cart.tap { |cart| cart.delete(product_id) } if @cart.key?(product_id)
end
def destroy_cart_session
session[:cart] = nil
end
def cart_total_price
@cart.sum do |product_id, product_attrs|
product = Product.find(product_id)
product.price * product_attrs[:quantity.to_s].to_i
end
end
def cart_total_items
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 send_order_detail_to_user(user, order)
@user = user
@order = order
mail to: user.email, subject: "Order detail #{@order.id}"
end
end
class LineItem < ApplicationRecord
belongs_to :product, optional: true
belongs_to :order, optional: true
def total_price
product.price * quantity
end
end
\ No newline at end of file
class Order < ApplicationRecord
has_many :line_items, dependent: :destroy
belongs_to :user
def total_price
line_items.to_a.sum { |item| item.product.price * item.quantity }
end
end
\ No newline at end of file
...@@ -2,6 +2,20 @@ class User < ApplicationRecord ...@@ -2,6 +2,20 @@ class User < ApplicationRecord
# 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, :authentication_keys => [:login]
attr_accessor :login
has_many :products, dependent: :destroy has_many :products, dependent: :destroy
has_many :orders, dependent: :destroy
validates :username, format: { with: /^[a-zA-Z0-9_\.]*$/, multiline: true }
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", {value: login.strip.downcase}]).first
end
def validate_username
errors.add(:username, :invalid) if User.where(email: username).exists?
end
end end
<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
<%= cart_total_items %>
\ No newline at end of file
<table class="cart-table">
<thead>
<tr>
<th></th>
<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_item', locals: { id: id, line_item: line_item } %>
<% end %>
</tbody>
<tfoot>
<%= 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
<div class="cart">
<div class="container">
<h1 class="h2 heading-primary mt-lg clearfix">
Shopping Cart
</h1>
<div class="row">
<div class="col-md-8 col-lg-9">
<div class="cart-table-wrap">
<% if notice.presence %>
<div class="alert alert-success"><%= notice %></div>
<% end %>
<%= render 'cart_table' %>
</div>
</div>
<aside class="col-md-4 col-lg-3 sidebar shop-sidebar">
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Cart Totals</h4>
</div>
<div id="panel-cart-total">
<div class="panel-body">
<table class="totals-table">
<tbody>
<tr>
<td>Grand Total</td>
<td><%= number_to_currency(cart_total_price) %></td>
</tr>
</tbody>
</table>
<div class="totals-table-action">
<%= button_to 'Proceed to checkout', new_order_path, method: :get, class: 'btn btn-primary btn-block' %>
</div>
</div>
</div>
</div>
</div>
</aside>
</div>
</div>
</div>
\ No newline at end of file
...@@ -10,12 +10,12 @@ ...@@ -10,12 +10,12 @@
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="form-content"> <div class="form-content">
<h3 class="heading-text-color font-weight-normal"> <h3 class="heading-text-color font-weight-normal">
Registered Customers
</h3> </h3>
<div class="form-group"> <div class="form-group">
<%= f.label :email %> <%= f.label :login %>
<%= f.email_field :email, autofocus: true, class: "form-control" %> <%= f.email_field :login, autofocus: true, class: "form-control" %>
</div> </div>
<div class="form-group"> <div class="form-group">
......
...@@ -9,17 +9,3 @@ ...@@ -9,17 +9,3 @@
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br /> <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end -%> <% end -%>
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br />
<% end -%>
<% end -%>
...@@ -14,9 +14,16 @@ ...@@ -14,9 +14,16 @@
</div> </div>
</div> </div>
<div class="header-column"> <div class="header-column">
<div class="header-row"> <div class="row">
<div class="cart-area"> <div class="cart-area">
<div class="cart-dropdown">
<a href="<%= cart_index_url %>" class="cart-dropdown-icon">
<i class="minicart-icon"></i>
<span class="cart-info">
<span class="cart-qty"><%= cart_total_items %></span>
</span>
</a>
</div>
</div> </div>
<div class="header-search"> <div class="header-search">
......
...@@ -14,5 +14,6 @@ ...@@ -14,5 +14,6 @@
<%= yield %> <%= yield %>
</div> </div>
<%= render 'layouts/footer' %> <%= render 'layouts/footer' %>
<%= javascript_include_tag 'theme-main' %>
</body> </body>
</html> </html>
<tr>
<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><%= line_item[:quantity]%></td>
<td><%= number_to_currency(line_item[:product].price * line_item[:quantity]) %></td>
</tr>
\ No newline at end of file
$('.cart-qty').html("<%= render 'carts/cart_quantity' %>");
\ No newline at end of file
<!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 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 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>
<%= @user.email %>
Your order has been created
\ No newline at end of file
<div class="checkout">
<div class="container">
<h1 class="h2 heading-primary mt-lg mb-md clearfix">
Checkout
</h1>
<div class="row">
<div class="col-md-12">
<div class="form-col">
<h3>Your Information</h3>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" value="<%= current_user.email %>" disabled>
</div>
</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' %>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
\ No newline at end of file
...@@ -21,6 +21,14 @@ ...@@ -21,6 +21,14 @@
<span class="product-price"><%= number_to_currency(@product.price) %></span> <span class="product-price"><%= number_to_currency(@product.price) %></span>
</div> </div>
</div> </div>
<div class="product-actions">
<%= form_tag line_items_path(product_id: @product), remote: true do %>
<div class="product-detail-qty">
<%= number_field_tag :product_quantity, 1, min: 0, class: 'vertical-spinner' %>
</div>
<%= submit_tag 'Add to cart', class: 'addtocart' %>
<% end %>
</div>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -14,5 +14,6 @@ module DhpVenshop ...@@ -14,5 +14,6 @@ module DhpVenshop
# Settings in config/environments/* take precedence over those specified here. # Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers # Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded. # -- all .rb files in that directory are automatically loaded.
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
end end
end end
...@@ -51,4 +51,6 @@ Rails.application.configure do ...@@ -51,4 +51,6 @@ Rails.application.configure do
# Use an evented file watcher to asynchronously detect changes in source code, # Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem. # routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.delivery_method = :letter_opener
end end
...@@ -12,3 +12,4 @@ Rails.application.config.assets.paths << Rails.root.join('node_modules') ...@@ -12,3 +12,4 @@ Rails.application.config.assets.paths << Rails.root.join('node_modules')
# application.js, application.css, and all non-JS/CSS in the app/assets # application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added. # folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css ) # Rails.application.config.assets.precompile += %w( admin.js admin.css )
Rails.application.config.assets.precompile += %w( theme-main.js )
...@@ -34,7 +34,7 @@ Devise.setup do |config| ...@@ -34,7 +34,7 @@ Devise.setup do |config|
# session. If you need permissions, you should implement that in a before filter. # session. If you need permissions, you should implement that in a before filter.
# You can also supply a hash where the value is a boolean determining whether # You can also supply a hash where the value is a boolean determining whether
# or not authentication should be aborted when the value is not present. # or not authentication should be aborted when the value is not present.
# config.authentication_keys = [:email] config.authentication_keys = [:login]
# Configure parameters from the request object used for authentication. Each entry # Configure parameters from the request object used for authentication. Each entry
# given should be a request method and it will automatically be passed to the # given should be a request method and it will automatically be passed to the
......
...@@ -3,5 +3,9 @@ Rails.application.routes.draw do ...@@ -3,5 +3,9 @@ Rails.application.routes.draw do
root 'static_pages#index' root 'static_pages#index'
resources :categories resources :categories
resources :products resources :products
resources :orders, only: [:new, :create, :show]
resources :line_items
resources :cart, only: :index, controller: 'carts'
delete '/cart/remove', to: 'carts#destroy', as: 'remove_cart'
devise_for :users devise_for :users
end end
class CreateLineItems < ActiveRecord::Migration[5.1]
def change
create_table :line_items do |t|
t.integer :quantity, default: 1
t.references :product, index: true
t.references :order, index: true
t.timestamps
end
end
end
class CreateOrders < ActiveRecord::Migration[5.1]
def change
create_table :orders do |t|
t.references :user, index: true
t.timestamps
end
end
end
class AddCustomFieldsToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :username, :string
add_column :users, :first_name, :string
add_column :users, :last_name, :string
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,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: 20170608073720) do ActiveRecord::Schema.define(version: 20170613012113) do
create_table "categories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "categories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "title" t.string "title"
...@@ -68,5 +68,4 @@ ActiveRecord::Schema.define(version: 20170608073720) do ...@@ -68,5 +68,4 @@ ActiveRecord::Schema.define(version: 20170608073720) do
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end
add_foreign_key "products", "users"
end end
...@@ -36,9 +36,3 @@ categories = Category.all ...@@ -36,9 +36,3 @@ categories = Category.all
user_id: User.pluck(:id).shuffle[1]) user_id: User.pluck(:id).shuffle[1])
end end
end end
# Create super user
User.create!(email: 'admin@example.com',
password: 'password',
password_confirmation: 'password',
super_admin: true)
\ No newline at end of file
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