diff --git a/.rubocop.yml b/.rubocop.yml index d34dc774ea..ae0c9b41b3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -15,6 +15,7 @@ Rails: Metrics/BlockLength: Exclude: + - "app/views/catalog/index.json.jbuilder" - "lib/railties/blacklight.rake" - "tasks/blacklight.rake" diff --git a/app/views/catalog/index.json.jbuilder b/app/views/catalog/index.json.jbuilder index 64b86b0701..75d1cc98a5 100644 --- a/app/views/catalog/index.json.jbuilder +++ b/app/views/catalog/index.json.jbuilder @@ -1,5 +1,4 @@ # frozen_string_literal: true - json.links do json.self url_for(search_state.to_h.merge(only_path: false)) json.prev url_for(search_state.to_h.merge(only_path: false, page: @response.prev_page.to_s)) if @response.prev_page @@ -13,19 +12,26 @@ end json.data do json.array! @presenter.documents do |document| + document_url = polymorphic_url(url_for_document(document)) json.id document.id json.attributes do doc_presenter = index_presenter(document) index_fields(document).each do |field_name, field| - if should_render_index_field? document, field - json.set! field_name, doc_presenter.field_value(field_name) + next unless should_render_index_field? document, field + json.set!(field_name) do + json.id "#{document_url}##{field_name}" + json.type 'document_value' + json.attributes do + json.value doc_presenter.field_value(field_name) + json.label field.label + end end end end json.links do - json.self polymorphic_url(url_for_document(document)) + json.self document_url end end end @@ -35,6 +41,7 @@ json.included do json.type 'facet' json.id facet['name'] json.attributes do + json.label facet['label'] json.items do json.array! facet['items'] do |item| json.id diff --git a/spec/views/catalog/index.json.jbuilder_spec.rb b/spec/views/catalog/index.json.jbuilder_spec.rb index da18937c5c..cf253488df 100644 --- a/spec/views/catalog/index.json.jbuilder_spec.rb +++ b/spec/views/catalog/index.json.jbuilder_spec.rb @@ -11,7 +11,7 @@ let(:facets) { double("facets") } let(:config) do Blacklight::Configuration.new do |config| - config.add_index_field 'title_tsim', label: 'Title:' + config.add_index_field 'title', label: 'Title', field: 'title_tsim' end end let(:presenter) { Blacklight::JsonPresenter.new(response, facets, config) } @@ -54,12 +54,30 @@ expect(hash).to include(data: [ { id: '123', - attributes: { 'title_tsim' => 'Book1' }, + attributes: { + 'title': { + id: 'http://test.host/catalog/123#title', + type: 'document_value', + attributes: { + value: 'Book1', + label: 'Title' + } + } + }, links: { self: 'http://test.host/catalog/123' } }, { id: '456', - attributes: { 'title_tsim' => 'Book2' }, + attributes: { + 'title': { + id: 'http://test.host/catalog/456#title', + type: 'document_value', + attributes: { + value: 'Book2', + label: 'Title' + } + } + }, links: { self: 'http://test.host/catalog/456' } }, ]) @@ -77,6 +95,7 @@ expect(format['links']).to include self: 'http://test.host/some/facet/url' expect(format['attributes']).to include :items + expect(format['attributes']['label']).to eq 'Format' format_items = format['attributes']['items'].map { |x| x['attributes'] }