Skip to content

Commit

Permalink
Adopt JSON-API conventions for search results serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Apr 27, 2017
1 parent 1f6501e commit f0eb87a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
18 changes: 17 additions & 1 deletion app/views/catalog/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
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
json.next url_for(search_state.to_h.merge(only_path: false, page: @response.next_page.to_s)) if @response.next_page
json.last url_for(search_state.to_h.merge(only_path: false, page: @response.total_pages.to_s))
end

json.data do
json.array! @presenter.documents do |document|
json.id document.id
json.attributes document
json.links do
json.self polymorphic_url(url_for_document(document))
end
end
end

json.response do
json.docs @presenter.documents
json.facets @presenter.search_facets_as_json
json.pages @presenter.pagination_info
end
1 change: 0 additions & 1 deletion spec/features/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,3 @@
expect(page).to have_content "No results found for your search"
end
end

48 changes: 33 additions & 15 deletions spec/views/catalog/index.json.jbuilder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
# frozen_string_literal: true
RSpec.describe "catalog/index.json" do
let(:response) { instance_double(Blacklight::Solr::Response, documents: docs) }
let(:docs) { [{ id: '123', title_t: 'Book1' }, { id: '456', title_t: 'Book2' }] }
let(:response) { instance_double(Blacklight::Solr::Response, documents: docs, prev_page: nil, next_page: 2, total_pages: 3) }
let(:docs) { [SolrDocument.new(id: '123', title_t: 'Book1'), SolrDocument.new(id: '456', title_t: 'Book2')] }
let(:facets) { double("facets") }
let(:config) { instance_double(Blacklight::Configuration) }
let(:config) { Blacklight::Configuration.new }
let(:presenter) { Blacklight::JsonPresenter.new(response, facets, config) }

it "renders index json" do
allow(view).to receive(:blacklight_config).and_return(config)
allow(presenter).to receive(:pagination_info).and_return({ current_page: 1, next_page: 2,
prev_page: nil })
allow(presenter).to receive(:search_facets_as_json).and_return(
[{ name: "format", label: "Format",
items: [{ value: 'Book', hits: 30, label: 'Book' }] }])
assign :presenter, presenter
assign :response, response
render template: "catalog/index.json", format: :json
hash = JSON.parse(rendered)
expect(hash).to eq('response' => { 'docs' => [{ 'id' => '123', 'title_t' => 'Book1' },
{ 'id' => '456', 'title_t' => 'Book2' }],
'facets' => [{ 'name' => "format", 'label' => "Format",
'items' => [
{ 'value' => 'Book',
'hits' => 30,
'label' => 'Book' }] }],
'pages' => { 'current_page' => 1,
'next_page' => 2,
'prev_page' => nil } }
)
hash = JSON.parse(rendered).with_indifferent_access
expect(hash).to include(links: hash_including(self: 'http://test.host/', next: 'http://test.host/?page=2', last: 'http://test.host/?page=3'))
expect(hash).to include(response: hash_including(facets: [
{
'name' => "format", 'label' => "Format",
'items' => [
{ 'value' => 'Book',
'hits' => 30,
'label' => 'Book' }]
}]))
expect(hash).to include(response: hash_including(pages:
{
'current_page' => 1,
'next_page' => 2,
'prev_page' => nil
}))
expect(hash).to include(data: [
{
id: '123',
attributes: { 'id' => '123', 'title_t' => 'Book1' },
links: { self: 'http://test.host/catalog/123' }
},
{
id: '456',
attributes: { 'id' => '456', 'title_t' => 'Book2' },
links: { self: 'http://test.host/catalog/456' }
},
])
end
end

0 comments on commit f0eb87a

Please sign in to comment.