Skip to content
Open
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
8 changes: 6 additions & 2 deletions lib/perron/paginate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

module Perron
class Paginate
def initialize(collection, page:, per_page:, base_path: nil, page_path_template: nil, use_query_params: false)
def initialize(collection, page:, per_page:, base_path: "/", page_path_template: nil, use_query_params: false)
unless per_page.is_a?(Integer) && per_page.positive?
raise ArgumentError, "per_page must be a positive integer, got #{per_page.inspect}"
end

@collection = collection
@per_page = per_page
@base_path = base_path
@base_path = base_path || "/"
@page_path_template = page_path_template || "/page/:page/"
@use_query_params = use_query_params

Expand Down
28 changes: 28 additions & 0 deletions test/perron/paginate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,32 @@ class Perron::PaginateTest < ActiveSupport::TestCase

assert_equal "/articles/?page=2", paginate.previous
end

test "next falls back to a root-relative path when base_path is omitted" do
paginate = Perron::Paginate.new((1..10).to_a, page: 1, per_page: 5)

assert_equal "/page/2/", paginate.next
end

test "previous falls back to root when base_path is omitted" do
paginate = Perron::Paginate.new((1..10).to_a, page: 2, per_page: 5)

assert_equal "/", paginate.previous
end

test "an explicit nil base_path is treated as root" do
paginate = Perron::Paginate.new((1..10).to_a, page: 1, per_page: 5, base_path: nil)

assert_equal "/page/2/", paginate.next
end

test "raises ArgumentError for a non-positive or non-integer per_page" do
[0, -5, nil, 2.5].each do |bad|
error = assert_raises(ArgumentError) do
Perron::Paginate.new((1..10).to_a, page: 1, per_page: bad)
end

assert_match(/per_page must be a positive integer/, error.message)
end
end
end