Skip to content

Commit

Permalink
Hide facet config rows for facets containing zero items. Fixes #1438.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored and cbeer committed Aug 16, 2016
1 parent ac5479a commit 570a843
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/models/spotlight/blacklight_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def blacklight_config
def custom_index_fields
Hash[exhibit.custom_fields.map do |x|
field = Blacklight::Configuration::IndexField.new x.configuration.merge(
key: x.field, field: x.solr_field
key: x.field, field: x.solr_field, custom_field: true
)
[x.field, field]
end]
Expand All @@ -202,7 +202,7 @@ def custom_index_fields
def custom_facet_fields
Hash[exhibit.custom_fields.vocab.map do |x|
field = Blacklight::Configuration::FacetField.new x.configuration.merge(
key: x.field, field: x.solr_field, show: false
key: x.field, field: x.solr_field, show: false, custom_field: true
)
[x.field, field]
end]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<% if metadata[:document_count] > 0 %>
<% if metadata[:value_count] > Spotlight::FieldMetadata::FACET_LIMIT %>
<%= content_tag :span, t(:'.too_many_values_count', count: Spotlight::FieldMetadata::FACET_LIMIT) %>
<%= content_tag :span, t(:'.too_many_values_count', count: Spotlight::FieldMetadata::FACET_LIMIT) %>
<% else %>
<%= content_tag :span, t(:'.value_count', count: metadata[:value_count]) %>
<%= content_tag :span, t(:'.value_count', count: metadata[:value_count]) %>
<% end %>
<% if metadata[:terms].any? %>
<%= tag("span", class: 'btn-with-tooltip glyphicon glyphicon-info-sign', data: {container: 'body', toggle: 'tooltip', placement: 'top', title: metadata[:terms].join(" • ")}) %>
<%= tag("span", class: 'btn-with-tooltip glyphicon glyphicon-info-sign', data: {container: 'body', toggle: 'tooltip', placement: 'top', title: metadata[:terms].join(" • ")}) %>
<% end %>
<% end %>
<% end %>
3 changes: 2 additions & 1 deletion app/views/spotlight/search_configurations/_facets.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<%= f.fields_for :facet_fields do |idxf| %>
<% @blacklight_configuration.blacklight_config.facet_fields.each do |key, config| %>
<% metadata = @field_metadata.field(key) %>
<li class="dd-item" data-id="<%= key.parameterize %>">
<% next unless metadata[:document_count] > 0 || config.custom_field %>
<li class="dd-item" data-id="<%= key.parameterize %>">
<div class="dd-handle dd3-handle"><%= t :drag %></div>
<%= idxf.fields_for key do |facet| %>
<div class="dd3-content panel panel-default facet-config-<%= key.parameterize %>">
Expand Down
2 changes: 2 additions & 0 deletions spec/models/spotlight/blacklight_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
it 'includes any custom fields' do
allow(subject).to receive_messages(custom_facet_fields: { 'a' => double(if: nil, :if= => true, merge!: true, validate!: true, normalize!: true) })
expect(subject.blacklight_config.facet_fields).to include('a')
expect(subject.blacklight_config.facet_fields['a'].custom_field).to eq true
end

it 'defaults to not showing a custom field in the facets' do
Expand Down Expand Up @@ -245,6 +246,7 @@
allow(subject).to receive_messages(custom_index_fields: { 'a' => double(if: nil, :if= => true, merge!: true, validate!: true, normalize!: true) })

expect(subject.blacklight_config.show_fields).to include('a')
expect(subject.blacklight_config.show_fields['a'].custom_field).to eq true
end

it 'orders the fields by weight' do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
describe 'spotlight/search_configurations/_facets', type: :view do
let(:exhibit) { FactoryGirl.create(:exhibit) }
let!(:custom_field) { FactoryGirl.create(:custom_field, exhibit: exhibit, label: 'Foobar', field_type: 'vocab') }
let(:config) do
exhibit.blacklight_configuration
end
let(:field_metadata) { double('field_metadata') }
let(:empty_facet) { { document_count: 0, value_count: 0, terms: [] } }
let(:nonempty_facet) { { document_count: 1, value_count: 3, terms: %w(a b c) } }
let(:f) do
form_helper = nil
controller.view_context.bootstrap_form_for(config, url: '/update') do |f|
form_helper = f
end
form_helper
end

before do
assign(:blacklight_configuration, config)
allow(view).to receive_messages(current_exhibit: exhibit,
blacklight_config: config.blacklight_config)
allow(field_metadata).to receive(:field).with(any_args).and_return(nonempty_facet)
allow(field_metadata).to receive(:field).with('genre_ssim').and_return(empty_facet)
allow(field_metadata).to receive(:field).with(custom_field.field).and_return(empty_facet)
assign(:field_metadata, field_metadata)
render partial: 'spotlight/search_configurations/facets', locals: { f: f }
end

it 'shows the config for the non-empty personal name facet' do
expect(rendered).to have_content 'Personal Names'
end

it 'shows the config for the empty custom facet' do
expect(rendered).to have_content 'Foobar'
end

it 'hides the config for the empty genre facet' do
expect(rendered).not_to have_content 'Genre'
end
end

0 comments on commit 570a843

Please sign in to comment.