Skip to content

Commit

Permalink
Search feature (#143)
Browse files Browse the repository at this point in the history
* Adds search box to welcome page

Co-authored-by: Bess Sadler <bess@users.noreply.github.com>

* Returning results past first page

Co-authored-by: Bess Sadler <bess@users.noreply.github.com>
Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>

---------

Co-authored-by: Bess Sadler <bess@users.noreply.github.com>
Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 7, 2023
1 parent 03523d4 commit 3165bd0
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/guide_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def index
@guide_cards = GuideCard.page(params[:page])
end

def search
@exact_match = GuideCard.find_by(heading: params[:search_term])
@guide_cards = GuideCard.page(@exact_match.index_page)
end

def show
@guide_card = GuideCard.find(params[:id])
@sub_guide_cards = @guide_card.children
Expand Down
5 changes: 5 additions & 0 deletions app/models/guide_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
class GuideCard < ApplicationRecord
paginates_per 10
include HasChildren

def index_page
number_to_check = id - 1
(number_to_check / 10) + 1
end
end
13 changes: 13 additions & 0 deletions app/views/guide_cards/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1 id="list_header"> GuideCard Index </h1>

<% @guide_cards.each do |card| %>
<h2>
<% if @exact_match.id == card.id %>
<%= link_to "* #{card.heading}", guide_card_path(card.id) %>
<% else %>
<%= link_to card.heading, guide_card_path(card.id) %>
<% end %>
</h2>
<% end %>
<%= paginate @guide_cards %>
7 changes: 6 additions & 1 deletion app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ Please enter a search string to go to the guide card nearest that search string.

<p> GuideCards Link (Temporary): <%= link_to("GuideCards index", guide_cards_path) %></p>

<p> Search box will go here </p>
<p> Please enter a search string to go to the guide card nearest that search string. </p>

<%= form_tag "/search", method: "GET" do %>
<%= text_field_tag :search_term, params[:search_term] %>
<%= submit_tag "Go" %>
<% end %>

<p><%= link_to("Filing rules", "https://imagecat2.princeton.edu/ecc/cardhelp.html") %></p>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Rails.application.routes.draw do
root 'welcome#index'
get '/welcome/index'
get '/search/', to: 'guide_cards#search'
get 'guide_cards/', to: 'guide_cards#index'
get 'guide_cards/:id', to: 'guide_cards#show'
resources :guide_cards
Expand Down
30 changes: 30 additions & 0 deletions spec/fixtures/guide_card_search_fixture.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ID,heading,sortid,path,ID,heading,sortid,path
1,*****,1.5,14/0001/A1100,1,*****,1.5,14/0001/A1100
2,A,2.5,14/0001/B4491,2,A,2.5,14/0001/B4491
3,AALAS,3.5,14/0001/A1002,3,AALAS,3.5,14/0001/A1002
4,A.B.C.,4.5,14/0001/A1003,4,A.B.C.,4.5,14/0001/A1003
5,A.C.I.,5.5,14/0001/A1004,5,A.C.I.,5.5,14/0001/A1004
6,A-D,6.5,14/0001/A1005,6,A-D,6.5,14/0001/A1005
7,AGPA,7.5,14/0001/A1006,7,AGPA,7.5,14/0001/A1006
8,AID,8.5,14/0001/A1007,8,AID,8.5,14/0001/A1007
9,AILA,9.5,14/0002/A1008,9,AILA,9.5,14/0002/A1008
10,A.L.,10.5,14/0002/A1009,10,A.L.,10.5,14/0002/A1009
11,A.M.,11.5,14/0002/A1010,11,A.M.,11.5,14/0002/A1010
12,APL,12.5,14/0002/A1011,12,APL,12.5,14/0002/A1011
13,ARS,13.5,14/0002/A1012,13,ARS,13.5,14/0002/A1012
14,ASM,14.5,14/0002/A1013,14,ASM,14.5,14/0002/A1013
15,A.T.,15.5,14/0002/A1014,15,A.T.,15.5,14/0002/A1014
16,Aa,16.5,21/0003/A1015,16,Aa,16.5,21/0003/A1015
17,Aagaard,17.5,21/0003/A1016,17,Aagaard,17.5,21/0003/A1016
18,Aalund,18.5,21/0003/A1017,18,Aalund,18.5,21/0003/A1017
19,Aaron,19.5,21/0003/A1018,19,Aaron,19.5,21/0003/A1018
20,Aaronson,20.5,21/0003/A1019,20,Aaronson,20.5,21/0003/A1019
21,Abadan,21.5,14/0004/A1020,21,Abadan,21.5,14/0004/A1020
22,"Abailard, Pierre",22.5,sub,22,"Abailard, Pierre",22.5,sub
23,"Abailard, Pierre (Subject)",23.5,14/0004/A1026,23,"Abailard, Pierre (Subject)",23.5,14/0004/A1026
24,Abailard et Eloise,24.5,14/0004/A1047,24,Abailard et Eloise,24.5,14/0004/A1047
25,Abandoned,25.5,14/0004/A1022,25,Abandoned,25.5,14/0004/A1022
26,Abaza,26.5,14/0004/A1027,26,Abaza,26.5,14/0004/A1027
27,Abb,27.5,14/0004/A1028,27,Abb,27.5,14/0004/A1028
28,Abbas,28.5,14/0004/A1029,28,Abbas,28.5,14/0004/A1029
29,Abbasids,29.5,14/0004/A1030,29,Abbasids,29.5,14/0004/A1030
19 changes: 19 additions & 0 deletions spec/models/guide_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,23 @@
end
end
end

context 'knows what page of the index it should be on' do
it 'id 2 is on the first page' do
guide_card2 = GuideCard.create(id: 2)
expect(guide_card2.index_page).to eq 1
end
it 'id 11 is on the second page' do
guide_card11 = GuideCard.create(id: 11)
expect(guide_card11.index_page).to eq 2
end
it 'id 30 is on the third page' do
guide_card30 = GuideCard.create(id: 30)
expect(guide_card30.index_page).to eq 3
end
it 'id 19 is on the second page' do
guide_card19 = GuideCard.create(id: 19)
expect(guide_card19.index_page).to eq 2
end
end
end
19 changes: 19 additions & 0 deletions spec/system/search_feature_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Search Feature', type: :system, js: true do
let(:guide_card_fixture) { Rails.root.join('spec', 'fixtures', 'guide_card_search_fixture.csv') }
before do
GuideCardLoadingService.new(csv_location: guide_card_fixture).import
end

describe 'search bar on front page' do
it 'returns an index of GuideCards with search term indicated' do
visit '/'
fill_in 'search_term', with: 'Aaron'
click_on 'Go'
expect(page).to have_link('* Aaron')
end
end
end

0 comments on commit 3165bd0

Please sign in to comment.