Commit b0ddaf8e by Vy Quoc Vu

nginx

parents 4cbbdb6a 1f9b4365
...@@ -7,6 +7,7 @@ gem 'bootstrap-will_paginate', '0.0.10' ...@@ -7,6 +7,7 @@ gem 'bootstrap-will_paginate', '0.0.10'
gem 'faker', '1.4.2' gem 'faker', '1.4.2'
gem 'solr-ruby' gem 'solr-ruby'
gem 'rsolr' gem 'rsolr'
gem 'unicorn'
# 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'
......
...@@ -97,6 +97,7 @@ GEM ...@@ -97,6 +97,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)
listen (3.0.3) listen (3.0.3)
rb-fsevent (>= 0.9.3) rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9) rb-inotify (>= 0.9)
...@@ -163,6 +164,7 @@ GEM ...@@ -163,6 +164,7 @@ 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)
rb-fsevent (0.9.5) rb-fsevent (0.9.5)
rb-inotify (0.9.5) rb-inotify (0.9.5)
...@@ -204,6 +206,10 @@ GEM ...@@ -204,6 +206,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)
...@@ -243,6 +249,7 @@ DEPENDENCIES ...@@ -243,6 +249,7 @@ DEPENDENCIES
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-bootstrap will_paginate-bootstrap
......
class Admins::ProductsController < ApplicationController class Admins::ProductsController < ApplicationController
before_action :solr_init, only: [:edit,:create,:destroy]
def solr_init
@solr = Mysolr.new
end
def new def new
if admin_signed_in? if admin_signed_in?
@product = Product.new @product = Product.new
...@@ -19,16 +24,14 @@ class Admins::ProductsController < ApplicationController ...@@ -19,16 +24,14 @@ class Admins::ProductsController < ApplicationController
def create def create
if admin_signed_in? if admin_signed_in?
if product_params[:price].to_i >= 0 && params[:category][:id].to_i > 0 && if validate_product_params?(product_params, params)
!product_params[:image].nil? && !product_params[:image].empty? &&
!product_params[:price].empty? && !product_params[:name].empty? &&
product_params[:price].to_i <= 999999
product = Product.find_or_create_by(name: product_params[:name]) do |product| product = Product.find_or_create_by(name: product_params[:name]) do |product|
product.image = product_params[:image] product.image = product_params[:image]
product.category_id = params[:category][:id] product.category_id = params[:category][:id]
product.price = product_params[:price].to_i*100 product.price = product_params[:price].to_i*100
product.description = product_params[:description] product.description = product_params[:description]
end end
@solr.add_product_to_solr(product) if product.save
flash[:success] = "Success!" flash[:success] = "Success!"
redirect_to "/products/#{product.id}" redirect_to "/products/#{product.id}"
else else
...@@ -43,23 +46,24 @@ class Admins::ProductsController < ApplicationController ...@@ -43,23 +46,24 @@ class Admins::ProductsController < ApplicationController
def edit def edit
if admin_signed_in? if admin_signed_in?
if product_params[:price].to_i > 0 && params[:category][:id].to_i > 0 && if validate_product_params?(product_params, params)
!product_params[:image].nil? && !product_params[:image].empty? &&
!product_params[:price].empty? && !product_params[:name].empty? &&
product_params[:price].to_i <= 999999
product = Product.find(params[:id]) product = Product.find(params[:id])
product.name = product_params[:name] product.name = product_params[:name]
product.image = product_params[:image] product.image = product_params[:image]
product.category_id = params[:category][:id] product.category_id = params[:category][:id]
product.price = product_params[:price].to_i*100 product.price = product_params[:price].to_i*100
product.description = product_params[:description] product.description = product_params[:description]
product.save @solr.update_product_on_solr(product) if product.save
flash[:success] = "Success!" flash[:success] = "Success!"
redirect_to "/products/#{product.id}" redirect_to "/products/#{product.id}"
else else
flash[:danger] = "Wrong params! Please check name, price and image again." flash[:danger] = "Wrong params! Please check name, price and image again."
if !product_params
redirect_to :action => :index
else
redirect_to :back redirect_to :back
end end
end
else else
flash[:danger] = "only admin!" flash[:danger] = "only admin!"
redirect_to root_path redirect_to root_path
...@@ -70,6 +74,7 @@ class Admins::ProductsController < ApplicationController ...@@ -70,6 +74,7 @@ class Admins::ProductsController < ApplicationController
if admin_signed_in? if admin_signed_in?
@product = Product.find(params[:id]) @product = Product.find(params[:id])
@product.destroy @product.destroy
@solr.delete_product_from_solr(params[:id])
flash[:success] = "Deleted!" flash[:success] = "Deleted!"
redirect_to :back redirect_to :back
else else
...@@ -88,9 +93,16 @@ class Admins::ProductsController < ApplicationController ...@@ -88,9 +93,16 @@ class Admins::ProductsController < ApplicationController
private private
def validate_product_params?(product_params, params)
product_params[:price].to_i >= 0 && params[:category][:id].to_i > 0 &&
!product_params[:image].nil? && !product_params[:image].empty? &&
!product_params[:price].empty? && !product_params[:name].empty? &&
product_params[:price].to_i <= 999999
end
def product_params def product_params
params.require(:product).permit(:name, :price, :description, if params[:product]
:image, :id) params.require(:product).permit(:name, :price, :description,:image, :id)
end
end end
end end
require 'solr'
require 'rsolr'
require 'rubygems'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
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.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
......
...@@ -31,7 +31,6 @@ class CartProductsController < ApplicationController ...@@ -31,7 +31,6 @@ class CartProductsController < ApplicationController
end end
def update def update
byebug
if params[:id].nil? if params[:id].nil?
flash[:danger] = "Product not found!" flash[:danger] = "Product not found!"
elsif params[:new_quantity].nil? || params[:new_quantity].to_i <= 0 elsif params[:new_quantity].nil? || params[:new_quantity].to_i <= 0
......
...@@ -30,6 +30,7 @@ class CartsController < ApplicationController ...@@ -30,6 +30,7 @@ class CartsController < ApplicationController
Emailer.send_email_to(cart_params[:mail].to_s,session[:cart]).deliver Emailer.send_email_to(cart_params[:mail].to_s,session[:cart]).deliver
flash[:success] = "Success!" flash[:success] = "Success!"
session[:cart] = nil session[:cart] = nil
redirect_to :action => "show"
else else
flash[:danger] = "Your mail, your address, your name" flash[:danger] = "Your mail, your address, your name"
redirect_to :back redirect_to :back
......
...@@ -21,17 +21,34 @@ class ProductsController < ApplicationController ...@@ -21,17 +21,34 @@ class ProductsController < ApplicationController
end end
end end
def check_search_params(search_params)
special_characters = ["+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "~", ":",'\\','/']
@ok_string = ""
search_params.each_char do |key|
special_characters.each do |ch|
key = "\\#{ch}" if key == ch
end
@ok_string += key
end
end
def search def search
if params[:search] @solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on)
if !params[:page] || (params[:page].to_i == 0) check_search_params(params["search_params"])
params[:page] = 1 query = "((name:#{@ok_string}))"
else select_obj = Solr::Request::Select.new(nil, {'q' => query})
params[:page] 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 end
@products = Product.search(params[:search]).paginate(page: params[:page], :per_page => 50) if @products.empty?
flash[:success] = "Success!" flash.now[:danger] = "No result!"
else else
flash[:danger] = "Danger!" flash.now[:success] = "All result!"
end end
end end
......
class Mysolr < ActiveRecord::Base
def initialize
@solr = RSolr.connect :url => Rails.configuration.solr_host.to_s
end
def add_product_to_solr(product)
docs = [{:id =>product.id,
:price => product.price,
:name => product.name,
:description => product.description
}]
@solr.add docs
@solr.commit
@solr.optimize
end
def delete_product_from_solr(id)
@solr.delete_by_query "id:#{id}"
@solr.commit
@solr.optimize
end
def update_product_on_solr(product)
docs = [{:id =>product.id,
:price => product.price,
:name => product.name,
:description => product.description
}]
@solr.delete_by_query "id:#{product.id}"
@solr.add docs
@solr.commit
@solr.optimize
end
end
class Solr < ActiveRecord::Base
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<%= f.label :price %> <%= f.label :price %>
<%= f.number_field :price, value: @product.price/100, class: 'form-control' %> <%= f.number_field :price, value: @product.price/100, class: 'form-control' %>
<br> <br>
<%= f.label :Category %> <%= f.label :Category %><p> </p>
<%= collection_select :category, :id, Category.all, :id, :name, :selected => @product.category_id %> <%= collection_select :category, :id, Category.all, :id, :name, :selected => @product.category_id %>
</br> </br>
......
...@@ -6,8 +6,9 @@ ...@@ -6,8 +6,9 @@
<div class="navbar-header"> <div class="navbar-header">
<table> <table>
<td><a class="navbar-brand" href= "/" > Venshop </a></td> <td><a class="navbar-brand" href= "/" > Venshop </a></td>
<td></td>
<form action= "/search" style="padding-left : 100px ; resize: vertical;" method="get" class="navbar-brand" > <form action= "/search" style="padding-left : 100px ; resize: vertical;" method="get" class="navbar-brand" >
<td ><input type="text" name="search" value="" , class="form-control"/></td> <td ><input type="text" name="search_params" value="" , class="form-control"/></td>
<td style="padding-left : 20px" ><input type="submit" value="Search" style="font-size: 1em;" , class="btn btn-primary"/></td> <td style="padding-left : 20px" ><input type="submit" value="Search" style="font-size: 1em;" , class="btn btn-primary"/></td>
</form> </form>
</table> </table>
......
<%= will_paginate %> <div class="row">
<div class="row">
<% @products.each do |product| %> <% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px"> <div class="col-md-4" style="padding-top : 20px">
<div> <div>
...@@ -15,5 +14,4 @@ ...@@ -15,5 +14,4 @@
</div> </div>
</div> </div>
<% end %> <% end %>
</div> </div>
<%= will_paginate %> \ No newline at end of file
\ No newline at end of file
<%= will_paginate %>
<div class="row"> <div class="row">
<% @products.each do |product| %> <% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px"> <div class="col-md-4" style="padding-top : 20px">
<div> <div>
...@@ -18,5 +18,4 @@ ...@@ -18,5 +18,4 @@
</div> </div>
</div> </div>
<% end %> <% end %>
</div> </div>
<%= will_paginate %> \ No newline at end of file
\ No newline at end of file
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
</div> </div>
<div class="col-md-9" > <div class="col-md-9" >
<div class="form-group"> <div class="form-group">
<%= will_paginate %>
<%= render 'products/custom_products' %> <%= render 'products/custom_products' %>
<%= will_paginate %>
</div> </div>
</div> </div>
\ No newline at end of file
...@@ -7,7 +7,9 @@ Rails.application.configure do ...@@ -7,7 +7,9 @@ Rails.application.configure do
config.cache_classes = false config.cache_classes = false
# Do not eager load code on boot. # Do not eager load code on boot.
config.eager_load = false config.eager_load = false
config.solr_host = "http://localhost:8080/solr/core0"
# Show full error reports and disable caching. # Show full error reports and disable caching.
config.consider_all_requests_local = true config.consider_all_requests_local = true
......
...@@ -9,7 +9,7 @@ Rails.application.configure do ...@@ -9,7 +9,7 @@ Rails.application.configure do
# and those relying on copy on write to perform better. # and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance. # Rake tasks automatically ignore this option for performance.
config.eager_load = true config.eager_load = true
config.solr_host = "http://localhost:8080/solr/core0"
# Full error reports are disabled and caching is turned on. # Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false config.consider_all_requests_local = false
config.action_controller.perform_caching = true config.action_controller.perform_caching = true
......
app_path = File.expand_path(File.dirname(__FILE__) + '/..')
...@@ -4,8 +4,7 @@ class DeviseCreateUsers < ActiveRecord::Migration ...@@ -4,8 +4,7 @@ class DeviseCreateUsers < ActiveRecord::Migration
## Database authenticatable ## Database authenticatable
t.string :email, null: false, default: "" t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: "" t.string :encrypted_password, null: false, default: ""
t.string :name t.string :name , default: "Username"
t.string :address
## Recoverable ## Recoverable
t.string :reset_password_token t.string :reset_password_token
......
...@@ -4,6 +4,7 @@ class DeviseCreateAdmins < ActiveRecord::Migration ...@@ -4,6 +4,7 @@ class DeviseCreateAdmins < ActiveRecord::Migration
## Database authenticatable ## Database authenticatable
t.string :email, null: false, default: "" t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: "" t.string :encrypted_password, null: false, default: ""
t.string :name , default: "adminname"
## Recoverable ## Recoverable
t.string :reset_password_token t.string :reset_password_token
......
class CreateSolrs < ActiveRecord::Migration class CreateMysolrs < ActiveRecord::Migration
def change def change
create_table :solrs do |t| create_table :mysolrs do |t|
t.timestamps null: false t.timestamps null: false
end end
......
...@@ -11,11 +11,12 @@ ...@@ -11,11 +11,12 @@
# #
# 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: 20150730041232) do ActiveRecord::Schema.define(version: 20150811034415) 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
t.string "encrypted_password", limit: 255, default: "", null: false t.string "encrypted_password", limit: 255, default: "", null: false
t.string "name", limit: 255, default: "adminname"
t.string "reset_password_token", limit: 255 t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at" t.datetime "reset_password_sent_at"
t.datetime "remember_created_at" t.datetime "remember_created_at"
...@@ -59,6 +60,11 @@ ActiveRecord::Schema.define(version: 20150730041232) do ...@@ -59,6 +60,11 @@ ActiveRecord::Schema.define(version: 20150730041232) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "mysolrs", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "products", force: :cascade do |t| create_table "products", force: :cascade do |t|
t.string "name", limit: 3000, null: false t.string "name", limit: 3000, null: false
t.string "image", limit: 1000, null: false t.string "image", limit: 1000, null: false
...@@ -69,9 +75,20 @@ ActiveRecord::Schema.define(version: 20150730041232) do ...@@ -69,9 +75,20 @@ ActiveRecord::Schema.define(version: 20150730041232) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "rsorls", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "solrs", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false t.string "encrypted_password", limit: 255, default: "", null: false
t.string "name", limit: 255, default: "Username"
t.string "reset_password_token", limit: 255 t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at" t.datetime "reset_password_sent_at"
t.datetime "remember_created_at" t.datetime "remember_created_at"
......
# 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
# 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 MysolrTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class RsorlTest < 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