Skip to content

Commit

Permalink
JSON response for catalog#index and #show returns all Solr fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ebenenglish committed Dec 18, 2020
1 parent db01b1a commit 1478df8
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/helpers/commonwealth_vlr_engine/blacklight_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def document_heading(document = nil)
end
end

# override so we can set custom presenter for JSON API responses
def document_presenter_class(document)
formats.first == :json ? CommonwealthVlrEngine::JsonIndexPresenter : super
end

def extra_body_classes
@extra_body_classes ||= ['blacklight-' + controller_name, 'blacklight-' + [controller_name, controller.action_name].join('-')]
# if this is the home page
Expand Down
11 changes: 11 additions & 0 deletions app/presenters/commonwealth_vlr_engine/json_field_presenter.rb
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 app/presenters/commonwealth_vlr_engine/json_index_presenter.rb
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
3 changes: 3 additions & 0 deletions app/views/catalog/_field.json.jbuilder
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</dd>
<% end %>
<%# abstract/description field %>
<% if document[:abstract_tsim] %>
<% if document[:abstract_tsim].present? %>
<dt class="col-md-3"><%= t('blacklight.metadata_display.fields.abstract') %>:</dt>
<dd class="col-md-9">
<% document[:abstract_tsim].each do |abstract| %>
Expand Down
4 changes: 2 additions & 2 deletions spec/features/catalog/_az_links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def should_render_col_az?
end
end

it 'should show the a-z links' do
it 'shows the a-z links' do
visit collections_path
within('.item_az_links') do
expect(page).to have_selector('.az_link')
end
end

it 'should show correct results after clicking a letter link' do
it 'shows correct results after clicking a letter link' do
visit collections_path
within('.item_az_links') do
click_link('C')
Expand Down
2 changes: 1 addition & 1 deletion spec/features/catalog/_basic_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

describe 'basic_search' do
it 'should show correct results after running a fielded search' do
it 'shows correct results after running a fielded search' do
visit search_catalog_path
within '#basic_search_form' do
select('Title', from: 'search_field')
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/sample_solr_documents.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@
license_ssm:
- No known restrictions on use.
title_info_primary_tsi:
- The woman's book
- The woman's book
title_info_partnum_tsi:
- v.1
subtitle_tsim:
Expand Down Expand Up @@ -2195,7 +2195,7 @@
license_ssm:
- No known restrictions on use.
title_info_primary_tsi:
- The woman's book
- The woman's book
title_info_partnum_tsi:
- v.2
subtitle_tsim:
Expand Down
58 changes: 58 additions & 0 deletions spec/helpers/blacklight_helper_spec.rb
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
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
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
35 changes: 35 additions & 0 deletions spec/views/catalog/index.json.jbuilder_spec.rb
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
28 changes: 28 additions & 0 deletions spec/views/catalog/show.json.jbuilder_spec.rb
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

0 comments on commit 1478df8

Please sign in to comment.