Skip to content

Commit

Permalink
JsonPresenter has facets_from_request available to itself
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jul 24, 2018
1 parent c2c9790 commit 92e06b3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 0 additions & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def index
format.atom { render layout: false }
format.json do
@presenter = Blacklight::JsonPresenter.new(@response,
facets_from_request,
blacklight_config)
end
additional_response_formats(format)
Expand Down
7 changes: 3 additions & 4 deletions app/presenters/blacklight/json_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ class JsonPresenter
include Blacklight::Facet

# @param [Solr::Response] response raw solr response.
# @param [Array<Solr::Response::Facets::FacetField>] facets list of facets
# @param [Configuration] blacklight_config the configuration
def initialize(response, facets, blacklight_config)
# @param [Array] facets list of facets
def initialize(response, blacklight_config)
@response = response
@facets = facets
@blacklight_config = blacklight_config
end

Expand All @@ -19,7 +18,7 @@ def documents
end

def search_facets_as_json
@facets.map do |display_facet|
facets_from_request.map do |display_facet|
next if display_facet.items.empty?
f = display_facet.as_json
f.stringify_keys!
Expand Down
27 changes: 16 additions & 11 deletions spec/presenters/blacklight/json_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
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(:response) do
instance_double(Blacklight::Solr::Response,
documents: docs,
prev_page: nil,
next_page: 2,
total_pages: 3,
aggregations: aggregations)
end
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 }])
]
let(:aggregations) do
{ 'format_si' => Blacklight::Solr::Response::Facets::FacetField.new("format_si", [{ label: "Book", value: 'Book', hits: 20 }])}
end

let(:config) do
Expand All @@ -23,7 +28,7 @@
end
end

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


describe '#search_facets_as_json' do
Expand All @@ -41,11 +46,11 @@
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", [])
]
let(:aggregations) do
{
'format_si' => Blacklight::Solr::Response::Facets::FacetField.new("format_si", [{ label: "Book", value: 'Book', hits: 20 }]),
'example_query_facet_field' => Blacklight::Solr::Response::Facets::FacetField.new("example_query_facet_field", [])
}
end

it 'shows only facets that are defined' do
Expand Down
3 changes: 1 addition & 2 deletions spec/views/catalog/index.json.jbuilder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
SolrDocument.new(id: '456', title_tsim: 'Book2', author_tsim: 'Rosie')
]
end
let(:facets) { double("facets") }
let(:config) do
Blacklight::Configuration.new do |config|
config.add_index_field 'title_tsim', label: 'Title:'
end
end
let(:presenter) { Blacklight::JsonPresenter.new(response, facets, config) }
let(:presenter) { Blacklight::JsonPresenter.new(response, config) }

let(:hash) do
render template: "catalog/index.json", format: :json
Expand Down

0 comments on commit 92e06b3

Please sign in to comment.