diff --git a/app/controllers/concerns/blacklight/facet.rb b/app/controllers/concerns/blacklight/facet.rb index 7abfb3767e..80a74bb56f 100644 --- a/app/controllers/concerns/blacklight/facet.rb +++ b/app/controllers/concerns/blacklight/facet.rb @@ -19,21 +19,28 @@ def facets_from_request(fields = facet_field_names) end def facet_field_names - blacklight_config.facet_fields.keys + blacklight_config.facet_fields.values.map(&:field) end + # @param [String] field Solr facet name + # @return [Blacklight::Configuration::FacetField] Blacklight facet configuration for the solr field def facet_configuration_for_field(field) - blacklight_config.facet_fields[field] || - blacklight_config.facet_fields.values.find { |v| v.field.to_s == field.to_s } || + # short-circuit on the common case, where the solr field name and the blacklight field name are the same. + return blacklight_config.facet_fields[field] if blacklight_config.facet_fields[field] && blacklight_config.facet_fields[field].field == field + + # Find the facet field configuration for the solr field, or provide a default. + blacklight_config.facet_fields.find { |_, v| v.field.to_s == field.to_s } || Blacklight::Configuration::FacetField.new(field: field).normalize! end # Get a FacetField object from the @response def facet_by_field_name(field_or_field_name) case field_or_field_name - when String, Symbol, Blacklight::Configuration::FacetField + when String, Symbol facet_field = facet_configuration_for_field(field_or_field_name) - @response.aggregations[facet_field.key] + @response.aggregations[facet_field.field] + when Blacklight::Configuration::FacetField + @response.aggregations[field_or_field_name.field] else # is this really a useful case? field_or_field_name diff --git a/spec/helpers/facets_helper_spec.rb b/spec/helpers/facets_helper_spec.rb index 6ce04f3a4a..e543e1afa6 100644 --- a/spec/helpers/facets_helper_spec.rb +++ b/spec/helpers/facets_helper_spec.rb @@ -104,13 +104,13 @@ describe "facet_by_field_name" do it "retrieves the facet from the response given a string" do - facet_config = double(:query => nil, field: 'a', key: 'a') + facet_config = double(query: nil, field: 'b', key: 'a') facet_field = double() - allow(helper).to receive(:facet_configuration_for_field).with(anything()).and_return(facet_config) + allow(helper).to receive(:facet_configuration_for_field).with('b').and_return(facet_config) @response = double() - allow(@response).to receive(:aggregations).and_return('a' => facet_field) + allow(@response).to receive(:aggregations).and_return('b' => facet_field) - expect(helper.facet_by_field_name('a')).to eq facet_field + expect(helper.facet_by_field_name('b')).to eq facet_field end end