Skip to content

Commit

Permalink
Add search algorithm dropdown
Browse files Browse the repository at this point in the history
Co-authored-by: Anna Headley <hackartisan@users.noreply.github.com>
Co-authored-by: Carolyn Cole <carolyncole@users.noreply.github.com>
Co-authored-by: James R. Griffin III <jrgriffiniii@users.noreply.github.com>
Co-authored-by: Trey Pendragon <tpendragon@users.noreply.github.com>

Implementing the Orangelight::DropdownHelpTextComponent to support help
text markup within the algorithm selection dropdown UI component

Co-authored-by: Anna Headley <hackartisan@users.noreply.github.com>
Co-authored-by: Eliot Jordan <eliotjordan@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 13, 2023
1 parent dedb0d0 commit 15554a8
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
@import 'components/browse';
@import 'components/buttons';
@import 'components/dropdown';
@import 'components/dropdown-help-text';
@import 'components/facets';
@import 'components/font-face';
@import 'components/footer';
Expand Down
5 changes: 5 additions & 0 deletions app/assets/stylesheets/components/dropdown-help-text.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dropdown-help-text {
margin-left: 10px;
font-size: 0.8em;
font-style: italic;
}
21 changes: 21 additions & 0 deletions app/components/orangelight/dropdown_help_text_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class Orangelight::DropdownHelpTextComponent < Blacklight::System::DropdownComponent
# options are { label:, value:, helptext: } hashes
def option_text_and_value(option)
[option[:label], option[:value]]
end

# Override to allow adding and styling a helptext for each option
def before_render
with_button(classes: 'btn btn-outline-secondary dropdown-toggle', label: button_label) unless button

return if options.any?

with_options(@choices.map do |option|
text = "<div>#{option[:label]}</div><div id='#{option[:value]}' class='dropdown-help-text'>#{option[:helptext]}</div>".html_safe
value = option[:value]
{ text:, url: helpers.url_for(@search_state.params_for_search(@param => value)), selected: @selected == value }
end)
end
end
1 change: 1 addition & 0 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ class CatalogController < ApplicationController
config.spell_max = 0

# Add bookmark all widget
config.add_results_collection_tool(:algorithm_select)
config.add_results_collection_tool(:bookmark_all)

config.add_results_document_tool(:bookmark, partial: 'bookmark_control')
Expand Down
8 changes: 8 additions & 0 deletions app/helpers/catalog_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def ejournals_path
Rails.application.routes.url_helpers.url_for controller: 'catalog', params:, only_path: true
end

def search_algorithm_value
if params[:search_algorithm] == "engineering"
"engineering"
else
"default"
end
end

private

def document_types(document)
Expand Down
14 changes: 14 additions & 0 deletions app/views/catalog/_algorithm_select.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if Flipflop.multi_algorithm? %>
<%= render(Orangelight::DropdownHelpTextComponent.new(
param: :search_algorithm,
choices: [
{ label: "default", value: "default", helptext: "documents with highest term frequency are first"},
{ label: "engineering", value: "engineering", helptext: "move documents about engineering to the top" }
],
id: 'search-algorithm-dropdown',
search_state: search_state,
default: 'default',
interpolation: :algorithm,
selected: search_algorithm_value))
%>
<% end %>
1 change: 1 addition & 0 deletions app/views/catalog/_sort_and_per_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<%= render partial: 'tools', locals: { document_list: @document_list } %>
<% end %>
<%= render partial: 'sort_widget' %>
<%= render partial: 'algorithm_select' %>
<%= render partial: 'per_page_widget' %>
<% unless bookmarks? %>
<%= render partial: 'bookmark_all' %>
Expand Down
4 changes: 3 additions & 1 deletion config/locales/blacklight.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ en:
try_borrow_direct: 'Try Borrow Direct'
show:
label: '%{label}'
search_algorithm:
label_html: 'Rank by %{algorithm}'
skip_links:
first_result: 'Skip to result %{start} of %{total}'
header_links:
Expand All @@ -78,7 +80,7 @@ en:
login:
netid_login_msg: 'Log in with netID'
netid_button: 'Log in with netID'
redirect_text: 'You will be redirected to the secure Princeton University Central Authentication Service (CAS) login.'
redirect_text: 'You will be redirected to the secure Princeton University Central Authentication Service () login.'
alma_login_msg: 'Log in with Alma Account (affiliates)'
barcode_netid: 'Please log in with netID.'
barcode_header: ''
Expand Down
39 changes: 39 additions & 0 deletions spec/system/search_algorithms_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'Selecting search algorithms for results', type: :system, js: false do
before do
stub_holding_locations
end

context 'with the search algorithms feature enabled' do
before do
allow(Flipflop).to receive(:multi_algorithm?).and_return(true)
end

it 'renders a select widget used to select the ordering algorithm' do
visit '/catalog?search_field=all_fields&q=roman'
expect(page).to have_text('1. Огонек : роман')

click_button('Rank by default')
within('#engineering.dropdown-help-text') do
expect(page).to have_text("move documents about engineering to the top")
end
click_link('engineering')
expect(page).to have_button('Rank by engineering')
expect(page).to have_text('1. Reconstructing the Vitruvian Scorpio: An Engineering Analysis')
end
end

context 'with the search algorithms feature disabled' do
before do
allow(Flipflop).to receive(:multi_algorithm?).and_return(false)
end

it 'does not render a select widget' do
visit '/catalog?search_field=all_fields&q=roman'
expect(page).not_to have_button('Rank by default')
end
end
end

0 comments on commit 15554a8

Please sign in to comment.