diff --git a/Gemfile b/Gemfile index c857d165..07606352 100644 --- a/Gemfile +++ b/Gemfile @@ -14,3 +14,5 @@ gem 'selenium-webdriver' gem 'sqlite3' gem 'webdrivers', require: false gem 'yard' + +gem 'geared_pagination', '~> 1.1' diff --git a/Gemfile.lock b/Gemfile.lock index ca1cc0c2..e85034fc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,7 +6,6 @@ PATH activejob (>= 6.0) activerecord (>= 6.0) job-iteration (~> 1.1) - pagy (~> 3.9) railties (>= 6.0) GEM @@ -89,6 +88,9 @@ GEM concurrent-ruby (1.1.8) crass (1.0.6) erubi (1.10.0) + geared_pagination (1.1.0) + activesupport (>= 5.0) + addressable (>= 2.5.0) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.8.9) @@ -112,7 +114,6 @@ GEM nokogiri (1.11.1) mini_portile2 (~> 2.5.0) racc (~> 1.4) - pagy (3.11.0) parallel (1.20.1) parser (2.7.2.0) ast (~> 2.4.1) @@ -206,6 +207,7 @@ PLATFORMS DEPENDENCIES capybara + geared_pagination (~> 1.1) maintenance_tasks! mocha pry-byebug diff --git a/app/controllers/maintenance_tasks/application_controller.rb b/app/controllers/maintenance_tasks/application_controller.rb index e15c030c..56b9e02a 100644 --- a/app/controllers/maintenance_tasks/application_controller.rb +++ b/app/controllers/maintenance_tasks/application_controller.rb @@ -5,8 +5,6 @@ module MaintenanceTasks # # Can be extended to add different authentication and authorization code. class ApplicationController < ActionController::Base - include Pagy::Backend - BULMA_CDN = 'https://cdn.jsdelivr.net' content_security_policy do |policy| diff --git a/app/controllers/maintenance_tasks/tasks_controller.rb b/app/controllers/maintenance_tasks/tasks_controller.rb index 7a05c86c..ba55c39b 100644 --- a/app/controllers/maintenance_tasks/tasks_controller.rb +++ b/app/controllers/maintenance_tasks/tasks_controller.rb @@ -20,7 +20,9 @@ def index def show @task = TaskData.find(params.fetch(:id)) set_refresh if @task.last_run&.active? - @pagy, @previous_runs = pagy(@task.previous_runs) + set_page_and_extract_portion_from(@task.previous_runs, + ordered_by: { created_at: :desc }) + set_paginated_headers end # Runs a given Task and redirects to the Task page. diff --git a/app/helpers/maintenance_tasks/application_helper.rb b/app/helpers/maintenance_tasks/application_helper.rb index 2a610dda..4f8755ea 100644 --- a/app/helpers/maintenance_tasks/application_helper.rb +++ b/app/helpers/maintenance_tasks/application_helper.rb @@ -4,15 +4,19 @@ module MaintenanceTasks # # @api private module ApplicationHelper - include Pagy::Frontend - - # Renders pagination for the page, if there is more than one page present. - # - # @param pagy [Pagy] the pagy instance containing pagination details, - # including the number of pages the results are spread across. - # @return [String] the HTML to render for pagination. - def pagination(pagy) - raw(pagy_bulma_nav(pagy)) if pagy.pages > 1 + # Renders pagination-related text showing the current page, total pages, + # and total number of records, if there is more than one page present. + # @param page [GearedPagination::Page] the page instance containing + # pagination details. + # @param record_type [String] the type of the recordset. + # @return [String] the HTML to render with the helpful pagination text. + def pagination_text(page, record_type) + page_count = page.recordset.page_count + if page_count > 1 + record_count = page.recordset.records_count + tag.span("Showing page #{page.number} of #{page_count} " \ + "(#{record_count} total #{record_type.pluralize(record_count)})") + end end # Renders a time element with the given datetime, worded as relative to the diff --git a/app/models/maintenance_tasks/task_data.rb b/app/models/maintenance_tasks/task_data.rb index 4d0bb3a7..b7ed7ff1 100644 --- a/app/models/maintenance_tasks/task_data.rb +++ b/app/models/maintenance_tasks/task_data.rb @@ -83,7 +83,7 @@ def code # @return [nil] if there are no Runs associated with the Task. def last_run return @last_run if defined?(@last_run) - @last_run = runs.first + @last_run = runs.last end # Returns the set of Run records associated with the Task previous to the @@ -135,7 +135,7 @@ def csv_task? private def runs - Run.where(task_name: name).with_attached_csv.order(created_at: :desc) + Run.where(task_name: name).with_attached_csv end end end diff --git a/app/views/maintenance_tasks/tasks/show.html.erb b/app/views/maintenance_tasks/tasks/show.html.erb index 3c6494ca..ed06231c 100644 --- a/app/views/maintenance_tasks/tasks/show.html.erb +++ b/app/views/maintenance_tasks/tasks/show.html.erb @@ -41,12 +41,11 @@
<%= highlight_code(code) %>
<% end %> -<% if @previous_runs.present? %> +<% if @task.previous_runs.any? %>

