From b187a00460a94bd9619ab9a0a47fa2dd55efa6a7 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 13 Oct 2023 09:26:47 -0500 Subject: [PATCH] Allow configuration of the kaminari pagination options This enables us to turn off deep pagination via the configuration or by passing arguments to the component --- .../response/pagination_component.rb | 7 ++- lib/blacklight/configuration.rb | 4 +- .../response/pagination_component_spec.rb | 53 +++++++++++++++++++ .../_paginate_compact.html.erb_spec.rb | 2 + 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 spec/components/blacklight/response/pagination_component_spec.rb diff --git a/app/components/blacklight/response/pagination_component.rb b/app/components/blacklight/response/pagination_component.rb index c95f7914ab..5eaa6fb825 100644 --- a/app/components/blacklight/response/pagination_component.rb +++ b/app/components/blacklight/response/pagination_component.rb @@ -17,7 +17,12 @@ def html_attr end def pagination - helpers.paginate @response, **Blacklight::Engine.config.blacklight.default_pagination_options, **@pagination_args + args = configured_options.merge(@pagination_args).compact + helpers.paginate @response, **args + end + + def configured_options + controller.blacklight_config.index.pagination_options end end end diff --git a/lib/blacklight/configuration.rb b/lib/blacklight/configuration.rb index 057e9a0d9d..4a55781402 100644 --- a/lib/blacklight/configuration.rb +++ b/lib/blacklight/configuration.rb @@ -175,7 +175,9 @@ def initialized_default_configuration? # component class used to render the search bar search_bar_component: nil, # component class used to render the header above the documents - search_header_component: Blacklight::SearchHeaderComponent + search_header_component: Blacklight::SearchHeaderComponent, + # pagination parameters to pass to kaminari + pagination_options: Blacklight::Engine.config.blacklight.default_pagination_options.dup ) # @!attribute show diff --git a/spec/components/blacklight/response/pagination_component_spec.rb b/spec/components/blacklight/response/pagination_component_spec.rb new file mode 100644 index 0000000000..3410becf98 --- /dev/null +++ b/spec/components/blacklight/response/pagination_component_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Blacklight::Response::PaginationComponent, type: :component do + let(:render) do + with_request_url '/catalog?q=foo' do + render_inline(instance) + end + end + + let(:instance) { described_class.new(response: response) } + + context 'when there are many results' do + let(:response) { instance_double(Blacklight::Solr::Response, total: 10, current_page: 5, limit_value: 10_000, total_pages: 100) } + + context 'with default config' do + before { render } + + it "has links to deep pages" do + expect(page).not_to have_link '98' + expect(page).to have_link '99' + expect(page).to have_link '100' + expect(page).not_to have_link '101' + end + end + + context 'when a different configuration that removes deep links is passed as a parameter' do + let(:instance) { described_class.new(response: response, left: 5, right: 0, outer_window: nil) } + + before { render } + + it "does not link to deep pages" do + expect(page).to have_link '1' + expect(page).not_to have_link '100' + end + end + + context 'when a different configuration that removes deep links is configured in the controller' do + before do + allow(controller.blacklight_config.index) + .to receive(:pagination_options) + .and_return(theme: 'blacklight', left: 5, right: 0) + render + end + + it "does not link to deep pages" do + expect(page).to have_link '1' + expect(page).not_to have_link '100' + end + end + end +end diff --git a/spec/views/catalog/_paginate_compact.html.erb_spec.rb b/spec/views/catalog/_paginate_compact.html.erb_spec.rb index 1082c0fd1c..c664efcd59 100644 --- a/spec/views/catalog/_paginate_compact.html.erb_spec.rb +++ b/spec/views/catalog/_paginate_compact.html.erb_spec.rb @@ -2,9 +2,11 @@ RSpec.describe "catalog/_paginate_compact.html.erb" do let(:user) { User.new { |u| u.save(validate: false) } } + let(:blacklight_config) { Blacklight::Configuration.new } before do controller.request.path_parameters[:action] = 'index' + allow(controller).to receive(:blacklight_config).and_return(blacklight_config) end it "renders paginatable arrays" do