Skip to content

Commit

Permalink
Do not add facet limit unless it is known.
Browse files Browse the repository at this point in the history
This commit updates the code that adds facet limits from user supplied
parameters so that facets are only added if they have been configured
as facet fields.  This is done to allow for multiple faceted searches to
be defined in the same URL while still keeping the facets separate.
  • Loading branch information
dkinzer committed Oct 23, 2018
1 parent c1ce34f commit 82f5462
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/helpers/blacklight/render_constraints_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def render_filter_element(facet, values, path)

safe_join(Array(values).map do |val|
next if val.blank? # skip empty string
# skip if facet field not configured
next if blacklight_config.facet_fields[facet.to_s].blank?
render_constraint_element(facet_field_label(facet_config.key),
facet_display_value(facet, val),
remove: search_action_path(path.remove_facet_params(facet, val)),
Expand Down
1 change: 1 addition & 0 deletions lib/blacklight/solr/search_builder_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def add_facet_fq_to_solr(solr_parameters)
f_request_params = blacklight_params[:f]

f_request_params.each_pair do |facet_field, value_list|
next unless blacklight_config.facet_fields[facet_field.to_s].present?
Array(value_list).reject(&:blank?).each do |value|
solr_parameters.append_filter_query facet_value_to_fq_string(facet_field, value)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@

before do
allow(helper).to receive(:blacklight_config).and_return(config)
expect(helper).to receive(:facet_field_label).with('type').and_return("Item Type")
end

let(:params) { ActionController::Parameters.new q: 'biz' }
let(:path) { Blacklight::SearchState.new(params, config, controller) }

it "has a link relative to the current url" do
expect(helper).to receive(:facet_field_label).with('type').and_return("Item Type")
expect(subject).to have_link "Remove constraint Item Type: journal", href: "/catalog?q=biz"
expect(subject).to have_selector ".filter-name", text: 'Item Type'
end
Expand All @@ -63,9 +63,18 @@
subject { helper.render_filter_element('type', 'journal', path) }

it "handles string values gracefully" do
expect(helper).to receive(:facet_field_label).with('type').and_return("Item Type")
expect(subject).to have_link "Remove constraint Item Type: journal", href: "/catalog?q=biz"
end
end

context "with unknown facet field in param" do
subject { helper.render_filter_element('unknown_field', 'journal', path) }

it "does not render for an unknown field" do
expect(subject).to be_empty
end
end
end

describe "#render_constraints_filters" do
Expand Down
11 changes: 11 additions & 0 deletions spec/models/blacklight/solr/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@

expect(solr_parameters[:fq]).to be_a_kind_of Array
end

context "facet not defined in config" do
let(:single_facet) { { unknown_facet_field: "foo" } }
let(:user_params) { { f: single_facet } }

it "does not add facet to solr_parameters" do
solr_parameters = Blacklight::Solr::Request.new
subject.add_facet_fq_to_solr(solr_parameters)
expect(solr_parameters[:fq]).to be_empty
end
end
end

describe "#add_solr_fields_to_query" do
Expand Down

0 comments on commit 82f5462

Please sign in to comment.