Commit acbd7d6e by Hoang Phuc Do

Search by Solr feature

parents d86ad8a5 c4452af3
(function($) {
// Vertical Spinner - Touchspin - Product Details Quantity input
if ( $.fn.TouchSpin ) {
$('#product_quantity').TouchSpin({
$('.vertical-spinner').TouchSpin({
verticalbuttons: true
});
$('.qty-input').TouchSpin();
......
(function($) {
toastr.options.closeButton = true;
// Search Dropdown Toggle
$('.search-toggle').on('click', function (e) {
$('.header-search-wrapper').toggleClass('open');
e.preventDefault();
});
}).apply(this, [jQuery]);
\ No newline at end of file
......@@ -92,15 +92,6 @@ html .btn-primary:active:focus {
#header .header-logo img {
margin: 0 24px 0 0;
}
#header .header-container {
padding-top: 28px;
padding-bottom: 28px;
position: relative;
display: table;
}
#header .header-container.header-nav {
padding: 0;
}
#header .cart-area {
float: right;
vertical-align: middle;
......@@ -171,7 +162,6 @@ header .header-search {
position: relative;
width: 100%;
min-width: 250px;
padding-right: 170px;
background-color: #fff;
}
#header .header-search .header-search-wrapper.open {
......@@ -278,6 +268,9 @@ header .header-search {
color: #000;
background-color: transparent;
}
#header .header-nav-main nav > ul > li > a {
border-radius: 0;
}
.panel-default>.panel-heading {
color: #333;
background-color: #f5f5f5;
......
class SearchController < ApplicationController
before_action :set_solr
before_action :set_search_request, only: :show
before_action :set_search_result, only: :show
# GET /search
def show
products = Search.matched_products(@solr.docs)
if products.is_a?(Array)
@products = Kaminari.paginate_array(products).page(params[:page]).per(10)
else
@products = products.page(params[:page]).per(10)
end
@products = if @search_result.is_a?(Array)
Kaminari.paginate_array(@search_result).page(params[:page]).per(5)
else
@search_result.page(params[:page]).per(5)
end
end
private
def set_solr
@solr = SolrSearch.new
end
def set_search_request
@solr.search_for(params[:q])
def set_search_result
@search_result = if params[:q].present?
Search.new(params[:q]).products
else
Product.all
end
end
end
\ No newline at end of file
class StaticPagesController < ApplicationController
def index
@latest_products = Product.page(params[:page]).per(10)
@latest_products = Product.page(params[:page]).per(5)
# @TODO: Get recommended products
@recommended_products = Product.last(6)
# session.clear
......
module SearchHelper
def render_search_result(search_result)
if search_result.blank?
render html: 'Sorry! There are no products matched your query'
else
render(partial: 'products/product_list',
locals: { products: search_result })
end
end
end
\ No newline at end of file
......@@ -16,7 +16,7 @@ class Cart
real_quantity = product_quantity(product_id, quantity)
return false unless product_is_in_stock?(product_id, real_quantity)
if product_item_exist?(product_id)
@cart[product_id.to_s][:quantity.to_s] += real_quantity.to_i
@cart[product_id.to_s][:quantity.to_s] = real_quantity.to_i
else
@cart[product_id.to_s] = { quantity: real_quantity.to_i }
end
......
class Search
def self.matched_products(array)
return Product.all if array.empty?
product_ids = []
array.each do |product|
product_ids << product[:id.to_s]
end
def initialize(search_query)
@solr = SolrSearch.new
@search_query = search_query
end
def products
@solr.search_for(@search_query)
filter_products(@solr.docs)
end
private
def filter_products(products)
product_ids = products.map { |product| product[:id.to_s] }
Product.find(product_ids)
end
end
\ No newline at end of file
......@@ -18,6 +18,6 @@ class SolrSearch
end
def sanitize_query(query)
query.delete(':')
query.gsub(/[^0-9A-Za-z]/, '')
end
end
\ No newline at end of file
......@@ -5,7 +5,7 @@
<td class="product-image-td"></td>
<td class="product-name-td">
<h2 class="product-name">
<%= link_to product_item[:product].title, product_item_url(product_item[:product]) %>
<%= link_to product_item[:product].title, product_url(product_item[:product]) %>
</h2>
</td>
<td><%= number_to_currency(product_item[:product].price) %></td>
......
......@@ -41,8 +41,6 @@
</div>
</div>
</div>
<div class="header-container header-nav">
</div>
<%= render 'layouts/header/main_nav' %>
</div>
</header>
\ No newline at end of file
......@@ -5,6 +5,7 @@
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_link_tag 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800%7CShadows+Into+Light' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
......
<div class="header-container header-nav">
<div class="container">
<div class="header-nav-main">
<nav>
<ul id="mainNav" class="nav nav-pills">
<%= active_link_to 'Home', root_url, wrap_tag: :li, active: :exclusive %>
</ul>
</nav>
</div>
</div>
</div>
\ No newline at end of file
......@@ -2,7 +2,7 @@
<a href="#" class="search-toggle"><i class="fa fa-search"></i></a>
<%= form_tag search_result_path, method: :get do %>
<div class="header-search-wrapper">
<%= text_field_tag :q, '', class: 'form-control', placeholder: 'Search...' %>
<%= text_field_tag :q, params[:q], class: 'form-control', placeholder: 'Search...' %>
<%= button_tag raw('<i class="fa fa-search"></i>'), class: 'btn btn-default' %>
</div>
<% end %>
......
......@@ -26,7 +26,7 @@
<div class="col-md-12">
<div class="form-group">
<%= f.label :quantity %>
<%= f.number_field :quantity, class: "form-control" %>
<%= f.number_field :quantity, class: "form-control vertical-spinner" %>
</div>
</div>
......
......@@ -30,7 +30,7 @@
<div class="product-detail-qty">
<%= number_field_tag :product_quantity, 1, min: 0, class: 'vertical-spinner' %>
</div>
<%= submit_tag 'Add to cart', class: 'addtocart' %>
<%= button_tag raw(fa_icon('shopping-cart', text: 'Add to cart')), class: 'addtocart' %>
<% end %>
</div>
</div>
......
<div class="product-actions">
<%= form_tag cart_add_product_item_path(product.id), remote: true do %>
<%= hidden_field_tag :product_quantity, 1 %>
<%= button_tag fa_icon('shopping-cart', text: 'Add to cart'), class: 'addtocart' %>
<% end %>
</div>
\ No newline at end of file
......@@ -12,6 +12,7 @@
<div class="product-price-box">
<span class="product-price"><%= number_to_currency(product.price) %></span>
</div>
<%= render partial: 'products/product_action', locals: { product: product } %>
</div>
</div>
</li>
......
......@@ -11,11 +11,12 @@
<%= link_to fa_icon('pencil'), edit_product_path(product) if product.belongs_to_user?(current_user) %>
</h2>
<div class="product-short-desc">
<%= product.description %>
<%= truncate(product.description, length: 300) %>
</div>
<div class="product-price-box">
<span class="product-price"><%= number_to_currency(product.price) %></span>
</div>
<%= render partial: 'products/product_action', locals: { product: product } %>
</div>
</div>
</li>
......
......@@ -4,7 +4,7 @@
<h2 class="h2 heading-primary mt-lg clearfix">
<span>Search result</span>
</h2>
<%= render partial: 'products/product_list', locals: { products: @products } %>
<%= render_search_result(@products) %>
<div class="toolbar-bottom">
<div class="toolbar">
......
<tr>
<td><%= link_to "##{product.id}", edit_product_path(product) %></td>
<td class="product-name-td">
<h2 class="product-name">
<%= link_to product.title, product_url(product) %>
</h2>
</td>
<td><%= number_to_currency(product.price) %></td>
<td><%= product.quantity %></td>
<td><%= link_to fa_icon('pencil'), edit_product_path(product) %></td>
</tr>
\ No newline at end of file
<table class="cart-table">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Unit Price</th>
<th>Qty</th>
<th></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<%= render partial: 'product', locals: { product: product } %>
<% end %>
</tbody>
</table>
\ No newline at end of file
......@@ -4,7 +4,7 @@
<h2 class="h2 heading-primary font-weight-normal">
My Products
</h2>
<%= render partial: 'products/product_list', locals: { products: @products } %>
<%= render 'product_table' %>
<div class="toolbar-bottom">
<div class="toolbar">
<div class="sorter">
......
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