Skip to content

Commit

Permalink
Add a remove link for selected facets in the JSON response
Browse files Browse the repository at this point in the history
Fixes #1951
  • Loading branch information
jcoyne committed Jul 26, 2018
1 parent 93a7f47 commit 5c85b68
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
26 changes: 13 additions & 13 deletions app/presenters/blacklight/json_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class JsonPresenter

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

def search_facets_as_json
facets_from_request.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
f
end.compact
# @return [Array<Blacklight::Solr::Response::Facets::FacetField>]
def search_facets
facets_from_request.select { |display_facet| display_facet.items.present? }
# 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
# f
# end.compact
end

# extract the pagination info from the response object
Expand Down
17 changes: 9 additions & 8 deletions app/views/catalog/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,32 @@ json.data do
end

json.included do
json.array! @presenter.search_facets_as_json do |facet|
json.array! @presenter.search_facets do |facet|
json.type 'facet'
json.id facet['name']
json.id facet.name
json.attributes do
json.label facet['label']
facet_config = facet_configuration_for_field(facet.name)
json.label facet_field_label(facet_config.key)
json.items do
json.array! facet['items'] do |item|
json.array! facet.items do |item|
json.id
json.attributes do
json.label item['label']
json.value item['value']
json.hits item['hits']
end
json.links do
if facet_in_params?(facet['name'], item['value'])
json.remove search_action_path(search_state.remove_facet_params(facet_field, item))
if facet_in_params?(facet.name, item['value'])
json.remove search_action_path(search_state.remove_facet_params(facet, item))
else
json.self path_for_facet(facet['name'], item['value'], only_path: false)
json.self path_for_facet(facet.name, item['value'], only_path: false)
end
end
end
end
end
json.links do
json.self search_facet_path(id: facet['name'], only_path: false)
json.self search_facet_path(id: facet.name, only_path: false)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/presenters/blacklight/json_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
let(:presenter) { described_class.new(response, config) }


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

context 'for defined facets that are present in the response' do
it 'has a label' do
Expand Down
12 changes: 8 additions & 4 deletions spec/views/catalog/index.json.jbuilder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
JSON.parse(rendered).with_indifferent_access
end

let(:format_facet) do
Blacklight::Solr::Response::Facets::FacetField.new('format',
[{ 'value' => 'Book', 'hits' => 30, 'label' => 'Book' }],
'label' => 'Format')
end

before do
allow(view).to receive(:blacklight_config).and_return(config)
allow(view).to receive(:search_action_path).and_return('http://test.host/some/search/url')
allow(view).to receive(:search_facet_path).and_return('http://test.host/some/facet/url')
allow(presenter).to receive(:pagination_info).and_return({ current_page: 1, next_page: 2,
prev_page: nil })
allow(presenter).to receive(:search_facets_as_json).and_return(
[{ 'name' => "format", 'label' => "Format",
'items' => [{ 'value' => 'Book', 'hits' => 30, 'label' => 'Book' }] }])
allow(presenter).to receive(:search_facets).and_return([format_facet])
assign :presenter, presenter
assign :response, response
end
Expand Down Expand Up @@ -103,7 +107,7 @@
params[:f] = { format: 'Book' }
end
it 'has a link to remove the selected value' do
expect(format_items.first['links']).to eq "remove:"#{}" 'http://test.host/some/facet/url'
expect(format_items.first['links']).to eq('remove' => 'http://test.host/some/search/url')
end
end
end
Expand Down

0 comments on commit 5c85b68

Please sign in to comment.