Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to cursor-based pagination with geared_pagination #363

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ gem 'selenium-webdriver'
gem 'sqlite3'
gem 'webdrivers', require: false
gem 'yard'

gem 'geared_pagination', '~> 1.1'
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ PATH
activejob (>= 6.0)
activerecord (>= 6.0)
job-iteration (~> 1.1)
pagy (~> 3.9)
railties (>= 6.0)

GEM
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -206,6 +207,7 @@ PLATFORMS

DEPENDENCIES
capybara
geared_pagination (~> 1.1)
maintenance_tasks!
mocha
pry-byebug
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/maintenance_tasks/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/maintenance_tasks/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 13 additions & 9 deletions app/helpers/maintenance_tasks/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/maintenance_tasks/task_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
9 changes: 4 additions & 5 deletions app/views/maintenance_tasks/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@
<pre><code><%= highlight_code(code) %></code></pre>
<% end %>

<% if @previous_runs.present? %>
<% if @task.previous_runs.any? %>
<hr/>

<h4 class="title is-4">Previous Runs</h4>

<%= 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 %>
6 changes: 6 additions & 0 deletions config/initializers/geared_pagination.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this kind of implementation detail, I wonder if we're not better off implementing it ourselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see if I can mock something up!

end
end
2 changes: 0 additions & 2 deletions lib/maintenance_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion maintenance_tasks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 14 additions & 9 deletions test/helpers/maintenance_tasks/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<span>Showing page 1 of 2 (12 total runs)</span>',
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
Expand Down
4 changes: 2 additions & 2 deletions test/models/maintenance_tasks/task_data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down