Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VietTH-VenShop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
4
Merge Requests
4
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tran Hoang Viet
VietTH-VenShop
Commits
c4a3614e
Commit
c4a3614e
authored
Jul 08, 2015
by
Tran Hoang Viet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
VietTH: Implement feature search
parent
d570482e
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
79 additions
and
4 deletions
+79
-4
Gemfile
+3
-0
Gemfile.lock
+6
-0
app/controllers/products_controller.rb
+5
-0
app/models/product.rb
+4
-0
app/models/solr.rb
+40
-0
app/views/layouts/_search.html.haml
+4
-4
app/views/products/search.html.haml
+10
-0
config/application.yml
+3
-0
config/routes.rb
+4
-0
No files found.
Gemfile
View file @
c4a3614e
...
@@ -70,3 +70,5 @@ gem 'sidekiq', '~> 3.4.1'
...
@@ -70,3 +70,5 @@ gem 'sidekiq', '~> 3.4.1'
# pagination
# pagination
gem
'
kaminari
'
,
'~> 0.16.3'
gem
'
kaminari
'
,
'~> 0.16.3'
gem
'
rsolr
'
,
'~> 1.0.12'
\ No newline at end of file
Gemfile.lock
View file @
c4a3614e
...
@@ -177,6 +177,8 @@ GEM
...
@@ -177,6 +177,8 @@ GEM
request_store (1.1.0)
request_store (1.1.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)
ruby_parser (3.7.0)
ruby_parser (3.7.0)
sexp_processor (~> 4.1)
sexp_processor (~> 4.1)
sass (3.4.15)
sass (3.4.15)
...
@@ -259,6 +261,7 @@ DEPENDENCIES
...
@@ -259,6 +261,7 @@ DEPENDENCIES
pry-rails (~> 0.3.4)
pry-rails (~> 0.3.4)
quiet_assets
quiet_assets
rails (= 4.2.3)
rails (= 4.2.3)
rsolr (~> 1.0.12)
sass-rails (~> 5.0)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
sdoc (~> 0.4.0)
settingslogic (~> 2.0.9)
settingslogic (~> 2.0.9)
...
@@ -270,3 +273,6 @@ DEPENDENCIES
...
@@ -270,3 +273,6 @@ DEPENDENCIES
unicorn
unicorn
vacuum (~> 1.3.0)
vacuum (~> 1.3.0)
web-console (~> 2.0)
web-console (~> 2.0)
BUNDLED WITH
1.10.5
app/controllers/products_controller.rb
View file @
c4a3614e
...
@@ -30,6 +30,11 @@ class ProductsController < ApplicationController
...
@@ -30,6 +30,11 @@ class ProductsController < ApplicationController
redirect_to
@product
redirect_to
@product
end
end
def
search
query
=
params
[
:query
].
present?
?
{
title:
params
[:
query
]}
:
'*'
@products
=
Product
.
search
(
query
,
page:
params
[
:page
])
end
private
private
def
set_product
def
set_product
@product
=
Product
.
find_by
(
id:
params
[
:id
])
@product
=
Product
.
find_by
(
id:
params
[
:id
])
...
...
app/models/product.rb
View file @
c4a3614e
class
Product
<
ActiveRecord
::
Base
class
Product
<
ActiveRecord
::
Base
include
Solr
# scopes
# scopes
scope
:recommended
,
->
{
self
.
offset
(
rand
(
self
.
count
)).
limit
(
Settings
.
limit_product_recommended
)
}
scope
:recommended
,
->
{
self
.
offset
(
rand
(
self
.
count
)).
limit
(
Settings
.
limit_product_recommended
)
}
scope
:newest
,
->
{
self
.
order
(
created_at: :desc
).
limit
(
Settings
.
limit_product_newest
)
}
scope
:newest
,
->
{
self
.
order
(
created_at: :desc
).
limit
(
Settings
.
limit_product_newest
)
}
...
@@ -14,4 +16,6 @@ class Product < ActiveRecord::Base
...
@@ -14,4 +16,6 @@ class Product < ActiveRecord::Base
enum
product_type:
%i(system amazon)
enum
product_type:
%i(system amazon)
mount_uploader
:image
,
ImageUploader
mount_uploader
:image
,
ImageUploader
solr_options
per_page:
2
end
end
app/models/solr.rb
0 → 100644
View file @
c4a3614e
module
Solr
extend
ActiveSupport
::
Concern
module
ClassMethods
attr_accessor
:solr_per_page
,
:solr_page
,
:solr_total_count
def
solr_options
(
fields
=
{})
@solr_per_page
=
fields
[
:per_page
]
||
5
end
def
search
(
query
,
options
=
{})
return
unless
query
.
is_a?
(
Hash
)
@solr_page
=
options
[
:page
]
||
1
query
=
query
.
inject
(
''
)
{
|
str
,
item
|
str
<<
"
#{
item
.
first
}
:
#{
item
.
last
}
"
<<
' '
}
results
=
rsolr
.
paginate
(
solr_page
,
solr_per_page
,
'select'
,
params:
{
q:
query
,
fl:
self
.
primary_key
}
)
@solr_total_count
=
results
[
'response'
][
'numFound'
].
to_i
parse
(
results
[
'response'
])
end
def
parse
(
results
)
ids
=
results
[
'docs'
].
map
{
|
item
|
item
[
self
.
primary_key
].
to_i
}
self
.
where
(
self
.
primary_key
=>
ids
)
end
def
rsolr
@rsolr_service
||=
RSolr
.
connect
(
url:
ENV
[
'SOLR_URL'
])
end
def
kaminari_paginate
Kaminari
.
paginate_array
([],
total_count:
solr_total_count
).
page
(
solr_page
).
per
(
solr_per_page
)
end
end
end
\ No newline at end of file
app/views/layouts/_search.html.haml
View file @
c4a3614e
.input-group
=
form_tag
search_products_path
,
method: :get
do
%input
.form-control
{
placeholder:
"Search for..."
}
.input-group
=
text_field_tag
:query
,
params
[
:query
],
class:
'form-control'
,
placeholder:
'Search for title...'
%span
.input-group-btn
%span
.input-group-btn
%button
.btn.btn-default
{
type:
"button"
}
Go!
%button
.btn.btn-default
{
type:
"submit"
}
Go!
\ No newline at end of file
app/views/products/search.html.haml
0 → 100644
View file @
c4a3614e
%h3
.title
Search result for:
=
params
[
:query
]
.module
-
if
@products
.
present?
#category-items
.products-list-detail
=
render
@products
.pagination
=
paginate
@products
.
kaminari_paginate
\ No newline at end of file
config/application.yml
View file @
c4a3614e
...
@@ -2,3 +2,5 @@
...
@@ -2,3 +2,5 @@
AWS_ACCESS
:
AKIAJ77C4CTZOP7TUVWQ
AWS_ACCESS
:
AKIAJ77C4CTZOP7TUVWQ
AWS_SECRET
:
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
AWS_SECRET
:
cYJYb/MLGV0M6oi1+DjlliL1cfxmh78tKXnT6ZmX
AWS_TAG
:
zigexn6400-22
AWS_TAG
:
zigexn6400-22
SOLR_URL
:
http://localhost:8080/solr/venshop
\ No newline at end of file
config/routes.rb
View file @
c4a3614e
...
@@ -6,6 +6,10 @@ Rails.application.routes.draw do
...
@@ -6,6 +6,10 @@ Rails.application.routes.draw do
member
do
member
do
get
:add_cart
get
:add_cart
end
end
collection
do
get
:search
end
end
end
resources
:categories
resources
:categories
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment