Commit d86ad8a5 by Hoang Phuc Do

Merge branch 'bk_dhp_solr' into dhp_solr

# Conflicts:
#	Gemfile
#	app/controllers/product_items_controller.rb
#	app/helpers/carts_helper.rb
#	app/services/cart.rb
parent ced8b073
......@@ -36,6 +36,8 @@ gem 'carrierwave', '~> 1.0'
gem 'mini_magick'
# bootstrap-sass is a Sass-powered version of Bootstrap 3
gem 'bootstrap-sass', '~> 3.3.6'
# Config helps you easily manage environment specific settings in an easy and usable manner.
gem 'config'
# Faker, a port of Data::Faker from Perl, is used to easily generate fake data: names, addresses, phone numbers, etc.
gem 'faker', '~> 1.6', '>= 1.6.3'
# Kaminari is a Scope & Engine based, clean, powerful, agnostic, customizable and sophisticated paginator for Rails 4+
......@@ -58,6 +60,8 @@ gem 'toastr-rails', '~> 1.0', '>= 1.0.3'
# (or more often navigation item)
# is selected based on the current page or other arbitrary condition
gem 'active_link_to', '~> 1.0', '>= 1.0.4'
# RSolr aims to provide a simple and extensible library for working with Solr
gem 'rsolr', '~> 2.0', '>= 2.0.2'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
......
class SearchController < ApplicationController
before_action :set_solr
before_action :set_search_request, 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
end
private
def set_solr
@solr = SolrSearch.new
end
def set_search_request
@solr.search_for(params[:q])
end
end
\ No newline at end of file
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
Product.find(product_ids)
end
end
\ No newline at end of file
......@@ -5,5 +5,19 @@ class SolrSearch
def initialize
@rsolr = RSolr.connect url: Settings.rsolr.address
@response = nil
end
def search_for(search_query)
search_query ||= ''
@response = rsolr.get 'select', params: { q: sanitize_query(search_query) }
end
def docs
@response['response']['docs']
end
def sanitize_query(query)
query.delete(':')
end
end
\ No newline at end of file
......@@ -36,9 +36,7 @@
</a>
</div>
</div>
<div class="header-search">
</div>
<%= render 'layouts/header/search_form' %>
</div>
</div>
</div>
......
<div class="header-search">
<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...' %>
<%= button_tag raw('<i class="fa fa-search"></i>'), class: 'btn btn-default' %>
</div>
<% end %>
</div>
\ No newline at end of file
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3">
<h2 class="h2 heading-primary mt-lg clearfix">
<span>Search result</span>
</h2>
<%= render partial: 'products/product_list', locals: { products: @products } %>
<div class="toolbar-bottom">
<div class="toolbar">
<div class="sorter">
<%= paginate @products, theme: 'bootstrap' %>
</div>
</div>
</div>
</div>
<div class="col-md-3 col-md-pull-9 sidebar shop-sidebar">
<%= render 'shared/sidebar' %>
</div>
</div>
</div>
......@@ -20,4 +20,5 @@ Rails.application.routes.draw do
get '/', to: 'users#show', as: 'user_profile'
get '/products', to: 'users#products', as: 'user_products'
end
get '/search', to: 'search#show', as: 'search_result'
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