Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daccess-291: advanced search carryover #2196

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions blacklight-cornell/app/assets/javascripts/search_form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
document.addEventListener("DOMContentLoaded", function () {
const advancedSearchLink = document.getElementById("advanced-search-link");
// Select all inputs, selects, and textareas if no dynamic fields are found
let formFields = document.querySelectorAll("[data-dynamic='true']");
if (formFields.length === 0) {
formFields = document.querySelectorAll("input, select, textarea");
}

if (advancedSearchLink && formFields) {
// Function to update the Advanced Search link dynamically
const updateAdvancedSearchLink = () => {
let params = new URLSearchParams();

formFields.forEach((field) => {
if (field.name && field.value) {
// Mapping for browse-specific parameters
const fieldMappings = {
authq: "q",
browse_type: "search_field",
search_field: "search_field"
};

const searchValueMappings = {
author: "author",
author_browse: "author",
at_browse: "author",
"catalog:author": "author",
Author: "author",
"Author-Title": "author",
subject: "subject",
subject_browse: "subject",
"catalog:subject": "subject",
Subject: "subject",
publisher: "publisher",
"catalog:publisher": "publisher",
title: "title",
title_starts: "title",
"catalog:title": "title",
journaltitle: "journaltitle",
"catalog:journaltitle": "journaltitle",
"catalog:lc_callnum": "lc_callnum",
"Call-Number": "lc_callnum",
callnumber_browse: "lc_callnum",
lc_callnum: "lc_callnum"
};

// Check if the field name needs special mapping
if (field.name in fieldMappings) {
if (field.name === "browse_type" || field.name === "search_field") {
if (field.value in searchValueMappings) {
params.append("search_field", searchValueMappings[field.value]);
}
} else {
params.append(fieldMappings[field.name], field.value);
}
} else if (field.name !== "authenticity_token" && field.name !== "utf8") {
// Add other fields except unnecessary ones
params.append(field.name, field.value);
}
}
});
// Update the href of the link
advancedSearchLink.href = `/advanced?${params.toString()}`;
};

// Add event listeners to form fields for dynamic updates
formFields.forEach((field) => {
field.addEventListener("input", updateAdvancedSearchLink);
field.addEventListener("change", updateAdvancedSearchLink);
});

// Ensure the link updates on page load
updateAdvancedSearchLink();

// Ensure params are updated on click as a fallback
advancedSearchLink.addEventListener("click", updateAdvancedSearchLink);
}
});
43 changes: 36 additions & 7 deletions blacklight-cornell/app/views/shared/_search_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,27 @@
<% if !params[:q_row].present? || params[:action] == 'edit' %>
<div class="form-group col-6 mb-0">
<label for="q" class="sr-only"><%= t('blacklight.search.form.q') %></label>
<%= text_field_tag :q, params[:q], :placeholder => t('blacklight.search.form.q'), :class => "search-query phone-search-query form-control", :id => "q", :autocapitalize => "off" %>
<%= text_field_tag :q, params[:q],
:placeholder => t('blacklight.search.form.q'),
:class => "search-query phone-search-query form-control",
:id => "q",
:data => { :dynamic => "true" },
:autocapitalize => "off" %>
</div>

<% if search_fields.empty? or search_fields.nil? %>
<input type="hidden" name="search_field" value="all_fields"/>
<input type="hidden" name="search_field" value="all_fields"/>
<% else %>
<div class="form-group col-4 col-lg-5 mb-0">
<label for="search_field" class="sr-only"><%= t('blacklight.search.form.search_field.label') %></label>
<%= select_tag(:search_field, options_for_select(search_bar_select, selected:params[:search_field],disabled: ["separator_1","separator_2","separator_3","separator_4"]), :title => t('blacklight.search.form.search_field.title'), :class => 'form-control') %>
<%= select_tag(:search_field,
options_for_select(search_bar_select,
selected:params[:search_field],
disabled: ["separator_1","separator_2","separator_3","separator_4"]),
:title => t('blacklight.search.form.search_field.title'),
:class => 'form-control',
:data => { :dynamic => "true" } ) %>

<span class="sr-only"><%= t('blacklight.search.form.search_field.post_label') %></span>
</div>
<% end %>
Expand Down Expand Up @@ -67,15 +79,26 @@
<% if !params[:q_row].present? || params[:action] == 'edit' %>
<div class="form-group col-6 mb-0">
<label for="q" class="sr-only"><%= t('blacklight.search.form.q') %></label>
<%= text_field_tag :q, params[:q], :placeholder => t('blacklight.search.form.q'), :class => "search-query phone-search-query form-control", :id => "q", :autocapitalize => "off" %>
<%= text_field_tag :q, params[:q],
:placeholder => t('blacklight.search.form.q'),
:class => "search-query phone-search-query form-control",
:id => "q",
:data => { :dynamic => "true" },
:autocapitalize => "off" %>
</div>

