Skip to content

Commit

Permalink
Copy SearchContextComponent to ServerItemPaginationComponent to ease …
Browse files Browse the repository at this point in the history
…BL8 upgrade path (#3421)

* Copy SearchContextComponent to ServerItemPaginationComponent to ease BL8 upgrade path

Closes #3313

* Keep using old component for backwards compatibility

- Test components

Co-authored-by: Jane Sandberg <[email protected]>

* Have copied component inherit from main component, just with updated signature

---------

Co-authored-by: Jane Sandberg <[email protected]>
  • Loading branch information
maxkadel and sandbergja authored Nov 5, 2024
1 parent 6847a50 commit 9409ccc
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class='pagination-search-widgets'>

<div class="page-links">
<%= link_to_previous_document %> |

<%= item_page_entry_info %> |

<%= link_to_next_document %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Blacklight
module SearchContext
class ServerItemPaginationComponent < Blacklight::SearchContextComponent
with_collection_parameter :search_context

def initialize(search_context:, search_session:, current_document:)
@search_context = search_context
@search_session = search_session
@current_document_id = current_document.id
end
end
end
end
1 change: 1 addition & 0 deletions app/components/blacklight/search_context_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class SearchContextComponent < Blacklight::Component
with_collection_parameter :search_context

def initialize(search_context:, search_session:)
Deprecation.warn("Blacklight::SearchContextComponent is deprecated and will be moved to Blacklight::SearchContext::ServerItemPaginationComponent in Blacklight 8.0.0")
@search_context = search_context
@search_session = search_session
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def item_page_entry_info
total: number_with_delimiter(search_session['total']),
count: search_session['total'].to_i).html_safe
end
deprecation_deprecate item_page_entry_info: 'Use Blacklight::SearchContextComponent methods instead'
deprecation_deprecate item_page_entry_info: 'Use Blacklight::SearchContext::ServerItemPaginationComponent methods instead'

##
# Look up search field user-displayable label
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def link_to_previous_document(previous_document, classes: 'previous', **addl_lin
tag.span raw(t('views.pagination.previous')), class: 'previous'
end
end
deprecation_deprecate link_to_previous_document: 'Moving to Blacklight::SearchContextComponent'
deprecation_deprecate link_to_previous_document: 'Moving to Blacklight::SearchContext::ServerItemPaginationComponent'

##
# Link to the next document in the current search context
Expand All @@ -69,7 +69,7 @@ def link_to_next_document(next_document, classes: 'next', **addl_link_opts)
tag.span raw(t('views.pagination.next')), class: 'next'
end
end
deprecation_deprecate link_to_previous_document: 'Moving to Blacklight::SearchContextComponent'
deprecation_deprecate link_to_previous_document: 'Moving to Blacklight::SearchContext::ServerItemPaginationComponent'

##
# Attributes for a link that gives a URL we can use to track clicks for the current search session
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_previous_next_doc.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<% Deprecation.warn(self, 'The partial _previous_next_doc.html.erb will be removed in 8.0. Render Blacklight::SearchContextComponent instead.') %>
<% Deprecation.warn(self, 'The partial _previous_next_doc.html.erb will be removed in 8.0. Render Blacklight::SearchContext::ServerItemPaginationComponent instead.') %>
<%= render(Blacklight::SearchContextComponent.new(search_context: @search_context, search_session: search_session)) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Blacklight::SearchContext::ServerItemPaginationComponent, type: :component do
subject(:render) { render_inline(instance) }

let(:current_document_id) { 9 }
let(:current_document) { SolrDocument.new(id: current_document_id) }
let(:search_session) { { 'document_id' => current_document_id, 'counter' => 1, 'total' => '3' } }
let(:instance) { described_class.new(search_context: search_context, search_session: search_session, current_document: current_document) }

before do
allow(controller).to receive(:search_session).and_return(search_session)
allow(controller).to receive(:current_search_session).and_return(double(id: current_document_id))
controller.class.helper_method :search_session
controller.class.helper_method :current_search_session
end

context 'when there is next and previous' do
let(:search_context) { { next: next_doc, prev: prev_doc } }
let(:prev_doc) { SolrDocument.new(id: '777') }
let(:next_doc) { SolrDocument.new(id: '888') }

before do
# allow(controller).to receive(:controller_tracking_method).and_return('track_catalog_path')

allow(controller).to receive_messages(controller_name: 'catalog', link_to_previous_document: '', link_to_next_document: '')
end

it "renders content" do
expect(render.css('.page-links').to_html).not_to be_blank
end
end
end
31 changes: 31 additions & 0 deletions spec/components/blacklight/search_context_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'spec_helper'
RSpec.describe Blacklight::SearchContextComponent, type: :component do
subject(:render) { render_inline(instance) }

let(:current_document_id) { 9 }
let(:search_session) { { 'document_id' => current_document_id, 'counter' => 1, 'total' => '3' } }
let(:instance) { described_class.new(search_context: search_context, search_session: search_session) }

before do
allow(controller).to receive(:search_session).and_return(search_session)
allow(controller).to receive(:current_search_session).and_return(double(id: current_document_id))
controller.class.helper_method :search_session
controller.class.helper_method :current_search_session
end

context 'when there is next and previous' do
let(:search_context) { { next: next_doc, prev: prev_doc } }
let(:prev_doc) { SolrDocument.new(id: '777') }
let(:next_doc) { SolrDocument.new(id: '888') }

before do
allow(controller).to receive_messages(controller_name: 'catalog', link_to_previous_document: '', link_to_next_document: '')
end

it "renders content" do
expect(render.css('.page-links').to_html).not_to be_blank
end
end
end

0 comments on commit 9409ccc

Please sign in to comment.