Skip to content

Commit

Permalink
Show selected values in numismatics form facets
Browse files Browse the repository at this point in the history
  • Loading branch information
sandbergja committed Aug 17, 2023
1 parent 31b4859 commit cfc5c35
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 35 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/components/search--advanced.scss
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@
columns: 2;
}
}

.two-columns-md > * {
margin: 5px 5px 1rem;
}
62 changes: 39 additions & 23 deletions app/components/numismatics_search_form_component.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
# frozen_string_literal: true

class NumismaticsSearchFormComponent < Blacklight::AdvancedSearchFormComponent
def initialize_search_filter_controls
fields = ['issue_object_type_s', 'issue_denomination_s',
'issue_metal_s', 'issue_city_s', 'issue_state_s',
'issue_region_s', 'issue_ruler_s',
'issue_artists_s', 'find_place_s'].map do |field_name|
blacklight_config.facet_fields[field_name]
end.compact

fields.each do |config|
config.advanced_search_component = Orangelight::FacetFieldCheckboxesComponent
display_facet = @response.aggregations[config.field]
search_filter_control(config:, display_facet:)
end
end

def pub_date_field
blacklight_config.facet_fields['pub_date_start_sort']
end

def pub_date_presenter
view_context.facet_field_presenter(pub_date_field, {})
end

def sort_fields_select
options = sort_fields.values.map { |field_config| [helpers.sort_field_label(field_config.key), field_config.key] }
return unless options.any?

select_tag(:sort, options_for_select(options, params[:sort]), class: "sort-select", aria: { labelledby: 'advanced-search-sort-label' })
end

private

def initialize_search_filter_controls
fields = ['issue_object_type_s', 'issue_denomination_s',
'issue_metal_s', 'issue_city_s', 'issue_state_s',
'issue_region_s', 'issue_ruler_s',
'issue_artists_s', 'find_place_s'].map do |field_name|
blacklight_config.facet_fields[field_name]
end.compact

fields.each do |config|
config.advanced_search_component = Orangelight::FacetFieldCheckboxesComponent
display_facet = @response.aggregations[config.field]
with_search_filter_control(config:, display_facet:)
end
end

def pub_date_field
blacklight_config.facet_fields['pub_date_start_sort']
end

def pub_date_presenter
view_context.facet_field_presenter(pub_date_field, {})
end

def initialize_constraints
params = helpers.search_state.params_for_search.except :page, :q, :search_field, :op, :index, :sort

adv_search_context = helpers.search_state.reset(params)

constraints_text = render(Blacklight::ConstraintsComponent.for_search_history(search_state: adv_search_context))

return if constraints_text.blank?

with_constraint do
constraints_text
end
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<div class="form-group advanced-search-facet row">
<label class="col-sm-4 control-label advanced-facet-label"><%= @facet_field.label %></label>
<div class="col-sm-8">
<%= content_tag(:select, :multiple => true, "aria-hidden" => "true",
:name => "f_inclusive[#{@facet_field.key}][]",
:id => @facet_field.key.parameterize,
:class => "form-control custom-select selectpicker",
:title => "Type or select #{ @facet_field.label.downcase.pluralize }",
:data => { "live-search": "true" }) do %>
<%= content_tag(:select, multiple: true, "aria-hidden" => "true",
name: "f_inclusive[#{@facet_field.key}][]",
id: @facet_field.key.parameterize,
class: "form-control custom-select selectpicker",
title: "Type or select #{ @facet_field.label.downcase.pluralize }",
data: { "live-search": "true" }) do %>
<% presenters.each do |presenter| %>
<%= content_tag :option, :value => presenter.value, :selected => presenter.selected? do %>
<%= content_tag :option, value: presenter.value,
selected: presenter.search_state.filter(presenter.facet_config).include?([presenter.value]) do %>
<%= presenter.label %>&nbsp;&nbsp;(<%= number_with_delimiter presenter.hits %>)
<% end %>
<% end %>
Expand Down
28 changes: 23 additions & 5 deletions spec/components/numismatics_search_form_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,31 @@
allow(view_context).to receive(:facet_limit_for).and_return(nil)
end

it "renders fields in the correct order" do
expected_order = [
it "renders all expected fields" do
expected_fields = [
'Object Type', 'Denomination', 'Metal', 'City', 'State',
'Region', 'Ruler', 'Artist', 'Find Place', 'Year',
'date range (ending year)', 'date range (starting year)',
'Keyword'
'date range (starting year)', 'date range (ending year)',
'Keyword', 'Sort results by'
]
expect(rendered.all('label').map(&:text)).to match_array(expected_order)
expect(rendered.all('label').map(&:text)).to match_array(expected_fields)
end

context 'when URL includes facet params from the user' do
let(:params) do
{ "f_inclusive" => { "issue_ruler_s" => ["Alexios III Angelos"] } }.with_indifferent_access
end
let(:filter) do
facet_config = CatalogController.blacklight_config.facet_fields['issue_ruler_s']
search_state = Blacklight::SearchState.new(params, CatalogController.blacklight_config)
[Blacklight::SearchState::FilterField.new(facet_config, search_state)]
end
before do
allow_any_instance_of(Blacklight::SearchState).to receive(:filters).and_return(filter)
end
it "displays the constraints area" do
constraints_area = rendered.find('.constraints')
expect(constraints_area).to have_text('Ruler:Alexios III Angelos')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@
"Field"
)
end
context 'when url includes a facet value that the user has selected' do
let(:search_state) { Blacklight::SearchState.new({ "f_inclusive" => { "field" => ["b"] } }, Blacklight::Configuration.new) }
it 'displays the value as selected' do
expect(
render_inline(described_class.new(facet_field:)).to_s
).to include(
'<option value="b" selected>'
)
end
end
end

0 comments on commit cfc5c35

Please sign in to comment.