Skip to content

Commit

Permalink
Feature/apmw_62_locations_via_search (#142)
Browse files Browse the repository at this point in the history
* added Locations to search

* added test for location search

* fixed some tests

* rubocop

* refactoring
  • Loading branch information
joh-dah authored Jan 10, 2022
1 parent 25ecd98 commit db260ac
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
Binary file added app/assets/images/location.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 24 additions & 8 deletions app/controllers/search_results_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,32 @@ def search_result_params
params.require(:title, :link).permit(:description, :resource)
end

def add_search_results(rooms, buildings, locations, people)
add_buildings(buildings)
add_rooms(rooms)
add_locations(locations)
add_people(people)
end

def search_for_entries_starting_with(query)
buildings = Building.where("LOWER(name) LIKE ?", "#{query}%")
rooms = Room.where("LOWER(name) LIKE ?", "#{query}%")
locations = Location.where("LOWER(name) LIKE ?", "#{query}%")
people = Person.where("LOWER(first_name) || ' ' || LOWER(last_name) LIKE ?
OR LOWER(last_name) LIKE ?",
"#{query}%", "#{query}%")

add_buildings(buildings)
add_rooms(rooms)
add_people(people)
add_search_results(rooms, buildings, locations, people)
end

def search_for_entries_including(query)
buildings = Building.where("LOWER(name) LIKE ? AND NOT LOWER(name) LIKE ?", "%#{query}%", "#{query}%")
rooms = Room.where("LOWER(name) LIKE ? AND NOT LOWER(name) LIKE ?", "%#{query}%", "#{query}%")
locations = Location.where("LOWER(name) LIKE ? AND NOT LOWER(name) LIKE ?", "%#{query}%", "#{query}%")
people = Person.where("LOWER(first_name) || ' ' || LOWER(last_name) LIKE ?
AND NOT LOWER(first_name) || ' ' || LOWER(last_name) LIKE ?
AND NOT LOWER(last_name) LIKE ?",
"%#{query}%", "#{query}%", "#{query}%")

add_rooms(rooms)
add_buildings(buildings)
add_people(people)
add_search_results(rooms, buildings, locations, people)
end

def add_rooms(rooms)
Expand Down Expand Up @@ -93,6 +96,19 @@ def add_buildings(buildings)
end
end

def add_locations(locations)
locations.each do |location|
@search_results.append(SearchResult.new(
id: @result_id,
title: location.name,
link: location_path(location),
description: "Location",
type: "location"
))
@result_id += 1
end
end

def add_people(people)
people.each do |person|
@search_results.append(SearchResult.new(
Expand Down
2 changes: 2 additions & 0 deletions app/views/search_results/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<img src="<%=(url_for('/assets/person.png'))%>" height="25" width="25" alt="user_picture">
<% elsif search_result.type == "building" %>
<img src="<%=(url_for('/assets/building.png'))%>" height="25" width="25" alt="building_picture">
<% elsif search_result.type == "location" %>
<img src="<%=(url_for('/assets/location.png'))%>" height="25" width="25" alt="location_picture">
<% elsif search_result.type == "room" %>
<img src="<%=(url_for('/assets/room.png'))%>" height="25" width="25" alt="room_picture">
<% end %>
Expand Down
32 changes: 32 additions & 0 deletions spec/features/search_result_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
@abc_room = FactoryBot.create :room, name: "ABC Room"
@room_xyz = FactoryBot.create :room, name: "Room X.YZ"

@bank = FactoryBot.create :location, name: "Bank", details: "bank-details"
@pavillon = FactoryBot.create :location, name: "Pavillon", details: "pavillon-details"
@kocktail_bar = FactoryBot.create :location, name: "Kocktail Bar", details: "kocktail bar-details"

@curie = FactoryBot.create :person, first_name: "Marie", last_name: "Curie"
@riemann = FactoryBot.create :person, first_name: "Bernhard", last_name: "Riemann"
@bernoulli = FactoryBot.create :person, first_name: "Daniel", last_name: "Bernoulli"
Expand Down Expand Up @@ -45,6 +49,34 @@
expect(page.body.index(@building_abc.name)).to be < page.body.index(@abc_building.name)
end

it "shows locations matching the query" do
visit search_results_path(query: "vill")
expect(page).to have_link(@pavillon.name, href: location_path(@pavillon), count: 1)

visit search_results_path(query: "Ban")
expect(page).to have_link(@bank.name, href: location_path(@bank), count: 1)
end

it "does not show locations not matching the query" do
visit search_results_path(query: "Ban")
expect(page).not_to have_text(@pavillon.name)
expect(page).not_to have_link(href: location_path(@pavillon))
expect(page).not_to have_text(@kocktail_bar.name)
expect(page).not_to have_link(href: location_path(@kocktail_bar))

visit search_results_path(query: "vill")
expect(page).not_to have_text(@bank.name)
expect(page).not_to have_link(href: location_path(@bank))
end

it "lists locations starting with the query before other found locations" do
visit search_results_path(query: "ba")
expect(page.body.index(@bank.name)).to be < page.body.index(@kocktail_bar.name)

visit search_results_path(query: "k")
expect(page.body.index(@kocktail_bar.name)).to be < page.body.index(@bank.name)
end

it "shows rooms matching the query" do
visit search_results_path(query: "x.yz")
expect(page).to have_link(@room_xyz.name, href: room_path(@room_xyz), count: 1)
Expand Down

0 comments on commit db260ac

Please sign in to comment.