Commit ab64a07d by Nguyen Quoc Kien

Merge branch 'admin' into 'master'

Feature Admin

See merge request !1
parents ba3ecacb 8912f75e
# 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 :authenticate_admin!
def index
@products = Product.paginate(page: params[:page]).per_page(21)
@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
@categories = Category.all
end
def edit
@categories = Category.all
end
def create
@product = Product.new(name: params[:product][:name],
category_id: params[:product][:category_id].to_i,
price: params[:product][:price].to_i,
image: params[:product][:image],
description: params[:product][:description])
if @product.check_valid()
@product.save
flash[:success] = "Create product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error"
redirect_to new_admin_product_path
end
end
def update
if params[:product][:price].to_i > 0
if@product.check_valid()
@product.update(product_params)
flash[:success] = "Update product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error"
redirect_to edit_admin_product_path(id: params[:id])
end
else
flash[:danger] = "Error: Price"
redirect_to edit_admin_product_path(id: params[:id])
end
end
private
def find_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:category_id, :name, :price, :image, :description)
end
end
class Admin::UsersController < ApplicationController
before_action :authenticate_admin!
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,6 +3,7 @@ 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
......@@ -17,6 +18,11 @@ class ApplicationController < ActionController::Base
@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
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
......
class CartProductsController < ApplicationController
before_action :set_cart, only: [:create]
before_action :check_quantity?, only: [:create]
def create
product = Product.find(params[:product_id])
if check_quantity?
add_product_to_cart(product.id.to_i, params[:quantity].to_i )
respond_to do |format|
format.html { redirect_to cart_path(id: @user_id),
notice: 'Products add to cart' }
format.json { head :no_content }
end
redirect_to cart_path(id: @user_id)
flash[:success] = 'Products add to cart'
else
respond_to do |format|
format.html { redirect_to cart_path(id: @user_id),
notice: 'Errors: Quantity' }
format.json { head :no_content }
end
redirect_to cart_path(id: @user_id)
flash[:success] = 'Errors: Quantity'
end
end
......@@ -43,9 +38,11 @@ class CartProductsController < ApplicationController
end
def check_quantity?
if ( params[:quantity].to_i && params[:quantity].to_i > 0 )
if params[:quantity].to_i > 0
return true
else false
else
return false
end
end
end
class CartsController < ApplicationController
#before_action :find_card, only: [ :create ]
#before_action :check_phone, only: [ :create ]
def update
end
def new
@cart = Cart.new
end
def create
total = 0
@cart = Cart.new(cart_params)
@cart.save
if current_user
......@@ -29,19 +22,14 @@ class CartsController < ApplicationController
total += @product.price * value.to_f
end
@cart.update( total_price: total)
update_info_user()
OrderNotifier.received(@cart).deliver
respond_to do |format|
format.html { redirect_to products_path,
notice: 'Email to send' }
format.json { head :no_content }
end
session[user_id] = nil
flash[:success] = "Email to send"
redirect_to products_path
else
respond_to do |format|
format.html { redirect_to carts_path,
notice: 'Errors' }
format.json { head :no_content }
end
flash[:danger] = "Error"
redirect_to carts_path
end
end
......@@ -59,20 +47,22 @@ class CartsController < ApplicationController
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 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
end
def check_phone
params[:phone].is_a?
end
end
......@@ -6,14 +6,10 @@ class ProductsController < ApplicationController
@categories = Category.all
end
# GET /products/:id
def show
@categories = Category.all
end
def new
end
private
def find_product
......
class SearchController < ApplicationController
def search
if params[:keyword].nil?
@products = []
else
@products = Product.where("name like '%#{params[:keyword]}%'").paginate(page: params[:page]).per_page(18)
end
end
end
class ShoppingHistoryController < ApplicationController
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
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
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 }
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/
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
private
def downcase_email
self.email = email.downcase
end
def check_phone
if self.phone.is_a
return true
else
return false
end
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
......@@ -6,10 +6,24 @@ class Product < ActiveRecord::Base
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 :image, presence: true, length: { maximum: 1000 }
validates :description, presence: true, length: { maximum: 65535 }
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
def check_valid
if self.price < 0
return false
end
if self.description == nil
return false
end
if self.image == nil
return false
end
end
private
......
......@@ -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.created_at %></b></h4></td>
</tr>
<% cart_products.each do |cart_product| %>
<tbody>
<% 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="3"><%= cart.status %></td>
</tr>
<tr>
<% if cart.status != "Finish" %>
<td colspan="4" style="text-align:right"><%= button_to 'Next',admin_cart_path(id: cart.id, user_id: "buyers"), method: :put , class: "btn btn-primary" %></td>
<% else %>
<td colspan="4" style="text-align:right"><%= link_to 'Finished',"#" , class: "btn btn-danger" %></td>
<% end %>
</tr>
<% end %>
<tr>
<td><%= link_to 'Back', :back, class: "btn btn-info" %></td>
</tr>
</tbody>
</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.created_at %></b></h4></td>
</tr>
<% cart_products.each do |cart_product| %>
<tbody>
<% 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>
<tr>
<% if cart_to_user.status != "Finish" %>
<td colspan="4" style="text-align:right"><%= button_to 'Next',admin_cart_path(id: cart_to_user.id, user_id: @user.id), method: :put , class: "btn btn-primary" %></td>
<% else %>
<td colspan="4" style="text-align:right"><%= link_to 'Finished',"#" , class: "btn btn-danger" %></td>
<% end %>
</tr>
<% end %>
<tr>
<td><%= link_to 'Back', :back, class: "btn btn-info" %></td>
</tr>
</tbody>
</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| %>
<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| %>
<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 %>
......@@ -14,7 +14,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" %>
......
......@@ -21,6 +21,7 @@
</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 %>
......
......@@ -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>
<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>
<%= will_paginate @products %>
\ 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| %>
<tbody>
<% 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>
</tbody>
</table>
<% 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>
<p><%= link_to "Admin", admins_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'
root to: "static_pages#home"
......@@ -11,7 +14,11 @@ Rails.application.routes.draw do
resources :products
resources :carts
resources :cart_products, only: [:create, :destroy]
resources :admins
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".
......
......@@ -5,7 +5,7 @@ class CreateCarts < ActiveRecord::Migration
t.decimal :total_price, :default => 0
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
......@@ -27,7 +47,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
t.decimal "total_price", precision: 10, default: 0
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