Skip to content

Commit

Permalink
Consistently use FacetField#field when retrieving aggregation values …
Browse files Browse the repository at this point in the history
…from the response
  • Loading branch information
cbeer committed Jul 28, 2016
1 parent 7820fcb commit b8e2875
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
17 changes: 12 additions & 5 deletions app/controllers/concerns/blacklight/facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/helpers/facets_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b8e2875

Please sign in to comment.