Skip to content

Commit

Permalink
Add full-text search and restyle search
Browse files Browse the repository at this point in the history
- Search for keywords in groups' name, location and description
- Do not show search bar in home page
- Use only one form field for search and restyle search bar
- Extract groups index into a partial:
  The layout code was the same in 'groups/index' and 'searches/show'.
  • Loading branch information
lujanfernaud committed May 4, 2018
1 parent dc98fba commit 5c092db
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 139 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gem 'devise', '~> 4.4', '>= 4.4.3'
gem 'pundit', '~> 1.1'
gem 'rolify', '~> 5.2'
gem 'sucker_punch', '~> 2.0', '>= 2.0.4'
gem 'pg_search', '~> 2.1', '>= 2.1.2'
gem 'figaro', '~> 1.1', '>= 1.1.1'
gem 'gravatar_image_tag', '~> 1.2'
gem 'inline_svg', '~> 1.3', '>= 1.3.1'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ GEM
parser (2.5.1.0)
ast (~> 2.4.0)
pg (0.21.0)
pg_search (2.1.2)
activerecord (>= 4.2)
activesupport (>= 4.2)
arel (>= 6)
popper_js (1.12.9)
powerpack (0.1.1)
pry (0.11.3)
Expand Down Expand Up @@ -529,6 +533,7 @@ DEPENDENCIES
mini_magick (= 4.7.0)
minitest-reporters (= 1.1.14)
pg (~> 0.18)
pg_search (~> 2.1, >= 2.1.2)
pry-byebug (~> 3.6)
pry-rails (~> 0.3.6)
puma (~> 3.7)
Expand Down
40 changes: 5 additions & 35 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -201,42 +201,12 @@ body {
background-color: lighten($dark-indigo, 5);
}

