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

Fix #37 Page number should not be greater than total_pages #43

Merged
merged 1 commit into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
@spec paginate(Ecto.Query.t, Scrivener.Config.t) :: Scrivener.Page.t
def paginate(query, %Config{page_size: page_size, page_number: page_number, module: repo, caller: caller, options: options}) do
total_entries = Keyword.get_lazy(options, :total_entries, fn -> total_entries(query, repo, caller) end)
total_pages = total_pages(total_entries, page_size)
page_number = min(total_pages, page_number)

%Page{
page_size: page_size,
page_number: page_number,
entries: entries(query, repo, page_number, page_size, caller),
total_entries: total_entries,
total_pages: total_pages(total_entries, page_size)
total_pages: total_pages
}
end

Expand Down
19 changes: 19 additions & 0 deletions test/scrivener/paginator/ecto/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
assert page.total_entries == 130
end

test "will use total_pages if page_numer is too large" do
posts = create_posts()

config = %Scrivener.Config{
module: Scrivener.Ecto.Repo,
page_number: 2,
page_size: length(posts),
options: []
}

page =
Post
|> Post.published
|> Scrivener.paginate(config)

assert page.page_number == 1
assert page.entries == posts
end

test "can be used on a table with any primary key" do
create_key_values()

Expand Down