Skip to content

Commit

Permalink
Adds search feature for non-exact match
Browse files Browse the repository at this point in the history
Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>
  • Loading branch information
leefaisonr and sandbergja committed Sep 7, 2023
1 parent 3165bd0 commit 9574e94
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/guide_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index

def search
@exact_match = GuideCard.find_by(heading: params[:search_term])
@exact_match = GuideCard.where('heading < ?', 'Aarons').limit(1).order(heading: :desc).first if @exact_match.nil?
@guide_cards = GuideCard.page(@exact_match.index_page)
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/guide_cards/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<% @guide_cards.each do |card| %>
<h2>
<% if @exact_match.id == card.id %>
<% 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) %>
Expand Down
6 changes: 6 additions & 0 deletions spec/system/search_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
click_on 'Go'
expect(page).to have_link('* Aaron')
end
it 'finds the closest match if there is no exact match' do
visit '/'
fill_in 'search_term', with: 'Aarons'
click_on 'Go'
expect(page).to have_link('* Aaron')
end
end
end

0 comments on commit 9574e94

Please sign in to comment.