Previous Runs

- - <%= render @previous_runs %> - - <%= pagination(@pagy) %> + <%= render @page.records %> + <%= pagination_text(@page, 'run')%> + <%= link_to "Next page", task_path(@task, page: @page.next_param) unless @page.last? %> <% end %> diff --git a/config/initializers/geared_pagination.rb b/config/initializers/geared_pagination.rb new file mode 100644 index 00000000..96770334 --- /dev/null +++ b/config/initializers/geared_pagination.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +GearedPagination::Engine.config.after_initialize do + ActiveSupport.on_load(:action_controller) do + ActionController::Base.skip_after_action(:set_paginated_headers) + end +end diff --git a/lib/maintenance_tasks.rb b/lib/maintenance_tasks.rb index 0804aa3a..c8d741e9 100644 --- a/lib/maintenance_tasks.rb +++ b/lib/maintenance_tasks.rb @@ -6,8 +6,6 @@ require 'job-iteration' require 'maintenance_tasks/engine' -require 'pagy' -require 'pagy/extras/bulma' # Force the TaskJob class to load so we can verify upstream compatibility with # the JobIteration gem diff --git a/maintenance_tasks.gemspec b/maintenance_tasks.gemspec index 723975d1..e040abd8 100644 --- a/maintenance_tasks.gemspec +++ b/maintenance_tasks.gemspec @@ -26,6 +26,5 @@ Gem::Specification.new do |spec| spec.add_dependency('activejob', '>= 6.0') spec.add_dependency('activerecord', '>= 6.0') spec.add_dependency('job-iteration', '~> 1.1') - spec.add_dependency('pagy', '~> 3.9') spec.add_dependency('railties', '>= 6.0') end diff --git a/test/helpers/maintenance_tasks/application_helper_test.rb b/test/helpers/maintenance_tasks/application_helper_test.rb index c4bae936..8a4ba1c8 100644 --- a/test/helpers/maintenance_tasks/application_helper_test.rb +++ b/test/helpers/maintenance_tasks/application_helper_test.rb @@ -3,18 +3,23 @@ module MaintenanceTasks class ApplicationHelperTest < ActionView::TestCase - setup { @pagy = mock } + setup { @page = mock } - test '#pagination returns nil if pages is less than or equal to 1' do - @pagy.expects(pages: 1) - expects(:pagy_bulma_nav).never - assert_nil pagination(@pagy) + test '#pagination_text returns nil if pages is less than or equal to 1' do + @page.stubs(recordset: mock(page_count: 1)) + assert_nil pagination_text(@page, 'run') end - test '#pagination returns pagination element if pages is greater than 1' do - @pagy.expects(pages: 2) - expects(:pagy_bulma_nav).with(@pagy).returns('pagination') - assert_equal 'pagination', pagination(@pagy) + test '#pagination_text returns text with pagination info if pages is greater than 1' do + page_number = 1 + total_pages = 2 + records_count = 12 + @page.stubs( + number: page_number, + recordset: mock(page_count: total_pages, records_count: records_count) + ) + assert_equal 'Showing page 1 of 2 (12 total runs)', + pagination_text(@page, 'run') end test '#time_ago returns a time element with the given datetime worded as relative to now and ISO 8601 UTC time in title attribute' do diff --git a/test/models/maintenance_tasks/task_data_test.rb b/test/models/maintenance_tasks/task_data_test.rb index 26589e12..3346c73e 100644 --- a/test/models/maintenance_tasks/task_data_test.rb +++ b/test/models/maintenance_tasks/task_data_test.rb @@ -80,8 +80,8 @@ class TaskDataTest < ActiveSupport::TestCase task_data = TaskData.find('Maintenance::UpdatePostsTask') assert_equal 2, task_data.previous_runs.count - assert_equal run_2, task_data.previous_runs.first - assert_equal run_1, task_data.previous_runs.last + assert_equal run_1, task_data.previous_runs.first + assert_equal run_2, task_data.previous_runs.last end test '#previous_runs is empty when there are no Runs for the Task' do