.search-form {
flex-direction: column;
align-items: flex-end;
.container-search-bar {
padding: 0;

@include media-breakpoint-up(md) {
flex-direction: row;
align-items: center;
}
}

.search-group {
display: flex;
align-items: baseline;
margin-right: 0;
margin-bottom: .5rem !important;

@include media-breakpoint-up(md) {
margin-right: .5rem;
margin-bottom: 0 !important;
}
}

.search-label {
margin-right: .5rem;
}

.search-input {
width: initial;
}

.btn-search {
width: 100%;
margin: 0;

@include media-breakpoint-up(md) {
width: initial;
@include media-breakpoint-up(sm) {
padding-left: 15px;
padding-right: 15px;
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class SearchesController < ApplicationController
require "will_paginate/array"

def show
@groups = Group.search(params[:location], params[:group])
@groups = Group.unhidden
.search(params[:keywords])
.paginate(page: params[:page], per_page: 15)
end
end
8 changes: 4 additions & 4 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Group < ApplicationRecord
include PgSearch

resourcify

after_create :add_owner_as_organizer
after_update :update_members_role

Expand All @@ -22,10 +25,7 @@ class Group < ApplicationRecord
validates :description, presence: true, length: { minimum: 70 }
validates :image, presence: true

scope :search, -> (location, group_name) {
where("lower(location) LIKE :location AND lower(name) LIKE :name",
location: "%#{location.downcase}%", name: "%#{group_name.downcase}%")
}
pg_search_scope :search, against: [:name, :location, :description]

scope :unhidden, -> {
where(hidden: false, sample_group: false)
Expand Down
24 changes: 24 additions & 0 deletions app/views/groups/_groups.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="container">
<div class="row">
<div class="col-md-12 mt-2rem">

<header>
<h2 class="text-center">
<%= yield :groups_header_title %>
</h2>
</header>

<main class="row d-flex justify-content-center">
<%= render groups %>
</main>

<footer class="row">
<div class="mx-auto mt-2rem mb-2rem">
<%= will_paginate groups,
:renderer => WillPaginate::ActionView::Bootstrap4LinkRenderer %>
</div>
</footer>

</div>
</div>
</div>
20 changes: 4 additions & 16 deletions app/views/groups/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
<%= render "searches/search_bar" %>

<div class="container">
<div class="row">
<div class="col-md-12 mt-4">
<h2 class="text-center">Unhidden Groups</h2>
<% content_for :groups_header_title do %>
Unhidden Groups
<% end %>

<div class="row d-flex justify-content-center">
<%= render @groups %>
</div>
<div class="row">
<div class="mx-auto mt-2rem mb-2rem">
<%= will_paginate @groups,
:renderer => WillPaginate::ActionView::Bootstrap4LinkRenderer %>
</div>
</div>
</div>
</div>
</div>
<%= render "groups", groups: @groups %>
1 change: 0 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<body class="d-flex flex-column bg-light">
<%= render "header" %>
<%= render "searches/search_bar" if home_page? %>
<%= render "flash_messages" %>

<main class="main-container">
Expand Down
32 changes: 17 additions & 15 deletions app/views/searches/_search_bar.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<div class="container-fluid search-bar">
<div class="container d-flex justify-content-center">
<div class="row">
<div class="col">
<%= form_tag search_path, method: :get,
class: "form-inline search-form" do %>
<div class="form-group search-group">
<%= label_tag :location, nil, class: "search-label" %>
<%= text_field_tag :location, params[:location],
class: "form-control search-input" %>
</div>
<div class="container container-search-bar">

<div class="row justify-content-center">
<div class="col-md-6 col-lg-6 col-xl-4">

<%= form_tag search_path, method: :get, class: "form-inline" do %>

<%= label_tag :keywords, nil, hidden: true %>

<div class="form-group search-group">
<%= label_tag :group, nil, class: "search-label" %>
<%= text_field_tag :group, params[:group],
class: "form-control search-input" %>
<div class="input-group w-100">
<%= text_field_tag :keywords, params[:keywords],
class: "form-control", aria: { label: "Search" } %>

<div class="input-group-append">
<%= submit_tag "Search", name: nil, class: "btn" %>
</div>
</div>

<%= submit_tag "Search", name: nil, class: "btn btn-search" %>
<% end %>

</div>
</div>

</div>
</div>
24 changes: 5 additions & 19 deletions app/views/searches/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
<%= render "searches/search_bar" %>

<div class="container">
<div class="row">
<div class="col-md-12 mt-4">
<h2 class="text-center">
<%= @groups.count %>
<%= "group".pluralize(@groups.count) %> found
</h2>
<div class="row">
<%= render @groups %>
</div>
<div class="row">
<div class="mx-auto mt-2rem mb-2rem">
<%= will_paginate @groups,
:renderer => WillPaginate::ActionView::Bootstrap4LinkRenderer %>
</div>
</div>
</div>
</div>
</div>
<% content_for :groups_header_title do %>
<%= "#{@groups.count} #{'group'.pluralize(@groups.count)}" %> found
<% end %>

<%= render "groups/groups", groups: @groups %>

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
8 changes: 4 additions & 4 deletions test/fixtures/groups.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ one:
two:
name: Penny's Group
location: Portland, Oregon
description: <%= Faker::Lorem.paragraph %>
description: <%= Faker::Lorem.paragraph + " Nike" %>
image: sample.jpeg
hidden: false
all_members_can_create_events: true
Expand All @@ -30,7 +30,7 @@ three:
four:
name: Woodell's Hidden Group
location: Portland, Oregon
description: <%= Faker::Lorem.paragraph %>
description: <%= Faker::Lorem.paragraph + " Nike" %>
image: sample.jpeg
hidden: true
all_members_can_create_events: true
Expand All @@ -39,15 +39,15 @@ four:
woodells_group:
name: Woodell's Group
location: Portland, Oregon
description: <%= Faker::Lorem.paragraph %>
description: <%= Faker::Lorem.paragraph + " Nike" %>
image: sample.jpeg
hidden: false
all_members_can_create_events: true
owner: woodell

strangers_group:
name: Stranger's Group
location: Portland, Oregon
location: Somewhere
description: <%= Faker::Lorem.paragraph %>
image: sample.jpeg
hidden: true
Expand Down
70 changes: 27 additions & 43 deletions test/integration/groups/groups_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,58 @@ def setup
@user = users(:penny)
end

test "user can search for a group using only the location" do
test "search using location as keyword" do
log_in_as(@user)
visit root_path
visit groups_path

search_using_only_location "Portland"
assert page.has_content? "6 groups found"

search_using_only_location "Kyoto"
assert page.has_content? "1 group found"
search "Portland"
assert page.has_content? "3 groups found"
end

test "user can search for a group using only the group's name" do
test "search using name as keyword" do
log_in_as(@user)
visit root_path

search_using_only_group "Nike"
assert page.has_content? "1 group found"
visit groups_path

search_using_only_group "Stranger's Group"
search "Sakura"
assert page.has_content? "1 group found"
end

test "user can search for a group using the location and group's name" do
test "search using name and description as keyword" do
log_in_as(@user)
visit root_path

search location: "Kyoto", group: "Sakura"
assert page.has_content? "1 group found"
visit groups_path

search location: "Portland", group: "Woodell's Group"
assert page.has_content? "1 group found"
search "Nike"
assert page.has_content? "3 groups found"
end

test "user can search for a group using a partial string" do
test "search using two keywords" do
log_in_as(@user)
visit root_path
visit groups_path

search location: "Port", group: "Gr"
assert page.has_content? "5 groups found"

search location: "Kyo", group: ""
assert page.has_content? "1 group found"
search "Nike Portland"
assert page.has_content? "3 groups found"
end

test "user can search in a different case" do
test "search using three keywords" do
log_in_as(@user)
visit root_path
visit groups_path

search location: "", group: "sakura"
search "Nike Portland Penny"
assert page.has_content? "1 group found"

search location: "portland", group: ""
assert page.has_content? "6 groups found"
end

private
test "search for hidden group returns nothing" do
log_in_as(@user)
visit groups_path

def search_using_only_location(location)
search location: location, group: ""
end
search "Stranger's Group"
assert page.has_content? "0 groups found"
end

def search_using_only_group(group)
search location: "", group: group
end
private

def search(location:, group:)
fill_in "Location", with: location
fill_in "Group", with: group
def search(keywords)
fill_in "keywords", with: keywords
click_on "Search"
end
end

0 comments on commit 5c092db

Please sign in to comment.