Skip to content

Commit f40b1cf

Browse files
barmintorcbeer
authored andcommitted
add a page_links action to Blacklight::Searchable
- allows client-side management of show-view result set navigation
1 parent 9f67f71 commit f40b1cf

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

app/controllers/concerns/blacklight/search_context.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ def record_search_parameters opts = { only: :index }
1717
end
1818
end
1919

20+
# GET previous and next document json for the document specified by
21+
# the counter param in current search
22+
def page_links
23+
counter_param = params.delete(:counter)
24+
@page_link_data = {}
25+
if counter_param
26+
index = counter_param.to_i - 1
27+
response, documents = search_service.previous_and_next_documents_for_search index, search_state.reset_search
28+
if documents.detect(&:present?)
29+
@page_link_data[:prev] = page_links_document_path(documents.first, index)
30+
@page_link_data[:next] = page_links_document_path(documents.last, index + 2)
31+
end
32+
if response&.total && response.total.positive?
33+
@page_link_data[:counterRaw] = counter_param
34+
@page_link_data[:counterDelimited] = helpers.number_with_delimiter(counter_param.to_i)
35+
@page_link_data[:totalRaw] = response.total
36+
@page_link_data[:totalDelimited] = helpers.number_with_delimiter(response.total)
37+
end
38+
end
39+
render json: @page_link_data
40+
end
41+
2042
private
2143

2244
# sets up the session[:search] hash if it doesn't already exist
@@ -120,4 +142,14 @@ def setup_next_and_previous_documents
120142
logger&.warn "Unable to setup next and previous documents: #{e}"
121143
nil
122144
end
145+
146+
def page_links_document_path(document, counter)
147+
return nil unless document
148+
149+
if blacklight_config.view_config(:show).route
150+
search_state.url_for_document(document, counter: counter)
151+
else
152+
solr_document_path(document, counter: counter)
153+
end
154+
end
123155
end

lib/blacklight/routes/searchable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def initialize(defaults = {})
99
def call(mapper, _options = {})
1010
mapper.match '/', action: 'index', as: 'search', via: [:get, :post]
1111
mapper.get '/advanced', action: 'advanced_search', as: 'advanced_search'
12+
mapper.get '/page_links', action: 'page_links', as: 'page_links'
1213

1314
mapper.post ":id/track", action: 'track', as: 'track'
1415
mapper.get ":id/raw", action: 'raw', as: 'raw', defaults: { format: 'json' }

spec/controllers/catalog_controller_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,21 @@ def export_as_mock
919919
end
920920
end
921921
end
922+
923+
describe "page_links" do
924+
it "has prev/next docs and result set data for non-empty result sets", integration: true do
925+
get :page_links, params: { f: { "format" => 'Book' }, counter: 2 }
926+
expect(assigns(:page_link_data)).not_to be_empty
927+
expect(assigns(:page_link_data).fetch(:prev, nil)).to end_with('counter=1')
928+
expect(assigns(:page_link_data).fetch(:next, nil)).to end_with('counter=3')
929+
expect(assigns(:page_link_data).fetch(:totalRaw, nil)).to be 30
930+
end
931+
932+
it "is empty for empty result sets", integration: true do
933+
get :page_links, params: { f: { "format" => 'empty-result-set' }, counter: 1 }
934+
expect(assigns(:page_link_data)).to be_empty
935+
end
936+
end
922937
end
923938

924939
# there must be at least one facet, and each facet must have at least one value

spec/routing/catalog_routing_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
it "maps { :controller => 'catalog', :action => 'show', :id => 666 } to /catalog/666" do
2424
expect(get: "/catalog/666").to route_to(controller: 'catalog', action: 'show', id: "666")
2525
end
26+
27+
it "maps GET {:controller => 'catalog', :action => 'prev_next_documents'} to /catalog/page_links" do
28+
expect(get: "/catalog/page_links").to route_to(controller: 'catalog', action: 'page_links')
29+
end
2630
end
2731

2832
describe "solr_document_path for SolrDocument", test: true do

0 commit comments

Comments
 (0)