Commit ad1fccc0 by Nguyen Quoc Kien

Admin: products, carts

parent ba3ecacb
# 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/
class Admin::CartsController < ApplicationController
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]
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[:success] = "Create product : Error"
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.save
flash[:success] = "Create product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error: Create product"
redirect_to new_admin_product_path
end
end
def update
if params[:product][:price].to_i > 0
@product.update(product_params)
flash[:success] = "Update product : Success"
redirect_to admin_products_path
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
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) }
......
module Admin::CartsHelper
end
module Admin::ProductsHelper
end
module Admin::UsersHelper
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
......@@ -9,7 +9,7 @@ class Product < ActiveRecord::Base
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}
private
......
<div class="col-md-3">
<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><%= 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>
<% end %>
<tr>
<td colspan="3">Total</td>
<td><b><%= number_to_currency(cart.total_price) %></b></td>
</tr>
<tr>
<td colspan="2">Name: </td>
<td colspan="2"> <%= cart.full_name %>
</tr>
<tr>
<td colspan="2">Phone: </td>
<td colspan="2"> <%= cart.phone %>
</tr>
<tr>
<td colspan="2">Address: </td>
<td colspan="2"> <%= cart.address %>
</tr>
<tr>
<td colspan="2">Status: </td>
<td colspan="2"><%= 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><%= 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>
<% end %>
<tr>
<td colspan="3">Total</td>
<td><b><%= number_to_currency(cart_to_user.total_price) %></b></td>
</tr>
<tr>
<td colspan="2">Name: </td>
<td colspan="2"> <%= cart_to_user.full_name %>
</tr>
<tr>
<td colspan="2">Phone: </td>
<td colspan="2"> <%= cart_to_user.phone %>
</tr>
<tr>
<td colspan="2">Address: </td>
<td colspan="2"> <%= cart_to_user.address %>
</tr>
<tr>
<td colspan="2">Status: </td>
<td colspan="2"><%= 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-md-9 col-md-offset-3">
<%= 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 %>
<h2>Your Cart</h2>
<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-md-9 col-md-offset-3">
<%= 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>
......@@ -35,6 +35,9 @@
</li>
</ul>
</li>
<% end %>
<% if admin_signed_in? %>
<%= render 'layouts/header_admin' %>
<% else %>
<%= link_to "Log in", new_user_session_path, class: "btn btn-lg btn-primary", style: "margin: 2px" %>
<% end %>
......
<% if admin_signed_in? %>
<ul class="nav navbar-nav">
<li>
<%= link_to "Admin - products", admin_products_path %>
</li>
<li>
<%= link_to "Admin - Categories" %>
</li>
<li><%= link_to "Admin - Carts", admin_carts_path %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Admin", "" %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <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,9 @@
<%= 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 notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
......
......@@ -5,5 +5,5 @@
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
</p>
<p><%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-lg btn-primary" %></p>
<p><%= link_to "Admin", admins_path, class: "btn btn-lg btn-primary" %></p>
<p><%= link_to "Admin", "#", class: "btn btn-lg btn-primary" %></p>
</header>
Rails.application.routes.draw do
devise_for :admins
devise_for :users
get 'carts/index'
root to: "static_pages#home"
......@@ -11,7 +13,10 @@ Rails.application.routes.draw do
resources :products
resources :carts
resources :cart_products, only: [:create, :destroy]
resources :admins
namespace :admin do
resources :products, :carts
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
......
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
......
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
# 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