-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON response for catalog#index and #show returns all Solr fields
- Loading branch information
1 parent
db01b1a
commit 1478df8
Showing
13 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/presenters/commonwealth_vlr_engine/json_field_presenter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module CommonwealthVlrEngine | ||
class JsonFieldPresenter < Blacklight::FieldPresenter | ||
private | ||
|
||
def retrieve_values | ||
document[field_config.field] | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
app/presenters/commonwealth_vlr_engine/json_index_presenter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module CommonwealthVlrEngine | ||
class JsonIndexPresenter < Blacklight::IndexPresenter | ||
# override Blacklight::DocumentPresenter needed so we return value as array | ||
# since CommonwealthVlrEngine::JsonFieldPresenter#retrieve_values returns String | ||
# (app/view/catalog/index.json.builder calls display_type.first) | ||
def display_type(base_name = nil, default: nil) | ||
[super] | ||
end | ||
|
||
private | ||
|
||
# override Blacklight::IndexPresenter so we can return all Solr fields | ||
# @return [Hash<String,Configuration::Field>] all the fields for this index view | ||
def fields | ||
Hash[document.keys.collect { |k| [k, field_config(k)] } ] | ||
end | ||
|
||
# override so we can set custom presenter | ||
def field_presenter(field_config, options = {}) | ||
presenter_class = CommonwealthVlrEngine::JsonFieldPresenter | ||
presenter_class.new(view_context, document, field_config, options.merge(field_presenter_options)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.set! field_name, field_presenter.values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe CommonwealthVlrEngine::BlacklightHelper do | ||
let(:blacklight_config) { CatalogController.blacklight_config } | ||
let(:search_service) { Blacklight::SearchService.new(config: blacklight_config) } | ||
let(:document) { SolrDocument.find('bpl-dev:h702q6403') } | ||
|
||
before :each do | ||
allow(helper).to receive_messages(blacklight_config: blacklight_config) | ||
end | ||
|
||
describe '#document_presenter_class' do | ||
before :each do | ||
allow(helper).to receive(:formats).and_return([:json]) | ||
end | ||
|
||
it 'returns JsonIndexPresenter for JSON requests' do | ||
expect(helper.document_presenter_class(document)).to eq CommonwealthVlrEngine::JsonIndexPresenter | ||
end | ||
end | ||
|
||
describe 'document_heading methods' do | ||
let(:document) { SolrDocument.find('bpl-dev:3j334603p') } | ||
|
||
describe '#document_heading' do | ||
let(:heading) { helper.document_heading(document) } | ||
|
||
it 'returns the properly formatted heading' do | ||
expect(heading).to include document['title_info_primary_tsi'] | ||
expect(heading).to include document['subtitle_tsim'].first | ||
expect(heading).to include document['title_info_partnum_tsi'] | ||
end | ||
end | ||
|
||
describe '#render_document_heading' do | ||
let(:heading) { helper.render_document_heading(document) } | ||
|
||
it 'returns the properly formatted heading' do | ||
expect(heading).to include document['subtitle_tsim'].first | ||
end | ||
end | ||
end | ||
|
||
describe '#extra_body_classes' do | ||
let(:classes) { helper.extra_body_classes } | ||
before :each do | ||
allow(helper).to receive(:controller_name).and_return('pages') | ||
allow(helper).to receive(:action_name).and_return('home') | ||
end | ||
|
||
it 'returns the expected class values' do | ||
expect(classes).to include 'blacklight-home' | ||
expect(classes).to include 'blacklight-pages' | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
spec/presenters/commonwealth_vlr_engine/json_field_presenter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe CommonwealthVlrEngine::JsonFieldPresenter, api: true do | ||
subject(:presenter) { described_class.new({}, document, field_config) } | ||
|
||
let(:field_name) { 'genre_basic_ssim' } | ||
let(:blacklight_config) do | ||
Blacklight::Configuration.new.configure do |config| | ||
config.add_index_field field_name | ||
end | ||
end | ||
let(:field_config) { blacklight_config.index_fields[field_name] } | ||
let(:document) { SolrDocument.find('bpl-dev:h702q6403') } | ||
|
||
describe '#retrieve_values' do | ||
it 'returns the raw field value' do | ||
expect(subject.send(:retrieve_values)).to eq ['Photographs'] | ||
end | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
spec/presenters/commonwealth_vlr_engine/json_index_presenter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe CommonwealthVlrEngine::JsonIndexPresenter, api: true do | ||
subject { presenter } | ||
|
||
let(:request_context) { double(document_index_view_type: 'list') } | ||
let(:blacklight_config) { Blacklight::Configuration.new } | ||
let(:document) { SolrDocument.find('bpl-dev:h702q6403') } | ||
let(:presenter) { described_class.new(document, request_context, blacklight_config) } | ||
|
||
describe '#display_type' do | ||
before :each do | ||
blacklight_config.index.display_type_field = :active_fedora_model_suffix_ssi | ||
end | ||
|
||
it 'returns the value as an array' do | ||
expect(presenter.display_type).to eq ['PhotographicPrint'] | ||
end | ||
end | ||
|
||
describe '#fields' do | ||
let(:all_fields) { subject.send(:fields) } | ||
|
||
it 'returns all document fields' do | ||
expect(all_fields.length).to be > 50 | ||
expect(all_fields['date_start_dtsi']).not_to be_falsey | ||
expect(all_fields['genre_basic_ssim']).not_to be_falsey | ||
end | ||
end | ||
|
||
describe '#field_presenter' do | ||
let(:field_config) { Blacklight::Configuration::Field.new } | ||
|
||
it 'returns JsonFieldPresenter' do | ||
expect(subject.send(:field_presenter, field_config)).to be_a_kind_of CommonwealthVlrEngine::JsonFieldPresenter | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'catalog/index.json', api: true do | ||
let(:item_pid) { 'bpl-dev:h702q6403' } | ||
let(:docs) { [SolrDocument.find(item_pid)] } | ||
let(:response) { instance_double(Blacklight::Solr::Response, documents: docs, prev_page: nil, next_page: 2, total_pages: 3) } | ||
let(:blacklight_config) { Blacklight::Configuration.new } | ||
let(:presenter) { Blacklight::JsonPresenter.new(response, blacklight_config) } | ||
let(:search_state) { Blacklight::SearchState.new({}, blacklight_config, controller) } | ||
let(:hash) do | ||
render template: 'catalog/index.json', format: :json | ||
JSON.parse(rendered).with_indifferent_access | ||
end | ||
let(:rendered_doc) { hash['data'].first } | ||
|
||
before :each do | ||
allow(view).to receive(:blacklight_config).and_return(blacklight_config) | ||
allow(view).to receive(:search_state).and_return(search_state) | ||
allow(view).to receive_messages(blacklight_configuration_context: Blacklight::Configuration::Context.new(CatalogController.new)) | ||
allow(presenter).to receive(:pagination_info).and_return(current_page: 1, | ||
next_page: 2, | ||
prev_page: nil) | ||
assign :presenter, presenter | ||
assign :response, response | ||
end | ||
|
||
it 'renders all document attributes' do | ||
expect(rendered_doc['id']).to eq item_pid | ||
expect(rendered_doc['attributes']['active_fedora_model_ssi']).to eq 'Bplmodels::PhotographicPrint' | ||
expect(rendered_doc['attributes']['has_model_ssim']).to be_a_kind_of(Array) | ||
expect(rendered_doc['attributes']['harvesting_status_bsi']).to be_truthy | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'catalog/show.json' do | ||
let(:item_pid) { 'bpl-dev:h702q6403' } | ||
let(:document) { SolrDocument.find(item_pid) } | ||
let(:blacklight_config) { Blacklight::Configuration.new } | ||
let(:hash) do | ||
render template: 'catalog/show.json', format: :json | ||
JSON.parse(rendered).with_indifferent_access | ||
end | ||
let(:rendered_doc) { hash['data'] } | ||
|
||
before :each do | ||
allow(view).to receive(:blacklight_config).and_return(blacklight_config) | ||
allow(view).to receive_messages(blacklight_configuration_context: Blacklight::Configuration::Context.new(CatalogController.new)) | ||
allow(view).to receive(:action_name).and_return('show') | ||
assign :document, document | ||
end | ||
|
||
it 'renders all document attributes' do | ||
expect(rendered_doc['id']).to eq item_pid | ||
expect(rendered_doc['attributes']['title_info_primary_tsi']).to eq 'Beauregard' | ||
expect(rendered_doc['attributes']['genre_basic_ssim']).to be_a_kind_of(Array) | ||
expect(rendered_doc['attributes']['subject_point_geospatial']).not_to be_blank | ||
end | ||
end |