Commit b0ddaf8e by Vy Quoc Vu

nginx

parents 4cbbdb6a 1f9b4365
......@@ -7,6 +7,7 @@ gem 'bootstrap-will_paginate', '0.0.10'
gem 'faker', '1.4.2'
gem 'solr-ruby'
gem 'rsolr'
gem 'unicorn'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
......@@ -60,4 +61,4 @@ end
group :production do
#gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
end
\ No newline at end of file
end
......@@ -97,6 +97,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
kgio (2.9.3)
listen (3.0.3)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
......@@ -163,6 +164,7 @@ GEM
activesupport (= 4.2.3)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.15.0)
rake (10.4.2)
rb-fsevent (0.9.5)
rb-inotify (0.9.5)
......@@ -204,6 +206,10 @@ GEM
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
unicorn (4.9.0)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
vacuum (1.3.0)
jeff (~> 1.0)
multi_xml (~> 0.5.0)
......@@ -243,6 +249,7 @@ DEPENDENCIES
spring
turbolinks
uglifier (>= 1.3.0)
unicorn
vacuum
web-console (~> 2.0)
will_paginate-bootstrap
......
class Admins::ProductsController < ApplicationController
before_action :solr_init, only: [:edit,:create,:destroy]
def solr_init
@solr = Mysolr.new
end
def new
if admin_signed_in?
@product = Product.new
......@@ -19,16 +24,14 @@ class Admins::ProductsController < ApplicationController
def create
if admin_signed_in?
if 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
if validate_product_params?(product_params, params)
product = Product.find_or_create_by(name: product_params[:name]) do |product|
product.image = product_params[:image]
product.category_id = params[:category][:id]
product.price = product_params[:price].to_i*100
product.description = product_params[:description]
end
@solr.add_product_to_solr(product) if product.save
flash[:success] = "Success!"
redirect_to "/products/#{product.id}"
else
......@@ -43,22 +46,23 @@ class Admins::ProductsController < ApplicationController
def edit
if admin_signed_in?
if 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
if validate_product_params?(product_params, params)
product = Product.find(params[:id])
product.name = product_params[:name]
product.image = product_params[:image]
product.category_id = params[:category][:id]
product.price = product_params[:price].to_i*100
product.description = product_params[:description]
product.save
@solr.update_product_on_solr(product) if product.save
flash[:success] = "Success!"
redirect_to "/products/#{product.id}"
else
flash[:danger] = "Wrong params! Please check name, price and image again."
redirect_to :back
if !product_params
redirect_to :action => :index
else
redirect_to :back
end
end
else
flash[:danger] = "only admin!"
......@@ -70,6 +74,7 @@ class Admins::ProductsController < ApplicationController
if admin_signed_in?
@product = Product.find(params[:id])
@product.destroy
@solr.delete_product_from_solr(params[:id])
flash[:success] = "Deleted!"
redirect_to :back
else
......@@ -88,9 +93,16 @@ class Admins::ProductsController < ApplicationController
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
params.require(:product).permit(:name, :price, :description,
:image, :id)
if params[:product]
params.require(:product).permit(:name, :price, :description,:image, :id)
end
end
end
\ No newline at end of file
end
require 'solr'
require 'rsolr'
require 'rubygems'
class ApplicationController < ActionController::Base
before_action :call_category , only: [:show, :index]
before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
......
......@@ -31,7 +31,6 @@ class CartProductsController < ApplicationController
end
def update
byebug
if params[:id].nil?
flash[:danger] = "Product not found!"
elsif params[:new_quantity].nil? || params[:new_quantity].to_i <= 0
......
......@@ -30,6 +30,7 @@ class CartsController < ApplicationController
Emailer.send_email_to(cart_params[:mail].to_s,session[:cart]).deliver
flash[:success] = "Success!"
session[:cart] = nil
redirect_to :action => "show"
else
flash[:danger] = "Your mail, your address, your name"
redirect_to :back
......
......@@ -21,17 +21,34 @@ class ProductsController < ApplicationController
end
end
def search
if params[:search]
if !params[:page] || (params[:page].to_i == 0)
params[:page] = 1
else
params[:page]
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
@products = Product.search(params[:search]).paginate(page: params[:page], :per_page => 50)
flash[:success] = "Success!"
@ok_string += key
end
end
def search
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on)
check_search_params(params["search_params"])
query = "((name:#{@ok_string}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query})
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
if @products.empty?
flash.now[:danger] = "No result!"
else
flash[:danger] = "Danger!"
flash.now[:success] = "All result!"
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 @@
<%= f.label :price %>
<%= f.number_field :price, value: @product.price/100, class: 'form-control' %>
<br>
<%= f.label :Category %>
<%= f.label :Category %><p> </p>
<%= collection_select :category, :id, Category.all, :id, :name, :selected => @product.category_id %>
</br>
......
......@@ -6,8 +6,9 @@
<div class="navbar-header">
<table>
<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" >
<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>
</form>
</table>
......
<%= will_paginate %>
<div class="row">
<% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px">
<div>
<%= image_tag(product.image, size: "200x250")%>
<%= simple_format(truncate(product.name, length:21)) %>
</div>
<div>
<p> Price: <%= price = (product.price/100.to_f) %> </p>
</br>
<%= link_to "Edit", "/admins/products/#{product.id}", class: "btn btn-lg btn-primary" %>
<%= link_to "delete", "/admins/destroy/#{product.id}", class: "btn btn-lg btn-danger" %>
</form>
</div>
<div class="row">
<% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px">
<div>
<%= image_tag(product.image, size: "200x250")%>
<%= simple_format(truncate(product.name, length:21)) %>
</div>
<% end %>
</div>
<%= will_paginate %>
\ No newline at end of file
<div>
<p> Price: <%= price = (product.price/100.to_f) %> </p>
</br>
<%= link_to "Edit", "/admins/products/#{product.id}", class: "btn btn-lg btn-primary" %>
<%= link_to "delete", "/admins/destroy/#{product.id}", class: "btn btn-lg btn-danger" %>
</form>
</div>
</div>
<% end %>
</div>
\ No newline at end of file
<%= will_paginate %>
<div class="row">
<% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px">
<div>
<%= image_tag(product.image, size: "200x250")%>
<%= simple_format(truncate(product.name, length:21)) %>
<%= (product.price/100.to_f).to_s + "$" %>
</div>
<div>
<form action="/cart_products" style="padding-left : 0px" >
<input type="number" name="quantity" value= "1" min="1" max="100">
</br>
<input type="submit" value="Add" class = "btn btn-lg btn-success" />
<%= link_to "Detail", "/products/#{product.id}", class: "btn btn-lg btn-info" %>
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
</div>
<div class="row">
<% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px">
<div>
<%= image_tag(product.image, size: "200x250")%>
<%= simple_format(truncate(product.name, length:21)) %>
<%= (product.price/100.to_f).to_s + "$" %>
</div>
<% end %>
</div>
<%= will_paginate %>
\ No newline at end of file
<div>
<form action="/cart_products" style="padding-left : 0px" >
<input type="number" name="quantity" value= "1" min="1" max="100">
</br>
<input type="submit" value="Add" class = "btn btn-lg btn-success" />
<%= link_to "Detail", "/products/#{product.id}", class: "btn btn-lg btn-info" %>
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
</div>
</div>
<% end %>
</div>
\ No newline at end of file
......@@ -5,6 +5,8 @@
</div>
<div class="col-md-9" >
<div class="form-group">
<%= will_paginate %>
<%= render 'products/custom_products' %>
<%= will_paginate %>
</div>
</div>
\ No newline at end of file
......@@ -7,7 +7,9 @@ Rails.application.configure do
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
config.solr_host = "http://localhost:8080/solr/core0"
# Show full error reports and disable caching.
config.consider_all_requests_local = true
......
......@@ -9,7 +9,7 @@ Rails.application.configure do
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
config.solr_host = "http://localhost:8080/solr/core0"
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
......
app_path = File.expand_path(File.dirname(__FILE__) + '/..')
......@@ -4,8 +4,7 @@ class DeviseCreateUsers < ActiveRecord::Migration
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :name
t.string :address
t.string :name , default: "Username"
## Recoverable
t.string :reset_password_token
......
......@@ -4,6 +4,7 @@ class DeviseCreateAdmins < ActiveRecord::Migration
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :name , default: "adminname"
## Recoverable
t.string :reset_password_token
......
class CreateSolrs < ActiveRecord::Migration
class CreateMysolrs < ActiveRecord::Migration
def change
create_table :solrs do |t|
create_table :mysolrs do |t|
t.timestamps null: false
end
......
......@@ -11,21 +11,22 @@
#
# 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|
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "email", 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.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
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.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree
......@@ -59,6 +60,11 @@ ActiveRecord::Schema.define(version: 20150730041232) do
t.datetime "updated_at", null: false
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|
t.string "name", limit: 3000, null: false
t.string "image", limit: 1000, null: false
......@@ -69,19 +75,30 @@ ActiveRecord::Schema.define(version: 20150730041232) do
t.datetime "updated_at", null: false
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|
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "email", 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.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
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.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
......
# 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