From 9409ccca77ce7af16cfc3f8650e68cce87db967f Mon Sep 17 00:00:00 2001 From: Max Kadel Date: Tue, 5 Nov 2024 11:27:16 -0500 Subject: [PATCH] Copy SearchContextComponent to ServerItemPaginationComponent to ease 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 * Have copied component inherit from main component, just with updated signature --------- Co-authored-by: Jane Sandberg --- .../server_item_pagination_component.html.erb | 10 ++++++ .../server_item_pagination_component.rb | 15 ++++++++ .../blacklight/search_context_component.rb | 1 + .../blacklight/catalog_helper_behavior.rb | 2 +- app/helpers/blacklight/url_helper_behavior.rb | 4 +-- app/views/catalog/_previous_next_doc.html.erb | 2 +- .../server_item_pagination_component_spec.rb | 35 +++++++++++++++++++ .../search_context_component_spec.rb | 31 ++++++++++++++++ 8 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 app/components/blacklight/search_context/server_item_pagination_component.html.erb create mode 100644 app/components/blacklight/search_context/server_item_pagination_component.rb create mode 100644 spec/components/blacklight/search_context/server_item_pagination_component_spec.rb create mode 100644 spec/components/blacklight/search_context_component_spec.rb diff --git a/app/components/blacklight/search_context/server_item_pagination_component.html.erb b/app/components/blacklight/search_context/server_item_pagination_component.html.erb new file mode 100644 index 0000000000..dbe9ae9fc4 --- /dev/null +++ b/app/components/blacklight/search_context/server_item_pagination_component.html.erb @@ -0,0 +1,10 @@ +
+ + +
diff --git a/app/components/blacklight/search_context/server_item_pagination_component.rb b/app/components/blacklight/search_context/server_item_pagination_component.rb new file mode 100644 index 0000000000..7c40238c92 --- /dev/null +++ b/app/components/blacklight/search_context/server_item_pagination_component.rb @@ -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 diff --git a/app/components/blacklight/search_context_component.rb b/app/components/blacklight/search_context_component.rb index e7d8bb59a3..4b1d990a36 100644 --- a/app/components/blacklight/search_context_component.rb +++ b/app/components/blacklight/search_context_component.rb @@ -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 diff --git a/app/helpers/blacklight/catalog_helper_behavior.rb b/app/helpers/blacklight/catalog_helper_behavior.rb index 9120846efc..05befd35ae 100644 --- a/app/helpers/blacklight/catalog_helper_behavior.rb +++ b/app/helpers/blacklight/catalog_helper_behavior.rb @@ -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 diff --git a/app/helpers/blacklight/url_helper_behavior.rb b/app/helpers/blacklight/url_helper_behavior.rb index 16b5ddb0b0..a5ecc82d80 100644 --- a/app/helpers/blacklight/url_helper_behavior.rb +++ b/app/helpers/blacklight/url_helper_behavior.rb @@ -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 @@ -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 diff --git a/app/views/catalog/_previous_next_doc.html.erb b/app/views/catalog/_previous_next_doc.html.erb index c0d32fd6a4..3c2fd23392 100644 --- a/app/views/catalog/_previous_next_doc.html.erb +++ b/app/views/catalog/_previous_next_doc.html.erb @@ -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)) %> diff --git a/spec/components/blacklight/search_context/server_item_pagination_component_spec.rb b/spec/components/blacklight/search_context/server_item_pagination_component_spec.rb new file mode 100644 index 0000000000..e75cb37ecc --- /dev/null +++ b/spec/components/blacklight/search_context/server_item_pagination_component_spec.rb @@ -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 diff --git a/spec/components/blacklight/search_context_component_spec.rb b/spec/components/blacklight/search_context_component_spec.rb new file mode 100644 index 0000000000..ae98be0db7 --- /dev/null +++ b/spec/components/blacklight/search_context_component_spec.rb @@ -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