Skip to content

Commit

Permalink
HYC-1766 - Date range advanced search (#1023)
Browse files Browse the repository at this point in the history
* Configure date_issued field to have separate facet config for advanced search and regular search results. Add override of FacetsHelperBehavior to deduplicate list of facets during rendering, otherwise the date facet shows twice. Add advanced search range component (borrowed from stanford) for rendering the date field.

* Prepopulate range fields in advanced search if there was an existing value

* Add test for adv search date range, add date_issued to fixture records. Add placeholder value, fix font weight

* Rubocop
  • Loading branch information
bbpennel authored Oct 11, 2023
1 parent dd5f539 commit 5bb14d9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
15 changes: 15 additions & 0 deletions app/assets/stylesheets/unc_custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ span.institution_name {
}
}

.advanced-search-form {
.adv-date-year-range {
margin-left: -37.5%;
.form-control {
width: 6rem;
}
.form-inline {
margin-left: 0;
}
.col-form-label {
font-weight: bold;
}
}
}

.file_set.attributes .btn-group a {
color: black !important;
&:hover {
Expand Down
12 changes: 12 additions & 0 deletions app/components/advanced_search_range_limit_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%# [hyc-override] https://github.com/sul-dlss/SearchWorks/blob/01b8a0e8502220d4775ea868b097e9c723756455/app/components/advanced_search_range_limit_component.html.erb %>
<%# This is not an override, but it is based on a file from another project %>
<div class="form-group advanced-search-facet row facet_limit adv-date-year-range">
<%= label_tag "date_issued_isim", :class => "col-sm-3 col-form-label text-md-right" do %>Date<% end %>
<div class="col-sm-9 form-inline row">
<%= label_tag("range[date_issued_isim][begin]", 'from year', class: 'sr-only visually-hidden') %>
<%= number_field_tag("range[date_issued_isim][begin]", params.dig('range', 'date_issued_isim', 'begin'), maxlength: 6, class: "form-control text-center range_begin", placeholder: 'yyyy') %>
<span class="control-label">&nbsp;-&nbsp;</span>
<%= label_tag("range[date_issued_isim][end]", 'to year', class: 'sr-only visually-hidden') %>
<%= number_field_tag("range[date_issued_isim][end]", params.dig('range', 'date_issued_isim', 'end'), maxlength: 6, class: "form-control text-center range_end", placeholder: 'yyyy') %>
</div>
</div>
8 changes: 8 additions & 0 deletions app/components/advanced_search_range_limit_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true
# [hyc-override] https://github.com/sul-dlss/SearchWorks/blob/01b8a0e8502220d4775ea868b097e9c723756455/app/components/advanced_search_range_limit_component.rb
# The file is unchanged, but is imported from another project
class AdvancedSearchRangeLimitComponent < ViewComponent::Base
def initialize(facet_field:, **kwargs)
@facet_field = facet_field
end
end
8 changes: 7 additions & 1 deletion app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,20 @@ def single_item_search_builder(id)
config.add_facet_field solr_name('resource_type', :facetable), label: 'Resource Type', limit: 5
config.add_facet_field solr_name('creator_label', :facetable), label: 'Creator', limit: 5
config.add_facet_field solr_name('affiliation_label', :facetable), label: 'Departments', limit: 5
config.add_facet_field 'date_issued_isim', label: 'Date', limit: 5, **default_range_config
# Search results version of the date_issued facet
config.add_facet_field 'date_issued_isim', field: 'date_issued_isim', label: 'Date', range: true, range_config: {
input_label_range_begin: 'from year',
input_label_range_end: 'to year'
}
config.add_facet_field solr_name('keyword', :facetable), limit: 5
config.add_facet_field solr_name('subject', :facetable), limit: 5
config.add_facet_field solr_name('advisor_label', :facetable), label: 'Advisor', limit: 5
config.add_facet_field solr_name('edition', :facetable), label: 'Version', limit: 5
config.add_facet_field solr_name('language', :facetable), helper_method: :language_links_facets, limit: 5
config.add_facet_field solr_name('member_of_collections', :symbol), limit: 5, label: 'Collection'
config.add_facet_field solr_name('human_readable_type', :facetable), label: 'Type', show: false
# Advanced search version of the date_issued facet
config.add_facet_field 'date_issued_adv_search', show: false, field: 'date_issued_isim', label: 'Date', range: true, advanced_search_component: AdvancedSearchRangeLimitComponent

# The generic_type isn't displayed on the facet list
# It's used to give a label to the filter that comes from the user profile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true
# [hyc-override] https://github.com/projectblacklight/blacklight/blob/v7.33.1/app/helpers/blacklight/facets_helper_behavior.rb
Blacklight::FacetsHelperBehavior.module_eval do
# [hyc-override] make the list of facet fields unique to prevent duplicate Date facet
alias_method :original_render_facet_partials, :render_facet_partials
def render_facet_partials(fields = nil, options = {})
original_render_facet_partials(fields&.uniq, options)
end
end
40 changes: 40 additions & 0 deletions spec/features/advanced_search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true
require 'rails_helper'
require Rails.root.join('spec/support/oai_sample_solr_documents.rb')
include Warden::Test::Helpers

RSpec.describe 'Advanced search', type: :feature, js: false do
let(:solr) { Blacklight.default_index.connection }

before do
solr.delete_by_query('*:*') # delete everything in Solr
solr.add([SLEEPY_HOLLOW, MYSTERIOUS_AFFAIR, BEOWULF, LEVIATHAN, GREAT_EXPECTATIONS, ILIAD, MISERABLES, MOBY_DICK])
solr.commit
end

after do
solr.delete_by_query('*:*')
solr.commit
end

it 'date range field returns expected results and retains values' do
visit '/advanced'
fill_in('range_date_issued_isim_begin', with: '1990')
fill_in('range_date_issued_isim_end', with: '2020')
click_button('Search')
# Verify that only the titles with date issued within the given range are returned
expect(page).not_to have_content(SLEEPY_HOLLOW[:title_tesim][0])
expect(page).not_to have_content(MYSTERIOUS_AFFAIR[:title_tesim][0])
expect(page).not_to have_content(BEOWULF[:title_tesim][0])
expect(page).not_to have_content(LEVIATHAN[:title_tesim][0])
expect(page).to have_content(GREAT_EXPECTATIONS[:title_tesim][0])
expect(page).not_to have_content(ILIAD[:title_tesim][0])
expect(page).to have_content(MISERABLES[:title_tesim][0])
expect(page).to have_content(MOBY_DICK[:title_tesim][0])
# Return to the advanced search and verify that the date range is still present
click_link('Advanced search', match: :first)
expect(page).to have_content('Date:1990 to 2020')
expect(find('#range_date_issued_isim_begin').value).to eq('1990')
expect(find('#range_date_issued_isim_end').value).to eq('2020')
end
end
Loading

0 comments on commit 5bb14d9

Please sign in to comment.