Skip to content

Commit

Permalink
Merge pull request #1942 from projectblacklight/remove_empty
Browse files Browse the repository at this point in the history
Remove facets with no items from the API response
  • Loading branch information
mjgiarlo committed Jul 24, 2018
2 parents 7f3e3ee + 8ae2c95 commit c2c9790
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/controllers/concerns/blacklight/facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ def facet_paginator(field_config, response_data)
)
end

# @param fields [Array<String>] a list of facet field names
# @return [Array<Solr::Response::Facets::FacetField>]
def facets_from_request(fields = facet_field_names)
fields.map { |field| facet_by_field_name(field) }.compact
end

# @return [Array<String>] a list of the facet field names from the configuration
def facet_field_names
blacklight_config.facet_fields.values.map(&:field)
end
Expand Down
10 changes: 7 additions & 3 deletions app/presenters/blacklight/json_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class JsonPresenter
include Blacklight::Facet

# @param [Solr::Response] response raw solr response.
# @param [Array] facets list of facets
# @param [Array<Solr::Response::Facets::FacetField>] facets list of facets
# @param [Configuration] blacklight_config the configuration
def initialize(response, facets, blacklight_config)
@response = response
@facets = facets
Expand All @@ -18,14 +19,17 @@ def documents
end

def search_facets_as_json
@facets.as_json.each do |f|
@facets.map do |display_facet|
next if display_facet.items.empty?
f = display_facet.as_json
f.stringify_keys!
f.delete "options"
f["label"] = facet_configuration_for_field(f["name"]).label
f["items"] = f["items"].as_json.each do |i|
i['label'] ||= i['value']
end
end
f
end.compact
end

# extract the pagination info from the response object
Expand Down
2 changes: 2 additions & 0 deletions app/views/catalog/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

json.links do
json.self polymorphic_url(@document)
end
Expand Down
56 changes: 56 additions & 0 deletions spec/presenters/blacklight/json_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Blacklight::JsonPresenter, api: true do
let(:response) { instance_double(Blacklight::Solr::Response, documents: docs, prev_page: nil, next_page: 2, total_pages: 3) }
let(:docs) do
[
SolrDocument.new(id: '123', title_tsim: 'Book1', author_tsim: 'Julie'),
SolrDocument.new(id: '456', title_tsim: 'Book2', author_tsim: 'Rosie')
]
end

let(:facets) do
[
Blacklight::Solr::Response::Facets::FacetField.new("format_si", [{ label: "Book", value: 'Book', hits: 20 }])
]
end

let(:config) do
Blacklight::Configuration.new do |config|
config.add_facet_field 'format', field: 'format_si', label: 'Format'
config.add_index_field 'title_tsim', label: 'Title:'
end
end

let(:presenter) { described_class.new(response, facets, config) }


describe '#search_facets_as_json' do
subject { presenter.search_facets_as_json }

context 'for defined facets that are present in the response' do
it 'has a label' do
expect(subject.first["label"]).to eq 'Format'
end
end


context 'when there are defined facets that are not in the response' do
before do
config.add_facet_field 'example_query_facet_field', label: 'Publish Date', query: {}
end

let(:facets) do
[
Blacklight::Solr::Response::Facets::FacetField.new("format_si", [{ label: "Book", value: 'Book', hits: 20 }]),
Blacklight::Solr::Response::Facets::FacetField.new("example_query_facet_field", [])
]
end

it 'shows only facets that are defined' do
expect(subject.map { |f| f['name'] }).to eq ['format_si']
end
end
end
end

0 comments on commit c2c9790

Please sign in to comment.