Commit d38e9224 by Dao Minh Nhut

solr search, insert, update, delete

parent 9452905f
source 'https://rubygems.org' source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3' gem 'rails', '4.2.3'
# Use mysql as the database for Active Record # Use mysql as the database for Active Record
...@@ -22,25 +20,29 @@ gem 'turbolinks' ...@@ -22,25 +20,29 @@ gem 'turbolinks'
gem 'jbuilder', '~> 2.0' gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api. # bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password # Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7' # gem 'bcrypt', '~> 3.1.7'
gem 'will_paginate' gem 'will_paginate'
# Use Unicorn as the app server # Use Unicorn as the app server
# gem 'unicorn' gem 'unicorn'
gem 'bootstrap-will_paginate' gem 'bootstrap-will_paginate'
# Use Capistrano for deployment # Use Capistrano for deployment
# gem 'capistrano-rails', group: :development # gem 'capistrano-rails', group: :development
gem 'bcrypt-ruby' gem 'bcrypt-ruby'
gem 'rsolr'
gem 'faker' gem 'faker'
gem 'solr-ruby'
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug' gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views # Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0' gem 'web-console', '~> 2.0'
# 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'
......
...@@ -87,6 +87,7 @@ GEM ...@@ -87,6 +87,7 @@ GEM
railties (>= 4.2.0) railties (>= 4.2.0)
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (1.8.3) json (1.8.3)
kgio (2.9.3)
loofah (2.0.2) loofah (2.0.2)
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)
mail (2.6.3) mail (2.6.3)
...@@ -127,10 +128,13 @@ GEM ...@@ -127,10 +128,13 @@ GEM
activesupport (= 4.2.3) activesupport (= 4.2.3)
rake (>= 0.8.7) rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0) thor (>= 0.18.1, < 2.0)
raindrops (0.15.0)
rake (10.4.2) rake (10.4.2)
rdoc (4.2.0) rdoc (4.2.0)
responders (2.1.0) responders (2.1.0)
railties (>= 4.2.0, < 5) railties (>= 4.2.0, < 5)
rsolr (1.0.12)
builder (>= 2.1.2)
sass (3.4.16) sass (3.4.16)
sass-rails (5.0.3) sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
...@@ -141,6 +145,7 @@ GEM ...@@ -141,6 +145,7 @@ GEM
sdoc (0.4.1) sdoc (0.4.1)
json (~> 1.7, >= 1.7.7) json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0) rdoc (~> 4.0)
solr-ruby (0.0.8)
spring (1.3.6) spring (1.3.6)
sprockets (3.2.0) sprockets (3.2.0)
rack (~> 1.0) rack (~> 1.0)
...@@ -158,6 +163,10 @@ GEM ...@@ -158,6 +163,10 @@ GEM
uglifier (2.7.1) uglifier (2.7.1)
execjs (>= 0.3.0) execjs (>= 0.3.0)
json (>= 1.8.0) json (>= 1.8.0)
unicorn (4.9.0)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
vacuum (1.3.0) vacuum (1.3.0)
jeff (~> 1.0) jeff (~> 1.0)
multi_xml (~> 0.5.0) multi_xml (~> 0.5.0)
...@@ -185,11 +194,17 @@ DEPENDENCIES ...@@ -185,11 +194,17 @@ DEPENDENCIES
jquery-rails jquery-rails
mysql2 mysql2
rails (= 4.2.3) rails (= 4.2.3)
rsolr
sass-rails (~> 5.0) sass-rails (~> 5.0)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
solr-ruby
spring spring
turbolinks turbolinks
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
unicorn
vacuum vacuum
web-console (~> 2.0) web-console (~> 2.0)
will_paginate will_paginate
BUNDLED WITH
1.10.5
# 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 search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
...@@ -6,7 +6,6 @@ class Admins::CartsController < ApplicationController ...@@ -6,7 +6,6 @@ class Admins::CartsController < ApplicationController
if admin_signed_in? && !params[:cart][:status].nil? && !params[:cart][:status].empty? if admin_signed_in? && !params[:cart][:status].nil? && !params[:cart][:status].empty?
@cart = Cart.find(params[:id]) @cart = Cart.find(params[:id])
if @cart.status == params[:cart][:status] if @cart.status == params[:cart][:status]
flash[:danger] = "Notthing change!"
redirect_to :back redirect_to :back
else else
@cart.status = params[:cart][:status] @cart.status = params[:cart][:status]
...@@ -15,7 +14,6 @@ class Admins::CartsController < ApplicationController ...@@ -15,7 +14,6 @@ class Admins::CartsController < ApplicationController
redirect_to :back redirect_to :back
end end
else else
flash[:danger] = "Notthing change!"
redirect_to action: :index redirect_to action: :index
end end
end end
......
class Admins::ProductsController < ApplicationController class Admins::ProductsController < ApplicationController
before_action :init, only: [ :create, :edit, :destroy ]
def init
@solr = RSolr.connect :url => Rails.configuration.solr_host.to_s
end
def insert def insert
@product = Product.new @product = Product.new
@categories = Category.all @categories = Category.all
...@@ -15,6 +22,12 @@ class Admins::ProductsController < ApplicationController ...@@ -15,6 +22,12 @@ class Admins::ProductsController < ApplicationController
price: params[:product][:price].to_i, image: params[:product][:image], price: params[:product][:price].to_i, image: params[:product][:image],
description: params[:product][:description]) description: params[:product][:description])
if @product.save if @product.save
item = [{:id => @product.id, :price => @product.price, :name => @product.name,
:category_id => @product.category_id, :price => @product.price,
:image => @product.image, :description => @product.description}]
@solr.add item
@solr.commit
@solr.optimize
flash[:success] = "Create product : Success" flash[:success] = "Create product : Success"
redirect_to admins_product_path(id: "1") redirect_to admins_product_path(id: "1")
else else
...@@ -28,25 +41,33 @@ class Admins::ProductsController < ApplicationController ...@@ -28,25 +41,33 @@ class Admins::ProductsController < ApplicationController
@product = Product.find(params[:id]) @product = Product.find(params[:id])
@product.update(product_params) @product.update(product_params)
if @product.save if @product.save
flash[:success] = "Update product : Success"
redirect_to admins_product_path(id: "1") %x{curl 'localhost:8080/solr/core0/update?commit=true' -H 'Content-type:application/json' -d '
[{
"id":"#{params[:id]}",
"name":{"set":"#{params[:product][:name]}"}
}]'}
flash[:success] = "Update product : Success"
redirect_to admins_product_path(id: "1")
else else
flash[:danger] = "Error: Price" flash[:danger] = "Error"
render :update render :update
end end
end end
def delete def destroy
@product = Product.all
@product = Product.find(params[:id]) @product = Product.find(params[:id])
@product.destroy @product.destroy
redirect_to :back @solr.delete_by_query "id:#{params[:id]}"
@solr.commit
@solr.optimize
redirect_to :back
end end
def show def show
@products = Product.paginate(page: params[:page]) @products = Product.paginate(page: params[:page])
end
def cart
end end
def product_params def product_params
......
class Admin::UsersController < ApplicationController class Admins::UsersController < ApplicationController
before_action :authenticate_admin!
def show def show
@users = User.all @users = User.paginate(page: params[:page]).per_page(25)
end end
end def delete
\ No newline at end of file @user = User.find(params[:id])
@user.destroy
redirect_to :back
end
end
require 'solr'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery
before_action :sign_out_all, if: :devise_controller?
before_action :call_category , only: [:show, :index] before_action :call_category , only: [:show, :index]
before_action :configure_permitted_parameters, if: :devise_controller? before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
...@@ -7,7 +10,15 @@ class ApplicationController < ActionController::Base ...@@ -7,7 +10,15 @@ class ApplicationController < ActionController::Base
include CartsHelper include CartsHelper
include CategoriesHelper include CategoriesHelper
def sign_out_all
sign_out current_user if current_user
sign_out current_admin if current_admin
end
def configure_permitted_parameters def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username #devise_parameter_sanitizer.for(:sign_up) << :username
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:fullname, :email, :password, :password_confirmation, :phone, :address) }
#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 end
end end
...@@ -6,23 +6,26 @@ class CartProductsController < ApplicationController ...@@ -6,23 +6,26 @@ class CartProductsController < ApplicationController
if params[:id].nil? || params[:quantity].nil? if params[:id].nil? || params[:quantity].nil?
flash[:danger] = "Product not found!" flash[:danger] = "Product not found!"
redirect_to :action => :index redirect_to :action => :index
end else
if params[:quantity].to_i > 0 if params[:quantity].to_i > 0
id = params[:id] id = params[:id]
if session[:cart] then if session[:cart] then
cart_product = session[:cart]
else
session[:cart] ={}
cart_product = session[:cart] cart_product = session[:cart]
else end
session[:cart] ={}
cart_product = session[:cart]
end
if cart_product[id] then if cart_product[id] then
cart_product[id] = cart_product[id] + params[:quantity].to_i cart_product[id] = cart_product[id] + params[:quantity].to_i
else
cart_product[id] = params[:quantity].to_i
end
flash[:success] = "success"
redirect_to :action => :index
else else
cart_product[id] = params[:quantity].to_i redirect_to product_path
end end
flash[:success] = "success"
redirect_to :action => :index
end end
end end
...@@ -35,9 +38,11 @@ class CartProductsController < ApplicationController ...@@ -35,9 +38,11 @@ class CartProductsController < ApplicationController
if params[:new_quantity].nil? || params[:id].nil? if params[:new_quantity].nil? || params[:id].nil?
flash[:danger] = "Product not found!" flash[:danger] = "Product not found!"
else else
session[:cart][params[:id].to_s] = params[:new_quantity].to_i if params[:quantity].to_i > 0
session[:cart][params[:id].to_s] = params[:new_quantity].to_i
end
end end
redirect_to :action => :index redirect_to :action => :index
end end
def clear def clear
session[:cart] = nil session[:cart] = nil
......
...@@ -7,6 +7,7 @@ class CartsController < ApplicationController ...@@ -7,6 +7,7 @@ class CartsController < ApplicationController
end end
def info def info
@new_cart = Cart.new
if user_signed_in? if user_signed_in?
@current_user @current_user
end end
...@@ -14,17 +15,13 @@ class CartsController < ApplicationController ...@@ -14,17 +15,13 @@ class CartsController < ApplicationController
def create def create
if !session[:cart].nil? if !session[:cart].nil?
new_cart = Cart.new(cart_params) @new_cart = Cart.new(fullname: params[:cart][:fullname], email: params[:cart][:email],
new_cart.total_price = calculate_total_price address: params[:cart][:address], phone: params[:cart][:phone],
new_cart.status = "new cart" total_price: calculate_total_price, status: "new cart", user_id: cart_user_id)
if user_signed_in? if @new_cart.save
new_cart.user_id = current_user.id
end
new_cart.save
if !new_cart.id.nil?
session[:cart].each do |id, quantity| session[:cart].each do |id, quantity|
cart_product = CartProduct.new cart_product = CartProduct.new
cart_product.cart_id = new_cart.id cart_product.cart_id = @new_cart.id
cart_product.product_id = id cart_product.product_id = id
cart_product.price = Product.find_by_id(id).price cart_product.price = Product.find_by_id(id).price
cart_product.quantity = quantity cart_product.quantity = quantity
...@@ -33,14 +30,16 @@ class CartsController < ApplicationController ...@@ -33,14 +30,16 @@ class CartsController < ApplicationController
flash[:success] = "Success!" flash[:success] = "Success!"
Emailer.send_email_to(cart_params[:email].to_s,session[:cart]).deliver Emailer.send_email_to(cart_params[:email].to_s,session[:cart]).deliver
session[:cart] = nil session[:cart] = nil
redirect_to cart_path
else
render :info
end end
end end
flash[:danger] = "Wrong input please input again!" flash[:danger] = "Wrong input please input again!"
render :info
end end
def cart_params def cart_params
params.require(:session).permit(:fullname, :email, :address, :phone) params.require(:cart).permit(:fullname, :email, :address, :phone)
end end
private private
...@@ -49,12 +48,20 @@ class CartsController < ApplicationController ...@@ -49,12 +48,20 @@ class CartsController < ApplicationController
total = 0 total = 0
if !session[:cart].nil? if !session[:cart].nil?
session[:cart].each do |id, quantity| session[:cart].each do |id, quantity|
product = Product.find_by_id(id) product = Product.find_by_id(id)
if !product.nil? if !product.nil?
total = total + product.price * quantity total = total + product.price * quantity
end
end end
end end
end
total total
end end
def cart_user_id
user_id = ""
if user_signed_in?
user_id = current_user.id
end
user_id
end
end end
class CategoriesController < ApplicationController class CategoriesController < ApplicationController
include CategoriesHelper include CategoriesHelper
def show def show
category = Category.find(params[:id]) begin
@products = category.products category = Category.find(params[:id])
@products = category.products
rescue
redirect_to root_path
end
end
def index
redirect_to root_path
end end
end end
\ No newline at end of file
...@@ -2,7 +2,11 @@ class ProductsController < ApplicationController ...@@ -2,7 +2,11 @@ class ProductsController < ApplicationController
include CategoriesHelper include CategoriesHelper
include CartProductsHelper include CartProductsHelper
def index def index
@products = Product.paginate(page: params[:page]) begin
@products = Product.paginate(page: params[:page])
rescue
@products = Product.paginate(page: "1")
end
end end
def show def show
begin begin
......
class SearchController < ApplicationController
#before_action :check_page, only: [:search]
def show
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on)
check_params(params["search_params"])
query = "((name:#{@keyword}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query})
begin
results = @solr.send(select_obj).data['response']['docs']
@products = Array.new
results.each do |result|
id = result.to_h['id']
product = Product.find(id)
@products.insert(-1,product)
end
rescue Exception => e
redirect_to root_path
end
end
def check_params(search_params)
check = ["`", "~", "!", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", ":",'\\']
@keyword = ""
search_params.each_char do |key|
check.each do |ch|
key = "\\#{ch}" if key == ch
end
@keyword += key
end
end
end
module ProductsHelper module ProductsHelper
def call_product
@products = Product.all
end
end end
module SearchHelper
end
...@@ -2,11 +2,10 @@ class Cart < ActiveRecord::Base ...@@ -2,11 +2,10 @@ class Cart < ActiveRecord::Base
has_many :cart_product has_many :cart_product
VALID_PHONE_REGEX = /\d[0-9]\)*\z/ VALID_PHONE_REGEX = /\d[0-9]\)*\z/
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i 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 :email, presence: true, length: { maximum: 100 }, format: { with: VALID_EMAIL_REGEX }
validates :phone, presence: true, length: { maximum: 15 }, format: { with: VALID_PHONE_REGEX } validates :phone, presence: true, length: { maximum: 15 }, format: { with: VALID_PHONE_REGEX }
validates :total_price, presence: true, format: { with: VALID_NUMBER_REGEX } validates :total_price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 99999999 }
validates :fullname, presence: true, length: { maximum: 50 } validates :fullname, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 } validates :address, presence: true, length: { maximum: 1000 }
end end
class Category < ActiveRecord::Base class Category < ActiveRecord::Base
has_many :products has_many :products
def new
@category = Category.new
end
end end
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
belongs_to :category belongs_to :category
has_many :cart_products has_many :cart_products
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :category_id, presence: true validates :category_id, presence: true
validates :name, :image, presence: true, length: { maximum: 1000 } validates :name, :image, presence: true, length: { maximum: 1000 }
validates :description, length: { maximum: 65535 } validates :description, length: { maximum: 65535 }
validates :price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 9999 } validates :price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 99999999 }
end end
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
<td><%= f.text_field :status, value: cart.status.to_s, size: 10 , class: "create_input" %> <td><%= f.text_field :status, value: cart.status.to_s, size: 10 , class: "create_input" %>
</td> </td>
<td> <td>
<%= f.submit "Submit" , class: "btn btn-primary" %> <%= f.submit "Update" %>
</td> </td>
<% end %> <% end %>
</tr> </tr>
......
<h2>New products </h2> <% if admin_signed_in? %>
<% if @product.errors.any? %> <h2>New products </h2>
<div id="error_explanation"> <% if @product.errors.any? %>
<div class="alert alert-danger"> <div id="error_explanation">
The form contains <%= pluralize(@product.errors.count, "error") %>. <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> </div>
<ul> <% end %>
<% @product.errors.full_messages.each do |msg| %> <div>
<li><%= msg %></li> <%= form_for(@product, url: {action: "create"}) do |f| %>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form_for(@product, url: {action: "create"}) do |f| %>
<%= f.label :name %><br /> <%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %> <%= f.text_field :name, autofocus: true %>
<%= f.label :category_id %> <%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name %> <%= f.collection_select :category_id, @categories, :id, :name %>
<%= f.label :price %> <%= f.label :price %>
<%= f.number_field :price, min: "1", max: "100" %> <%= f.number_field :price, min: "1", max: "100" %>
<%= f.label :image %> <%= f.label :image %>
<%= f.text_field :image %> <%= f.text_field :image %>
<%= f.label :description %> <%= f.label :description %>
<%= f.text_area :description, rows: '15' %> <%= f.text_area :description, rows: '15' %>
<%= f.submit "New product", class: "btn btn-primary" %> <%= f.submit "New product", class: "btn btn-primary" %>
<% end %> <% end %>
</div> </div>
<% end %>
\ No newline at end of file
<% if admin_signed_in? %> <% if admin_signed_in? %>
<div class="span12"> <div class="span12">
<h2>Products</h2> <h2>Products</h2>
<%= link_to "Insert Product", insert_product_path %>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
<thead> <thead>
<tr> <tr>
<td></td>
<td></td>
<td></td>
<th><h4><%= link_to "Insert", insert_product_path %></h4></th>
</tr>
<tr>
<th>Product Name</th> <th>Product Name</th>
<th>Image</th>
<th>Edit</th> <th>Edit</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
...@@ -14,12 +20,15 @@ ...@@ -14,12 +20,15 @@
<% @products.each do |product| %> <% @products.each do |product| %>
<tr> <tr>
<td><%= product.name %></td> <td><%= product.name %></td>
<td><%= image_tag product.image, height: '60', width: '60' %></td>
<td><%= link_to "Edit", "/update_product/#{product.id}" %></td> <td><%= link_to "Edit", "/update_product/#{product.id}" %></td>
<td><%= link_to "Delete", "/delete_product/#{product.id}" %></td> <td><%= link_to "Delete", "/delete_product/#{product.id}", data: { confirm: 'Are you sure?' } %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
</div> <div align="center">
<%= will_paginate @products%> <%= will_paginate @products%>
</div>
</div>
<% end %> <% end %>
<h2>Edit products</h2> <% if admin_signed_in? %>
<% if @product.errors.any? %> <h2>Edit products</h2>
<div id="error_explanation"> <% if @product.errors.any? %>
<div class="alert alert-danger"> <div id="error_explanation">
The form contains <%= pluralize(@product.errors.count, "error") %>. <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> </div>
<ul> <% end %>
<% @product.errors.full_messages.each do |msg| %> <div>
<li><%= msg %></li> <%= form_for(@product, url: {action: "edit"}) do |f| %>
<% end %> <%= f.label :name %><br />
</ul> <%= f.text_field :name, autofocus: true, class: 'form-control' %>
</div> <%= f.label :category_id %>
<% end %> <%= f.collection_select :category_id, @categories, :id, :name, :selected => @product.category_id %>
<div> <%= f.label :price %>
<%= form_for(@product, url: {action: "edit"}) do |f| %> <%= f.number_field :price, class: 'form-control' %>
<%= f.label :name %><br /> <%= f.label :image %>
<%= f.text_field :name, autofocus: true, class: 'form-control' %> <%= f.text_field :image, class: 'form-control' %>
<%= f.label :category_id %> <%= f.label :description %>
<%= f.collection_select :category_id, @categories, :id, :name, :selected => @product.category_id %> <%= f.text_area :description, class: 'form-control', rows: '15' %>
<%= f.label :price %> <%= f.submit "Edit", class: "btn btn-primary" %>
<%= f.number_field :price, class: 'form-control' %> <% end %>
<%= f.label :image %> </div>
<%= f.text_field :image, class: 'form-control' %> <% end %>
<%= f.label :description %> \ No newline at end of file
<%= f.text_area :description, class: 'form-control', rows: '15' %>
<%= f.submit "Edit", class: "btn btn-primary" %>
<% end %>
</div>
<h2>Log in</h2> <h2>Log ind</h2>
<%= 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="field"> <div class="field">
......
<% if admin_signed_in? %> <% if notice %>
<div class="row"> <p id="notice"><%= notice %></p>
<h1>All users</h1> <% end %>
<table class="table"> <div class="col-lg-12">
<thead> <h2 class="text-left">All Users</h2>
<%= will_paginate @users %>
<table class="table table-hover">
<thead>
<tr>
<th>ID User</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr> <tr>
<th>Id</th> <td><%= user.id %></td>
<th>Email</th> <td><%= user.fullname %></td>
<td><%= user.email %></td>
<td><%= user.phone %></td>
<td><%= user.address %> </td>
<td><%= link_to "Delete", "/delete_user/#{user.id}", data: { confirm: 'Are you sure?' } %></td>
</tr> </tr>
</thead> <% end %>
<tbody> </tbody>
<% @users.each do |user|%> </table>
<tr> </div>
<td><%= user.id.to_s %></td> <%= will_paginate @users %>
<td><%= user.email.to_s %></td>
</tr>
<% end -%>
</tbody>
</table>
</div>
<% end %>
\ No newline at end of file
...@@ -2,55 +2,59 @@ ...@@ -2,55 +2,59 @@
<div class="span9"> <div class="span9">
<h2>Shopping Cart</h2> <h2>Shopping Cart</h2>
<div> <% if !session[:cart].nil? && !session[:cart].empty? %>
<ul> <div>
<table class="table table-striped table-hover"> <ul>
<thead> <table class="table table-striped table-hover">
<tr> <thead>
<th>Product Name</th> <tr>
<th>Quantity</th> <th>Product Name</th>
<th>Update</th> <th>Quantity</th>
<th>Delete</th> <th>Update</th>
<th>Price</th> <th>Delete</th>
<th>Total</th> <th>Price</th>
</tr> <th>Total</th>
</thead> </tr>
<tbody> </thead>
<% total = 0 %> <tbody>
<% if !@cart_product.nil? %> <% total = 0 %>
<% @cart_product.each do |id, quantity| %> <% if !@cart_product.nil? %>
<% product = Product.find_by_id(id) %> <% @cart_product.each do |id, quantity| %>
<% if !product.nil? %> <% product = Product.find_by_id(id) %>
<% total = total + product.price * quantity.to_i %> <% if !product.nil? %>
<tr> <% total = total + product.price * quantity.to_i %>
<td><%= link_to truncate(product.name, length:20), "/products/#{product.id}" %></td> <tr>
<form action="/cart_product/update" > <td><%= link_to truncate(product.name, length:20), "/products/#{product.id}" %></td>
<td><input name="new_quantity" min="1" max="100" type="number" class="span1" value= <%= quantity %> /></td> <form action="/cart_product/update" >
<td><input type="submit" value="Update" /><input type="hidden" name="id" value="<%= product.id %>"/></td> <td><input name="new_quantity" min="1" max="100" type="number" class="span1" value= <%= quantity %> /></td>
</form> <td><input type="submit" value="Update" /><input type="hidden" name="id" value="<%= product.id %>"/></td>
<td><form action="/cart_product/remove" ><input type="submit" value="Delete" /><input type="hidden" name="id" value="<%= product.id %>"/></form></td> </form>
<td><%= (product.price/100.to_f).to_s + "$" %></td> <td><form action="/cart_product/remove" ><input type="submit" value="Delete" /><input type="hidden" name="id" value="<%= product.id %>"/></form></td>
<td><%= ((product.price * quantity)/100.to_f).to_s + "$" %></td> <td><%= (product.price/100.to_f).to_s + "$" %></td>
</tr> <td><%= ((product.price * quantity)/100.to_f).to_s + "$" %></td>
</tr>
<% end %>
<% end %> <% end %>
<% else %>
<h2>Shopping Cart Empty</h2>
<% end %> <% end %>
<% else %> </tbody>
<h2>Shopping Cart Empty</h2> </table>
<% end %> </ul>
</tbody> </div>
</table>
</ul>
</div>
<dl class="dl-horizontal pull-right"> <dl class="dl-horizontal pull-right">
<dt>Sub-total:</dt> <dt>Sub-total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd> <dd><%=(total/100.to_f).to_s + "$" %></dd>
<dt>Total:</dt> <dt>Total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd> <dd><%=(total/100.to_f).to_s + "$" %></dd>
</dl> </dl>
<div class="clearfix"></div> <div class="clearfix"></div>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp;
<%= link_to "Delete All" ,'/cart_product/clear', class: "btn btn-lg btn-danger pull-right" %>
<%else%>
<h1>Your Cart is Empty!</h1>
<% end %>
<%= link_to "Continue Shopping " , root_path, class: "btn btn-primary" %> <%= link_to "Continue Shopping " , root_path, class: "btn btn-primary" %>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp;
<%= link_to "Delete All" ,'/cart_product/clear', class: "btn btn-lg btn-danger pull-right" %>
<!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%--> <!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%-->
</div> </div>
\ No newline at end of file
...@@ -32,25 +32,36 @@ ...@@ -32,25 +32,36 @@
<dd><%= number_to_currency(@total/100.to_f, :unit => '$')%></dd> <dd><%= number_to_currency(@total/100.to_f, :unit => '$')%></dd>
</dl></div><br><br><br> </dl></div><br><br><br>
<div align="center"> <div align="center">
<% flash.each do |message_type, message| %> <% if @new_cart.errors.any? %>
<div class="alert alert-<%= message_type %>"><%= message %></div> <div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@new_cart.errors.count, "error") %>.
</div>
<ul>
<% @new_cart.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %> <% end %>
<div> <div>
<%= form_for(@new_cart, url: create_cart_path) do |f| %>
<%= f.label :email %>
<% if user_signed_in? %> <% if user_signed_in? %>
<% email = @current_user.email %> <% email = @current_user.email %>
<% end %>
<%= form_for(:session, url: create_cart_path) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, value: email, class: 'form-control'%> <%= f.email_field :email, value: email, class: 'form-control'%>
<%= f.label :fullname %> <% else %>
<%= f.text_field :fullname, class: 'form-control' %> <%= f.email_field :email, class: 'form-control'%>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</br>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<% end %> <% end %>
<%= f.label :fullname %>
<%= f.text_field :fullname, class: 'form-control' %>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</br>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<% end %>
</div> </div>
<%else%> <%else%>
<h1>Your Cart is Empty. Thank you for order</h1> <h1>Your Cart is Empty. Thank you for order</h1>
......
<h1>show cart</h1> <%= render 'categories/view' %>
<ul>
<% if !@show_cart.nil? %>
<% @show_cart.each do |cart| %>
<h4>
<%= cart.id.to_s + " | "%>
<%= cart.mail.to_s + " | " %>
<%= cart.name.to_s + " | " %>
<%= cart.total_price.to_s + " | " %>
<%= cart.status.to_s + " | " %>
<%= cart.address %>
</h4>
<% end -%>
<% else -%>
<h2> Your cart Empty </h2>
<% end -%>
</ul> <div class="span9">
<h2>Order Cart</h2>
<div>
<ul>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<% if !@show_cart.nil? %>
<% @show_cart.each do |show_cart| %>
<% cart_product = CartProduct.find_by_id(show_cart.id) %>
<% product = Product.find_by_id(cart_product.product_id) %>
<tr>
<td><%= truncate(product.name, length:20) %></td>
<td><%= cart_product.quantity %></td>
<td><%= (cart_product.price/100.to_f).to_s + "$" %></td>
<td><%= (show_cart.total_price/100.to_f).to_s + "$" %></td>
<td><%= show_cart.status %></td>
</tr>
<% end %>
<% else %>
<h2>Your Order Empty</h2>
<% end %>
</tbody>
</table>
</ul>
</div>
<div class="clearfix"></div>
<%= link_to "Continue Shopping " , root_path, class: "btn btn-primary pull-right" %>
<!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%-->
</div>
\ No newline at end of file
<h2>Resend confirmation instructions</h2>
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>
<div class="actions">
<%= f.submit "Resend confirmation instructions" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
<p>Hello <%= @resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
<p>Hello <%= @resource.email %>!</p>
<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>
<p>Click the link below to unlock your account:</p>
<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %></p>
<h2>Change your password</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<div class="field">
<%= f.label :password, "New password" %><br />
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Change my password" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<h2>Forgot your password?</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Send me reset password instructions" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<% if admin_signed_in? %>
<% else %>
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<%= link_to "Back", :back %>
<% end %>
\ No newline at end of file
<div class="span12">
<div align="center">
<% if admin_signed_in? %>
<h2>Admin Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<% else %>
<h2>User Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :fullname %><br />
<%= f.text_field :fullname, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone, autofocus: true %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<% end %>
</div>
</div>
\ No newline at end of file
<div class="span12">
<div align="center">
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
\ No newline at end of file
<%- if controller_name != 'sessions' %>
<%= link_to "Log in", new_session_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% 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 #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
<% end -%>
<% end -%>
<h2>Resend unlock instructions</h2>
<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Resend unlock instructions" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
...@@ -6,19 +6,19 @@ ...@@ -6,19 +6,19 @@
<div class="nav-collapse collapse"> <div class="nav-collapse collapse">
<!--%= link_to "VenShop", root_path, id: "logo" %--> <!--%= link_to "VenShop", root_path, id: "logo" %-->
<ul class="nav"> <ul class="nav">
<li><%= link_to "Home", root_path %></li> <!-- li><%= link_to "Home", root_path %></li -->
<li><%= link_to "About", about_path %></li> <li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li> <li><%= link_to "Contact", contact_path %></li>
</ul> </ul>
<!--form class="navbar-form form-search pull-right"> <form action= "/search/show" method="get" class="navbar-form form-search pull-right">
<input id="Search" name="Search" type="text" placeholder="type text to search for" class="input-medium search-query"> <input id="Search" name="search_params" type="text" placeholder="type text to search for" class="input-medium search-query">
<button type="submit" class="btn">Search</button> <button type="submit" class="btn">Search</button>
</form--> </form>
<ul class="nav pull-right"> <ul class="nav pull-right">
<% if user_signed_in? %> <% if user_signed_in? %>
<!--li><%= link_to "Carts", "" %></li--> <li><%= link_to "Cart", carts_path %></li>
<!--li><%= link_to "Profile", "" %></li--> <li><%= link_to "Order", carts_show_path %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li> <li><%= link_to "Setting", edit_user_registration_path %></li>
<li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li> <li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li>
<% elsif admin_signed_in? %> <% elsif admin_signed_in? %>
<li><%= link_to "Products", admins_product_path(id: "1") %></li> <li><%= link_to "Products", admins_product_path(id: "1") %></li>
......
<%= render 'categories/view' %> <%= render 'categories/view' %>
<div class="span9"> <div class="span9">
<div class="hero-unit"> <div class="hero-unit">
<h1 class="">Ventura Trainee</h1> <h1 class="">Ventura Trainee</h1>
<p class="">Ruby on Rails</p> <p class="">Ruby on Rails</p>
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<label>Quantity:</label> <label>Quantity:</label>
<div> <div>
<form action="/cart_products"> <form action="/cart_products">
<input type="number" name="quantity" value= "1" min="1" max="100" value="1"> <input type="number" name="quantity" value= "1" min="1" max="100">
<input type="hidden" name="id" value="<%= @product.id %>"/> <input type="hidden" name="id" value="<%= @product.id %>"/>
<input type="submit" value="Add to cart" class = "btn btn-lg btn-success" /> <input type="submit" value="Add to cart" class = "btn btn-lg btn-success" />
</form> </form>
......
<%= render 'categories/view' %>
<div class="span9">
<div class="hero-unit">
<h1 class="">Ventura Trainee</h1>
<p class="">Ruby on Rails</p>
<p><%= link_to "Learn more »", about_path, class: "btn btn-primary btn-large"%></p>
</div>
<ul class="thumbnails">
<% if !@products.empty? %>
<% @products.each do |product| %>
<li class="span3">
<div class="thumbnail">
<%= image_tag product.image %>
<div class="caption">
<div style="height: 40px">
<h4><%= truncate(product.name, :length => 30, :omission => '...') %></h4>
</div>
<p><%= (product.price/100.to_f).to_s + "$" %></p>
<%= link_to "View Info", "/products/#{product.id}", class: "btn btn-primary" %>
<form action="/cart_products" class="pull-right" >
<input type="hidden" name="quantity" value= "1">
<input type="hidden" name="id" value="<%= product.id %>"/>
<input type="submit" value="Add to Cart" class = "btn btn-success" />
</form>
<!--%= link_to "Add to Cart", "/cart_products/#{product.id}", class: "btn btn-success" %-->
</div>
</div>
</li>
<% end %>
<% else %>
<li>
<h1> Product not found</h1></li>
<% end %>
</ul>
</div>
\ No newline at end of file
VenShop::Application.configure do VenShop::Application.configure do
# Settings specified here will take precedence over those in config/application.rb. # Settings specified here will take precedence over those in config/application.rb.
config.solr_host = "http://localhost:8080/solr/core0"
# In the development environment your application's code is reloaded on # In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development # every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes. # since you don't have to restart the web server when you make code changes.
......
VenShop::Application.routes.draw do VenShop::Application.routes.draw do
namespace :admins do
get 'users/show'
end
namespace :admins do
get 'users/new'
end
devise_for :users devise_for :users
devise_for :admins devise_for :admins
namespace :admins do namespace :users do
resources :products resources :products
end end
namespace :admins do
resources :products, :users
end
root 'products#index' root 'products#index'
get 'show_product' => 'products#index' get 'show_product' => 'products#index'
get 'insert_product' => 'admins/products#insert' get 'insert_product' => 'admins/products#insert'
get 'update_product/:id' => 'admins/products#update' get 'update_product/:id' => 'admins/products#update'
get 'delete_product/:id' => 'admins/products#delete' get 'delete_product/:id' => 'admins/products#destroy', as: 'destroy_product'
get 'show_cart' => 'admins/carts#show' get 'show_cart' => 'admins/carts#show'
get 'show_user' => 'admins/users#show' get 'show_user' => 'admins/users#show'
patch '/admins/products/:id/edit'=>'admins/products#edit' patch '/admins/products/:id/edit'=>'admins/products#edit'
patch '/admins/carts/:id/edit' => 'admins/carts#edit' patch '/admins/carts/:id/edit' => 'admins/carts#edit'
get 'delete_user/:id' => 'admins/users#delete'
get 'search/show'
get 'cart_products' => 'cart_products#add' get 'cart_products' => 'cart_products#add'
get 'cart_product/remove' => 'cart_products#remove' get 'cart_product/remove' => 'cart_products#remove'
......
class CreateCarts < ActiveRecord::Migration class CreateCarts < ActiveRecord::Migration
def change def change
create_table :carts do |t| create_table :carts do |t|
t.integer :User_id t.integer :user_id
t.integer :total_price t.integer :total_price
t.string :status t.string :status
t.string :fullname t.string :fullname
t.integer :phone t.string :phone
t.string :address t.string :address
t.string :email t.string :email
......
class ChangeLimit < ActiveRecord::Migration
def change
change_column :products, :price, :integer, :limit => 5
change_column :users, :phone, :string
change_column :cart_products, :cart_id, :integer, :limit => 5
change_column :cart_products, :product_id, :integer, :limit => 5
change_column :cart_products, :quantity, :integer, :limit => 5
change_column :cart_products, :price, :integer, :limit => 5
change_column :carts, :user_id, :integer, :limit => 5
change_column :carts, :total_price, :integer, :limit => 5
change_column :products, :name, :string, :limit => 1000
change_column :products, :category_id, :integer, :limit => 5
end
end
class AddFieldToUser < ActiveRecord::Migration class AddFieldUser < ActiveRecord::Migration
def change def change
add_column :users, :fullname, :string add_column :users, :fullname, :string
add_column :users, :phone, :integer add_column :users, :phone, :string
add_column :users, :address, :string add_column :users, :address, :string
end end
end end
class AddFieldAdmin < ActiveRecord::Migration
def change
add_column :admins, :fullname, :string
add_column :admins, :phone, :string
add_column :admins, :address, :string
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,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: 20150730075349) do ActiveRecord::Schema.define(version: 20150811014432) do
create_table "admins", force: :cascade do |t| create_table "admins", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false t.string "email", limit: 255, default: "", null: false
...@@ -26,26 +26,29 @@ ActiveRecord::Schema.define(version: 20150730075349) do ...@@ -26,26 +26,29 @@ ActiveRecord::Schema.define(version: 20150730075349) do
t.string "last_sign_in_ip", limit: 255 t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "fullname", limit: 255
t.string "phone", limit: 255
t.string "address", limit: 255
end end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree 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", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree
create_table "cart_products", force: :cascade do |t| create_table "cart_products", force: :cascade do |t|
t.integer "cart_id", limit: 4 t.integer "cart_id", limit: 8
t.integer "product_id", limit: 4 t.integer "product_id", limit: 8
t.integer "quantity", limit: 4 t.integer "quantity", limit: 8
t.integer "price", limit: 4 t.integer "price", limit: 8
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "carts", force: :cascade do |t| create_table "carts", force: :cascade do |t|
t.integer "User_id", limit: 4 t.integer "user_id", limit: 8
t.integer "total_price", limit: 4 t.integer "total_price", limit: 8
t.string "status", limit: 255 t.string "status", limit: 255
t.string "fullname", limit: 255 t.string "fullname", limit: 255
t.integer "phone", limit: 4 t.string "phone", limit: 255
t.string "address", limit: 255 t.string "address", limit: 255
t.string "email", limit: 255 t.string "email", limit: 255
t.datetime "created_at", null: false t.datetime "created_at", null: false
...@@ -59,11 +62,11 @@ ActiveRecord::Schema.define(version: 20150730075349) do ...@@ -59,11 +62,11 @@ ActiveRecord::Schema.define(version: 20150730075349) do
end end
create_table "products", force: :cascade do |t| create_table "products", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 1000
t.string "image", limit: 255 t.string "image", limit: 255
t.integer "price", limit: 4 t.integer "price", limit: 8
t.string "description", limit: 20000 t.string "description", limit: 20000
t.integer "category_id", limit: 4 t.integer "category_id", limit: 8
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
...@@ -82,7 +85,7 @@ ActiveRecord::Schema.define(version: 20150730075349) do ...@@ -82,7 +85,7 @@ ActiveRecord::Schema.define(version: 20150730075349) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "fullname", limit: 255 t.string "fullname", limit: 255
t.integer "phone", limit: 4 t.string "phone", limit: 255
t.string "address", limit: 255 t.string "address", limit: 255
end end
......
...@@ -3,17 +3,17 @@ class ImportAmazon ...@@ -3,17 +3,17 @@ class ImportAmazon
def initialize def initialize
@request = Vacuum.new('US') @request = Vacuum.new('US')
@request.configure( @request.configure(
aws_access_key_id: "AKIAJ77C4CTZOP7TUVWQ", aws_access_key_id: 'AKIAIAJR65JO6EIPQWTA',
aws_secret_access_key: "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", aws_secret_access_key: '8rpb5q169RUtj7HU3njH3zxcKthZJmWbgtrzESXy',
associate_tag: "zigexn6400-22" associate_tag: 'microv'
) )
@request.associate_tag = 'tag' @request.associate_tag = 'tag'
end end
def import_product def import_product
(1..100).each do |page| (1..100).each do |page|
get_response(page).each do |item| get_response(page).each do |item|
begin begin
category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"]) category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"])
products = Product.find_or_create_by(name: item["ItemAttributes"].to_h["Title"]) do |products| products = Product.find_or_create_by(name: item["ItemAttributes"].to_h["Title"]) do |products|
products.image = item["LargeImage"]["URL"] products.image = item["LargeImage"]["URL"]
...@@ -41,7 +41,7 @@ class ImportAmazon ...@@ -41,7 +41,7 @@ class ImportAmazon
'ItemPage' => page, 'ItemPage' => page,
} }
) )
response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"] response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"]
end end
end end
\ No newline at end of file
require 'test_helper' require 'test_helper'
class Admins::UsersControllerTest < ActionController::TestCase class Admins::UsersControllerTest < ActionController::TestCase
test "should get show" do test "should get new" do
get :show get :new
assert_response :success assert_response :success
end end
......
require 'test_helper'
class SearchControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
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