Skip to content

Commit

Permalink
Merge 1e5f35d into fed4685
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jul 19, 2018
2 parents fed4685 + 1e5f35d commit 5323a2a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -15,6 +15,7 @@ Rails:

Metrics/BlockLength:
Exclude:
- "app/views/catalog/index.json.jbuilder"
- "lib/railties/blacklight.rake"
- "tasks/blacklight.rake"

Expand Down
15 changes: 11 additions & 4 deletions 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
Expand All @@ -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
Expand All @@ -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
Expand Down
25 changes: 22 additions & 3 deletions spec/views/catalog/index.json.jbuilder_spec.rb
Expand Up @@ -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) }
Expand Down Expand Up @@ -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' }
},
])
Expand All @@ -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'] }

Expand Down

0 comments on commit 5323a2a

Please sign in to comment.