<% if search_fields.empty? or search_fields.nil? %>
<input type="hidden" name="search_field" value="all_fields"/>
<input type="hidden" name="search_field" value="all_fields"/>
<% else %>
<div class="form-group col-4 col-lg-5 mb-0">
<label for="search_field" class="sr-only"><%= t('blacklight.search.form.search_field.label') %></label>
<%= select_tag(:search_field, options_for_select(search_bar_select, selected:params[:search_field],disabled: ["separator_1","separator_2","separator_3","separator_4"]), :title => t('blacklight.search.form.search_field.title'), :class => 'form-control') %>
<%= select_tag(:search_field,
options_for_select(search_bar_select,
selected:params[:search_field],
disabled: ["separator_1","separator_2","separator_3","separator_4"]),
:title => t('blacklight.search.form.search_field.title'),
:class => 'form-control',
:data => { :dynamic => "true" } ) %>
<span class="sr-only"><%= t('blacklight.search.form.search_field.post_label') %></span>
</div>
<% end %>
Expand Down Expand Up @@ -103,9 +126,15 @@
<% if params[:controller] !='advanced_search' %>
<div class="search-nav col-12 col-lg-5">
<ul class="nav nav-pills">
<li><a href="/advanced"><i class="fa fa-search" aria-hidden="true"></i>Advanced Search</a></li>
<li>
<a id="advanced-search-link" href="/advanced">
<i class="fa fa-search" aria-hidden="true"></i>Advanced Search
</a>
</li>
</ul>
</div>
<% end %>

</div>

<%= javascript_include_tag 'search_form', 'data-turbo-track': 'reload' %>
3 changes: 1 addition & 2 deletions blacklight-cornell/app/views/single_search/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
<%= render 'single_search/search_form' %>
</div>
<div class="advanced-search-link col-md-4">
<%= link_to 'Catalog Advanced Search', '/advanced' %>
<%#= params[:q].inspect %>
<%= link_to 'Catalog Advanced Search', "/advanced?q=#{params[:q]}" %>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion blacklight-cornell/config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
Rails.application.config.assets.paths << Rails.root.join('vendor', 'assets', 'images')

Rails.application.config.assets.precompile += %w( aeon.css )
Rails.application.config.assets.precompile += %w( aeon.js )
Rails.application.config.assets.precompile += %w( aeon.js )
Rails.application.config.assets.precompile += %w( search_form.js )
13 changes: 13 additions & 0 deletions blacklight-cornell/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require_relative '../features/support/selenium'

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
Expand Down Expand Up @@ -62,4 +64,15 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")


###################################################
## Uncomment to see html of page at test failure ##
###################################################
# config.after(:each, type: :system) do |system_test|
# if system_test.exception
# save_page Rails.root.join("tmp/capybara", "failed_test.html")
# puts "Saved HTML to tmp/capybara/failed_test.html"
# end
# end
end
3 changes: 3 additions & 0 deletions blacklight-cornell/spec/system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rspec Feature Tests
* Replacement of Cucumber Feature Tests with Rspec system tests
* Organize tests in directories as needed
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'rails_helper'

RSpec.describe 'Basic Search to Advanced Search Carryover', type: :system do
context 'When user types in basic search form' do
it 'can have same text carryover to advanced search without submitting search' do
visit root_path
assert_text "Search..."
fill_in 'q', with: 'Test Query'
assert_text "Advanced Search"
click_on "Advanced Search"
expect(find('#q_row0').value).to eq('Test Query')
end

it 'can have same text and selected title field carryover to advanced search without submitting search' do
visit root_path
assert_text "Search..."
fill_in 'q', with: 'Test Query'
select 'Title', from: 'search_field'
assert_text "Advanced Search"
click_on "Advanced Search"
expect(find('#q_row0').value).to eq('Test Query')
expect(find('#search_field_advanced0').value).to eq('title')
end

it 'can have same text and selected field carryover to advanced search for all fields' do
visit root_path
assert_text "Search..."
fill_in 'q', with: 'Test Query'
# Define all field options to test
field_options = {
'All Fields' => 'all_fields',
'Title' => 'title',
'Journal Title' => 'journaltitle',
'Title Begins With' => 'title',
'Author' => 'author',
'Author Browse (A-Z) Sorted By Name' => 'author',
'Author Browse (A-Z) Sorted By Title' => 'author',
'Subject' => 'subject',
'Subject Browse (A-Z)' => 'subject',
'Call Number' => 'lc_callnum',
'Call Number Browse' => 'lc_callnum',
'Publisher' => 'publisher',
}

# Iterate through each field option and test carryover
field_options.each do |field_name, field_value|
select field_name, from: 'search_field'
assert_text "Advanced Search"
click_on "Advanced Search"
# Verify that the query text carried over
expect(find('#q_row0').value).to eq('Test Query')
# Verify that the selected field carried over
expect(find('#search_field_advanced0').value).to eq(field_value)
# Navigate back to the root_path for the next iteration
visit root_path
fill_in 'q', with: 'Test Query'
end
end

it 'can have same text carryover to advanced search after submitting search' do
visit root_path
assert_text "Search..."
fill_in 'q', with: 'Food'
click_button 'search-btn'
expect(page).to have_link("Library Catalog", href: "/")
click_on 'Advanced Search', id: 'advanced-search-link'
expect(find('#q_row0').value).to eq('Food')
end
end

it 'does not carry over when search field is left blank' do
visit root_path
assert_text "Search..."
assert_text "Advanced Search"
click_on "Advanced Search"
expect(find('#q_row0').value).to eq('')
end

it 'handles URI too large error gracefully' do
large_text = 'A' * 10_000
visit root_path
assert_text "Search..."
fill_in 'q', with: large_text
assert_text "Advanced Search"
click_on "Advanced Search"
expect(page).to have_content("Request-URI Too Large")
end

it 'handles special characters in input text' do
special_text = '!@#$%^&*()_+{}:"<>?~`-=[];,./'
visit root_path
assert_text "Search..."
fill_in 'q', with: special_text
assert_text "Advanced Search"
click_on "Advanced Search"
expect(find('#q_row0').value).to eq(special_text)
end
end