Skip to content

Instantly share code, notes, and snippets.

@bradpauly
Created February 24, 2023 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradpauly/33aa7f1da6bfda9a5b00a53a6d8b90c9 to your computer and use it in GitHub Desktop.
Save bradpauly/33aa7f1da6bfda9a5b00a53a6d8b90c9 to your computer and use it in GitHub Desktop.
require "minitest/autorun"
require "minitest/spec"
class Pager
def initialize(results:, rows_per_page:)
@results = results
@rows_per_page = rows_per_page
end
def total_pages
(@results.count/@rows_per_page.to_f).ceil
end
end
describe Pager do
describe "#total_pages" do
it "rounds up to the next whole number" do
# This will give us an Array with elements 1-33, i.e. [1, 2, 3, ...]
results = (1..33).to_a
pager = Pager.new(results: results, rows_per_page: 10)
assert_equal pager.total_pages, 4
end
it "returns 1 when there are less than rows_per_page" do
results = (1..5).to_a
pager = Pager.new(results: results, rows_per_page: 10)
assert_equal pager.total_pages, 1
end
it "returns 0 when there are 0 rows" do
results = []
pager = Pager.new(results: results, rows_per_page: 10)
assert_equal pager.total_pages, 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment