Skip to content

Commit

Permalink
Merge pull request #329 from woylie/fix/page-range
Browse files Browse the repository at this point in the history
fix/page range
  • Loading branch information
woylie authored Mar 23, 2024
2 parents beac1e2 + 28b8353 commit 8a05a28
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

- Don't render empty `style` attributes on `col` elements in `colgroup`.

### Fixed

- The page range calculation in the `Flop.Phoenix.pagination/1` was incorrect
towards the last pages.

## [0.22.7] - 2024-03-02

### Changed
Expand Down
2 changes: 1 addition & 1 deletion lib/flop_phoenix/pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Flop.Phoenix.Pagination do
max_pages >= total_pages ->
1..total_pages

current_page + additional >= total_pages ->
current_page + additional > total_pages ->
(total_pages - max_pages + 1)..total_pages

true ->
Expand Down
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ defmodule FlopPhoenix.MixProject do
{:ex_doc, "~> 0.21", only: :dev, runtime: false},
{:ex_machina, "~> 2.4", only: :test},
{:excoveralls, "~> 0.10", only: :test},
# Floki >= 0.36 uses Keyword.validate/2, which was introduced in
# Elixir 1.13
{:floki, "~> 0.35.0", only: :test},
{:flop, "~> 0.23.0 or ~> 0.24.0 or ~> 0.25.0"},
{:jason, "~> 1.0", only: [:dev, :test]},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%{
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"castore": {:hex, :castore, "1.0.5", "9eeebb394cc9a0f3ae56b813459f990abb0a3dedee1be6b27fdb50301930502f", [:mix], [], "hexpm", "8d7c597c3e4a64c395980882d4bca3cebb8d74197c590dc272cfd3b6a6310578"},
"castore": {:hex, :castore, "1.0.6", "ffc42f110ebfdafab0ea159cd43d31365fa0af0ce4a02ecebf1707ae619ee727", [:mix], [], "hexpm", "374c6e7ca752296be3d6780a6d5b922854ffcc74123da90f2f328996b962d33a"},
"credo": {:hex, :credo, "1.7.5", "643213503b1c766ec0496d828c90c424471ea54da77c8a168c725686377b9545", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f799e9b5cd1891577d8c773d245668aa74a2fcd15eb277f51a0131690ebfb3fd"},
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"},
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"ecto": {:hex, :ecto, "3.11.1", "4b4972b717e7ca83d30121b12998f5fcdc62ba0ed4f20fd390f16f3270d85c3e", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ebd3d3772cd0dfcd8d772659e41ed527c28b2a8bde4b00fe03e0463da0f1983b"},
"ecto": {:hex, :ecto, "3.11.2", "e1d26be989db350a633667c5cda9c3d115ae779b66da567c68c80cfb26a8c9ee", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c38bca2c6f8d8023f2145326cc8a80100c3ffe4dcbd9842ff867f7fc6156c65"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.31.2", "8b06d0a5ac69e1a54df35519c951f1f44a7b7ca9a5bb7a260cd8a174d6322ece", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "317346c14febaba9ca40fd97b5b5919f7751fb85d399cc8e7e8872049f37e0af"},
"ex_machina": {:hex, :ex_machina, "2.7.0", "b792cc3127fd0680fecdb6299235b4727a4944a09ff0fa904cc639272cd92dc7", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "419aa7a39bde11894c87a615c4ecaa52d8f107bbdd81d810465186f783245bf8"},
Expand Down
37 changes: 37 additions & 0 deletions test/flop/pagination_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule Flop.Phoenix.PaginationTest do
use ExUnit.Case

alias Flop.Phoenix.Pagination

describe "get_page_link_range/3" do
test "returns page range with odd max pages" do
assert Pagination.get_page_link_range(1, 3, 10) == 1..3
assert Pagination.get_page_link_range(2, 3, 10) == 1..3
assert Pagination.get_page_link_range(3, 3, 10) == 2..4
assert Pagination.get_page_link_range(4, 3, 10) == 3..5
assert Pagination.get_page_link_range(5, 3, 10) == 4..6
assert Pagination.get_page_link_range(6, 3, 10) == 5..7
assert Pagination.get_page_link_range(7, 3, 10) == 6..8
assert Pagination.get_page_link_range(8, 3, 10) == 7..9
assert Pagination.get_page_link_range(9, 3, 10) == 8..10
assert Pagination.get_page_link_range(10, 3, 10) == 8..10
end

test "returns page range with even max pages" do
assert Pagination.get_page_link_range(1, 4, 10) == 1..4
assert Pagination.get_page_link_range(2, 4, 10) == 1..4
assert Pagination.get_page_link_range(3, 4, 10) == 2..5
assert Pagination.get_page_link_range(4, 4, 10) == 3..6
assert Pagination.get_page_link_range(5, 4, 10) == 4..7
assert Pagination.get_page_link_range(6, 4, 10) == 5..8
assert Pagination.get_page_link_range(7, 4, 10) == 6..9
assert Pagination.get_page_link_range(8, 4, 10) == 7..10
assert Pagination.get_page_link_range(9, 4, 10) == 7..10
assert Pagination.get_page_link_range(10, 4, 10) == 7..10
end

test "does not return range beyond total pages" do
assert Pagination.get_page_link_range(1, 3, 2) == 1..2
end
end
end

0 comments on commit 8a05a28

Please sign in to comment.