Commit b9cc116f by Nguyen Quoc Kien

Merge branch 'fix_review_code' into 'develop'

Fix review code

See merge request !3
parents 43e93f27 a8fbe918
......@@ -36,7 +36,7 @@ gem 'bootstrap-will_paginate', '0.0.10'
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
......
......@@ -81,6 +81,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
kgio (2.9.3)
loofah (2.0.2)
nokogiri (>= 1.5.9)
mail (2.6.3)
......@@ -121,6 +122,7 @@ GEM
activesupport (= 4.2.2)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.15.0)
rake (10.4.2)
rdoc (4.2.0)
responders (2.1.0)
......@@ -152,6 +154,10 @@ GEM
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
unicorn (4.9.0)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
vacuum (1.3.0)
jeff (~> 1.0)
multi_xml (~> 0.5.0)
......@@ -183,6 +189,7 @@ DEPENDENCIES
spring
turbolinks
uglifier (>= 1.3.0)
unicorn
vacuum (~> 1.3.0)
web-console (~> 2.0)
will_paginate (= 3.0.7)
......
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the admin/carts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the admin/products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the admin/users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the shopping_history controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class Admin::CartsController < ApplicationController
before_action :authenticate_admin!
def index
@users = User.all
end
def show
if params[:id] != "buyers"
@users = User.all
@user = User.find(params[:id])
@carts_to_user = Cart.where(user_id: @user.id)
else
@carts = Cart.where(user_id: nil)
end
end
def update
@cart = Cart.find(params[:id])
if(@cart.status == "Checkout")
status = "In process"
else
status = "Finish"
end
@cart.update(status: status)
redirect_to admin_cart_path(id: params[:user_id])
end
end
class Admin::ProductsController < ApplicationController
before_action :find_product, only: [:destroy, :edit,:update]
before_action :get_categories, only: [:edit,:update, :new,:create]
before_action :authenticate_admin!
before_action :check_page, only: [:index]
def index
@products = Product.paginate(page: params[:page]).per_page(50)
@categories = Category.all
end
def destroy
if @product.destroy
flash[:success] = "Delete product : Success"
else
flash[:danger] = "Delete product : Error - Product add to carts"
end
redirect_to admin_products_path
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
flash[:success] = "Create product : Success"
redirect_to admin_products_path
else
render :new
end
end
def update
if @product.update(product_params)
flash[:success] = "Update product : Success"
redirect_to admin_products_path
else
render :edit
end
end
private
def product_params
params.require(:product).permit(:category_id, :name, :price, :image, :description)
end
def get_categories
@categories = Category.all
end
end
class Admin::UsersController < ApplicationController
before_action :authenticate_admin!
before_action :check_page, only: [:index]
def index
@users = User.paginate(page: params[:page]).per_page(21)
end
def destroy
@user = User.find(params[:id])
if @user.destroy
flash[:success] = "Delete User : Success"
else
flash[:danger] = "Delete User : Error"
end
redirect_to admin_users_path
end
end
......@@ -3,19 +3,24 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
protect_from_forgery
before_action :sign_out_all, if: :devise_controller?
before_action :configure_permitted_parameters, if: :devise_controller?
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 sign_out_all
sign_out current_user if current_user
sign_out current_admin if current_admin
end
def configure_permitted_parameters
......@@ -23,4 +28,28 @@ class ApplicationController < ActionController::Base
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end
def check_page
if (params[:page].to_i <= 0)
params[:page] = 1
end
if is_number?(params[:page]) == false
params[:page] = 1
end
end
def is_number? string
true if Float(string) rescue false
end
def find_product
if params[:id].to_i > (Product.count + 1)
redirect_to error_path
elsif is_number?(params[:id]) == false
redirect_to error_path
else
@product = Product.find(params[:id])
end
end
end
class CartProductsController < ApplicationController
before_action :set_cart, only: [:create]
before_action :set_cart, only: [:create, :update]
before_action :check_quantity?, only: [:create]
def create
product = Product.find(params[:product_id])
@cart_product = @cart.add_product(product.id, product.price)
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 }
if check_quantity?
add_product_to_cart(product.id.to_i, params[:quantity].to_i )
redirect_to cart_path(id: @user_id)
flash[:success] = 'Products add to cart'
else
format.html { render action: "new" }
format.json { render json: @cart_product.errors,
status: :unprocessable_entity }
redirect_to products_path
flash[:danger] = 'Errors: Quantity'
end
end
def update
product = Product.find(params[:product_id])
if check_quantity?
update_product_to_cart(product.id.to_i, params[:quantity].to_i )
redirect_to cart_path(id: @user_id)
flash[:success] = 'Update successful'
else
redirect_to cart_path(id: @user_id)
flash[:danger] = 'Errors: Quantity'
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 update_product_to_cart(product_id, number)
session[@user_id].each do |key, value|
if (key == product_id.to_s)
session[@user_id][key] = number
break
end
end
end
def check_quantity?
params[:quantity].to_i > 0 ? true : false
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)
def new
@cart = Cart.new
end
def edit
@cart = Cart.find(session[:cart_id])
def create
total = 0
@cart = Cart.new(cart_params)
@cart.save
@cart.add_user_id_and_status(current_user)
get_user_id()
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
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")
@cart.update( total_price: total)
update_info_user()
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 }
session[@user_id] = nil
flash[:success] = "Email to send"
redirect_to products_path
else
render :new
end
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to products_path,
notice: 'Your cart is currently empty' }
format.json { head :no_content }
session[params[:id]] = nil
redirect_to cart_path(params[:id])
end
def index
if current_user
user_id = current_user.id
else
user_id = 'guess'
end
redirect_to cart_path(user_id)
end
private
def cart_params
params.require(:cart).permit(:full_name, :email, :address, :phone)
end
def find_card
@cart = Cart.find(params[:id])
def get_user_id
if current_user
@user_id = current_user.id
else
@user_id = 'guess'
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
def remove_product_from_cart(product_id)
product_id = product_id.to_s
cart_products_hash.delete(product_id)
def update_info_user
if user_signed_in?
user = User.find(current_user.id)
if user.phone == nil
user.update(phone: params[:cart][:phone])
end
if user.address == nil
user.update(address: params[:cart][:address])
end
def cart_hash
@session['cart']
end
def cart_products_hash
@session['cart']['products']
end
end
class CategoriesController < ApplicationController
before_action :find_category, only: [:show]
before_action :check_page, only: [:show]
def index
@categories = Category.all
end
def show
@categories = Category.all
@category = Category.find(params[:id])
@current_category = @category.id
@products = @category.products.paginate(page: params[:page]).per_page(15)
end
private
def find_category
if params[:id].to_i > (Category.count + 1)
redirect_to error_path
elsif is_number?(params[:id]) == false
redirect_to error_path
else
@category = Category.find(params[:id])
end
end
end
class ProductsController < ApplicationController
before_action :find_product, only: [:show]
before_action :check_page, only: [:index]
def index
@products = Product.paginate(page: params[:page]).per_page(21)
@categories = Category.all
end
# GET /products/:id
def show
@categories = Category.all
end
private
def find_product
@product = Product.find(params[:id])
end
end
class SearchController < ApplicationController
before_action :check_page, only: [:search]
def search
if params[:keyword].nil?
@products = []
else
@products = Product.search(params[:keyword]).paginate(page: params[:page]).per_page(18)
end
end
end
class ShoppingHistoryController < ApplicationController
before_action :authenticate_user!
def index
@user = User.find(current_user.id)
@carts_to_user = Cart.where(user_id: @user.id)
end
end
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
end
module Admin::CartsHelper
end
module Admin::ProductsHelper
end
module Admin::UsersHelper
end
module SearchHelper
end
module ShoppingHistoryHelper
end
......@@ -7,7 +7,7 @@ class OrderNotifier < ApplicationMailer
#
def received(cart)
@cart = cart
mail to: cart.email, subject: 'Pragmatic Store Order Confirmation'
mail to: cart.email, subject: 'Venshop - Order Carts'
end
# Subject can be set in your I18n file at config/locales/en.yml
......
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
attr_accessor :login
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :username, :presence => true, length: { maximum: 50 }, :uniqueness => { :case_sensitive => false }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
before_save :downcase_email
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
private
def downcase_email
self.email = email.downcase
end
end
class Cart < ActiveRecord::Base
has_many :cart_products, dependent: :destroy
VALID_PHONE_REGEX = /\d[0-9]\)*\z/
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
def add_product(product_id, price)
current_item = cart_products.find_by(product_id: product_id)
if current_item
current_item.number += 1
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :phone, presence: true, length: { maximum: 15 },
format: { with: VALID_PHONE_REGEX }
validates :total_price, presence: true, format: { with: VALID_NUMBER_REGEX }
validates :full_name, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 }
before_save :downcase_email
def add_user_id_and_status(current_user)
if current_user
self.update(user_id: current_user.id, status: "Checkout")
else
current_item = cart_products.build(product_id: product_id, price: price)
self.update(user_id: "", status: "Checkout")
end
current_item
end
def total_price
cart_products.to_a.sum { |item| item.total_price }
private
def downcase_email
self.email = email.downcase
end
end
......@@ -2,8 +2,9 @@ class CartProduct < ActiveRecord::Base
belongs_to :product
belongs_to :cart
def total_price
product.price * number
end
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :number, presence: true, format: { with: VALID_NUMBER_REGEX }
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
end
......@@ -4,12 +4,22 @@ class Product < ActiveRecord::Base
belongs_to :category
has_many :cart_products
before_destroy :ensure_not_referenced_by_any_cart_product
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :category_id, presence: true
validates :name, presence: true
validates :image, presence: true, length: { maximum: 1000 }
validates :description, presence: true, length: { maximum: 65535 }
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates_numericality_of :price, presence: true, format: { with: VALID_NUMBER_REGEX }, greater_than: 0
before_destroy :ensure_not_referenced_by_any_cart_product
before_save :convert_data_product
def self.search(keyword)
Product.where("name like ?", "%#{keyword}%" )
end
private
......@@ -21,4 +31,9 @@ class Product < ActiveRecord::Base
return false
end
end
def convert_data_product
self.category_id.to_i
self.price.to_i
end
end
......@@ -7,14 +7,16 @@ class User < ActiveRecord::Base
:registerable, :recoverable,
:rememberable, :trackable, :validatable,
:authentication_keys => [:login]
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save :downcase_email
validates :username, :presence => true, length: { maximum: 50 }, :uniqueness => { :case_sensitive => false }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
before_save :downcase_email
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
......
<div class="col-md-12">
<p class="lead">Admin - Carts</p>
<div id="searchlist" class="list-group">
<%= link_to "Buyers", admin_cart_path(id: "buyers"), :class => "list-group-item "%>
<% @users.each do |user|%>
<%= link_to "#{ user.username }", admin_cart_path(id: user.id), :class => "list-group-item "%>
<% end %>
</div>
</div>
\ No newline at end of file
<% provide(:title, "User Carts") %>
<h2>Your Cart: Buyers</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Product name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total price</th>
</tr>
</thead>
<% @carts.each do |cart| %>
<% cart_products = cart.cart_products %>
<tr>
<td colspan="4"><h4><b>Cart <%= cart.id %>: <%= cart.created_at.to_formatted_s(:long_ordinal) %></b></h4></td>
</tr>
<% cart_products.each do |cart_product| %>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total:</td>
<td><b><%= number_to_currency(cart.total_price/100.00) %></b></td>
</tr>
<tr>
<td>Name: </td>
<td colspan="3"> <%= cart.full_name %>
</tr>
<tr>
<td>E-Mail: </td>
<td colspan="3"> <%= cart.email %>
</tr>
<tr>
<td>Phone: </td>
<td colspan="3"> <%= cart.phone %>
</tr>
<tr>
<td>Address: </td>
<td colspan="3"> <%= cart.address %>
</tr>
<tr>
<td>Status: </td>
<td colspan="2"><%= cart.status %></td>
<td><% if cart.status != "Finish" %>
<%= button_to 'Next',admin_cart_path(id: cart.id, user_id: "buyers"), method: :put , class: "btn btn-primary" %>
<% else %>
<%= link_to 'Finished',"#" , class: "btn btn-danger" %>
<% end %></td>
</tr>
<% end %>
<tr>
<td><%= link_to 'Back', :back, class: "btn btn-info" %></td>
</tr>
</table>
<% provide(:title, "User Carts") %>
<h2>Your Cart: <%= @user.username %>-<%= @user.email %></h2>
<table class="table table-hover">
<thead>
<tr>
<th>Product name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total price</th>
</tr>
</thead>
<% @carts_to_user.each do |cart_to_user| %>
<% cart_products = cart_to_user.cart_products %>
<tr>
<td colspan="4"><h4><b>Cart <%= cart_to_user.id %>: <%= cart_to_user.created_at.to_formatted_s(:long_ordinal) %></b></h4></td>
</tr>
<% cart_products.each do |cart_product| %>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total</td>
<td><b><%= number_to_currency(cart_to_user.total_price/100.00) %></b></td>
</tr>
<tr>
<td>Name: </td>
<td colspan="3"> <%= cart_to_user.full_name %>
</tr>
<tr>
<td>E-Mail: </td>
<td colspan="3"> <%= cart_to_user.email %>
</tr>
<tr>
<td>Phone: </td>
<td colspan="3"> <%= cart_to_user.phone %>
</tr>
<tr>
<td>Address: </td>
<td colspan="3"> <%= cart_to_user.address %>
</tr>
<tr>
<td>Status: </td>
<td colspan="2"><%= cart_to_user.status %></td>
<td>
<% if cart_to_user.status != "Finish" %>
<%= button_to 'Next',admin_cart_path(id: cart_to_user.id, user_id: @user.id), method: :put , class: "btn btn-primary" %>
<% else %>
<%= link_to 'Finished',"#" , class: "btn btn-danger" %>
<% end %>
</td>
</tr>
<% end %>
<tr>
<td><%= link_to 'Back', :back, class: "btn btn-info" %></td>
</tr>
</table>
<% provide(:title, "Admin Carts") %>
<%= render 'admin/carts/list_users' %>
\ No newline at end of file
<% if @user %>
<%= render 'admin/carts/show_to_user_id' %>
<% else %>
<%= render 'admin/carts/show_to_buyers' %>
<% end %>
\ No newline at end of file
<tr>
<td><%= product.id %></td>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= image_tag(product.image, alt: product.name, style: 'height: 50px') %></td>
<td><%= Category.find(product.category_id).name %> </td>
<th><%= link_to "Delete", admin_product_path(id: product.id), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to "Edit", edit_admin_product_path(id: product.id) %>
</th>
</tr>
<% provide(:title, "Edit Products") %>
<h2 style="text-align: center;">Edit products <%= @product.id %></h2>
<div class="col-9">
<%= form_for [:admin, @product] do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %><br/>
</div>
<div class="field">
<%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name, :selected => @product.category_id %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.number_field :price, class: 'form-control' %>
</div>
<div class="field">
<%= f.label :image %>
<%= f.text_field :image, class: 'form-control' %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control', rows: '15' %>
</div>
</br>
<div class="actions">
<%= f.submit "Update", class: "btn btn-primary" %>
</div>
<% end %>
<%= link_to "Back", :back %>
</div>
<% provide(:title, "All Products") %>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<div class="col-lg-12">
<h2 class="text-left">All Products</h2>
<%= will_paginate @products %>
<table class="table table-hover">
<thead>
<tr>
<th colspan="3"><%= link_to "New product", new_admin_product_path, :class => "btn btn-primary"%></th>
</tr>
<tr>
<th>ID Product</th>
<th>Product name</th>
<th>Price</th>
<th>Image</th>
<th>Category</th>
<th>Admins</th>
</tr>
</thead>
<tbody>
<%= render @products %>
</tbody>
</table>
</div>
<%= will_paginate @products %>
<% provide(:title, "New Products") %>
<h2 style="text-align: center;">New products </h2>
<div class="col-9 ">
<%= form_for [:admin, @product] do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %><br/>
</div>
<div class="field">
<%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.number_field :price, class: 'form-control' %>
</div>
<div class="field">
<%= f.label :image %>
<%= f.text_field :image, class: 'form-control' %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control', rows: '15' %>
</div>
</br>
<div class="actions">
<%= f.submit "New product", class: "btn btn-primary" %>
</div>
<% end %>
<%= link_to "Back", :back %>
</div>
<% provide(:title, "All Users") %>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<div class="col-lg-12">
<h2 class="text-left">All Users</h2>
<%= will_paginate @users %>
<table class="table table-hover">
<thead>
<tr>
<th>ID User</th>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= user.phone %></td>
<td><%= user.address %> </td>
<td><%= link_to "Delete", admin_user_path(id: user.id), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @users %>
<% provide(:title, "Order") %>
<div class="row">
<% if @cart.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@cart.errors.count, "error") %>.
</div>
<ul>
<% @cart.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<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' %>
......@@ -14,7 +25,7 @@
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
<%= f.number_field :phone, class: 'form-control' %>
<br>
<%= f.submit "Save changes", class: "btn btn-primary" %>
......
<% 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>
<th>Price</th>
<th>Quantity</th>
<th>Update</th>
<th>Total price</th>
<th>Delete</th>
</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>
<%= form_tag cart_product_path, method: :PATCH do %>
<%= hidden_field_tag :product_id, key %>
<td><%= number_field_tag :quantity, "#{value}", class: 'form-control', :style => "width: 70px"%></td>
<td><%= submit_tag "Update", :class => "btn btn-primary", :style => "width: 70px" %></td>
<% end %>
<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 colspan="4">Total</td>
<td> <%= number_to_currency(total) %></td>
</tr>
<tr>
<td><%= link_to 'Back', products_path, class: "btn btn-danger" %></td>
<td colspan="3"><%= link_to 'Back', products_path, class: "btn btn-danger" %></td>
<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>
<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>
</tr>
</tbody>
<% else %>
<h1>Cart Empty</h1>
<% end %>
</table>
\ No newline at end of file
......@@ -6,7 +6,6 @@
<%= f.label :login %><br />
<%= f.text_field :login, autofocus: true, class: 'form-control' %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
......@@ -17,13 +16,9 @@
<% end %>
<% end -%>
</div>
<div class="actions">
<%= f.submit "Log in", class: "btn btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
\ No newline at end of file
......@@ -17,10 +17,11 @@
<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? %>
<li><%= link_to "Shopping history", shopping_history_index_path %></li>
<li><%= link_to "Users", "" %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
......@@ -35,14 +36,14 @@
</li>
</ul>
</li>
<% else %>
<% end %>
<% if admin_signed_in? %>
<%= render 'layouts/header_admin' %>
<% end %>
<% if !admin_signed_in? && !user_signed_in? %>
<%= link_to "Log in", new_user_session_path, class: "btn btn-lg btn-primary", style: "margin: 2px" %>
<% end %>
</ul>
<%= form_for("#", html: { class: 'navbar-form navbar-left', role: 'search' }) do |f| %>
<%= f.text_field :search, class: 'form-group form-control', placeholder: 'Search' %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
</div>
</nav>
<% if admin_signed_in? %>
<ul class="nav navbar-nav">
<li>
<%= link_to "Admin - products", admin_products_path %>
</li>
<li>
<%= link_to "Admin - Users", admin_users_path %>
</li>
<li><%= link_to "Admin - Carts", admin_carts_path %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Admin <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Edit my user", edit_admin_registration_path %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", destroy_admin_session_path, method: "delete" %>
</li>
</ul>
</li>
</ul>
<% end %>
\ No newline at end of file
......@@ -11,6 +11,21 @@
<%= render 'layouts/header' %>
<div class="container">
<div class="row">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<% if !admin_signed_in? %>
<%= form_tag search_path, method: :get do %>
<div class="row">
<div class="col-md-10">
<%= text_field_tag :keyword, nil, class: 'form-control', placeholder: 'Search' %>
</div>
<div class="col-md-2">
<%= submit_tag "Search", :class => "btn btn-primary" %>
</div>
</div>
<% end %>
<% end %>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
......
<h1>OrderNotifier#received</h1>
<p>
<%= @greeting %>, find me in app/views/order_notifier/received.html.erb
</p>
<h1>Venshop - Email confirmation</h1>
<h2>Your Cart</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Product name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total price</th>
</tr>
</thead>
<tbody>
<% cart_products = @cart.cart_products %>
<% cart_products.each do |cart_product| %>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %>
<td><%= number_to_currency(cart_product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</td>
<% end %>
<tr>
<td colspan="3">Total</td>
<td> <%= number_to_currency(@cart.total_price/100.00) %></td>
</tr>
</tbody>
</table>
\ No newline at end of file
<h1>OrderNotifier#shipped</h1>
<p>
<%= @greeting %>, find me in app/views/order_notifier/shipped.html.erb
<%= @greeting %>
</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>
......@@ -6,13 +6,19 @@
<%= image_tag(@product.image, alt: @product.name, class: "img-responsive") %><br>
<hr>
<div class="caption-full">
<h3 class="pull-right">
<b>Price: $</b><%= (@product.price/100.00) %>
<%= button_to "Add to cart", cart_products_path(product_id: @product.id), :class => "btn btn-primary" %>
</h3>
<br/>
<h4><b><%= @product.name %></b></h4>
<%= simple_format(@product.description) %>
<p><%= simple_format(@product.description) %></p>
<h3>
<b>Price: $</b><%= (@product.price/100.00) %>
<%= form_tag cart_products_path do %>
<p>
<%= hidden_field_tag :product_id, @product.id %>
Quantity: <%= number_field_tag :quantity, "1", class: 'form-control', :style => "width: 100px;" %><br/>
<%= submit_tag "Add To cart", :class => "btn btn-primary", :style => "width: 100px" %>
</p>
<% end %>
</h3>
</div>
</div>
</div>
......
<% provide(:title, "Search Products") %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %></h2>
<%= will_paginate @products %>
<div class="col-md-12" style="text-align: center;">
<% if @products!= [] %>
<% @products.each do |product| %>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<%= image_tag(product.image, alt: product.name, style: 'height: 300px') %>
<div class="caption">
<div class="div_product_name" style="width: 235px; height: 53px;">
<h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3>
</div>
<p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p>
<%= 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>
<% end %>
<% else %>
<h1> Not found</h1>
<% end %>
</div>
<div><%= will_paginate @products %></div>
<% if @product.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@product.errors.count, "error") %>.
</div>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
\ No newline at end of file
<% provide(:title, "Shopping History") %>
<h2>Shopping History: <%= @user.username %>-<%= @user.email %></h2>
<table class="table table-hover">
<thead>
<tr>
<th>Product name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total price</th>
</tr>
</thead>
<% @carts_to_user.each do |cart_to_user| %>
<% cart_products = cart_to_user.cart_products %>
<tr>
<td colspan="4"><h4><b>Cart: <%= cart_to_user.created_at %></b></h4></td>
</tr>
<% cart_products.each do |cart_product| %>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total:</td>
<td><b><%= number_to_currency(cart_to_user.total_price/100.00) %></b></td>
</tr>
<tr>
<td>Name: </td>
<td colspan="3"> <%= cart_to_user.full_name %>
</tr>
<tr>
<td>E-Mail: </td>
<td colspan="3"> <%= cart_to_user.email %>
</tr>
<tr>
<td>Phone: </td>
<td colspan="3"> <%= cart_to_user.phone %>
</tr>
<tr>
<td>Address: </td>
<td colspan="3"> <%= cart_to_user.address %>
</tr>
<tr>
<td>Status: </td>
<td colspan="3"><%= cart_to_user.status %></td>
</tr>
<% end %>
<tr>
<td><%= link_to 'Back', :back, class: "btn btn-info" %></td>
</tr>
</table>
<h1>Pages not found!</h1>
\ No newline at end of file
<% provide(:title, "Home") %>
<br/>
<header class="jumbotron hero-spacer">
<h1>Home - AMAZON PRODUCT API</h1>
<h1>Home - AZIGExN VeNtura</h1>
<p>
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
Home - AMAZON PRODUCT API Home - AZIGExN VeNtura - ABring Innovation from Vietnam to the World!
</p>
<p><%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-lg btn-primary" %></p>
</header>
Rails.application.routes.draw do
devise_for :admins
devise_for :users
get 'carts/index'
get 'search' => 'search#search'
devise_for :users
root to: "static_pages#home"
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'error' => 'static_pages#error'
resources :categories, only: [:index, :show]
resources :products
resources :carts
resources :cart_products, only: [:create, :destroy]
resources :cart_products, only: [:create, :destroy, :update]
resources :shopping_history, only: [:index]
namespace :admin do
resources :products, :carts, :users
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
......
......@@ -3,9 +3,9 @@ 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 :phone, limit: 15
t.string :email
t.text :address
......
class AddPhoneToUsers < ActiveRecord::Migration
def change
add_column :users, :phone, :string
add_column :users, :phone, :string, limit: 15
end
end
class DeviseCreateAdmins < ActiveRecord::Migration
def change
create_table(:admins) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :admins, :email, unique: true
add_index :admins, :reset_password_token, unique: true
# add_index :admins, :confirmation_token, unique: true
# add_index :admins, :unlock_token, unique: true
end
end
class AddUsernameToAdmins < ActiveRecord::Migration
def change
add_column :admins, :username, :string
add_index :admins, :username, unique: true
end
end
......@@ -11,7 +11,27 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150727094623) do
ActiveRecord::Schema.define(version: 20150730023057) do
create_table "admins", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username", limit: 255
end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree
add_index "admins", ["username"], name: "index_admins_on_username", unique: true, using: :btree
create_table "cart_products", force: :cascade do |t|
t.integer "cart_id", limit: 4, null: false
......@@ -25,9 +45,9 @@ 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 "phone", limit: 15
t.string "email", limit: 255
t.text "address", limit: 65535
t.datetime "created_at", null: false
......@@ -68,7 +88,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username", limit: 255
t.string "phone", limit: 255
t.string "phone", limit: 15
t.string "address", limit: 255
end
......
require 'test_helper'
class Admin::CartsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class Admin::ProductsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class Admin::UsersControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class SearchControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class ShoppingHistoryControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
require 'test_helper'
class AdminTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
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