Repository: woylie/flop_phoenix (elixir)

Issue:
function clause error in Flop.Phoenix.Pagination.pagination_type/1
### Summary

[info] GET /
[debug] Processing with Bla.Index.index/2
  Parameters: %{}
  Pipelines: [:browser, :require_authenticated_user]

(This is the content of flop in lib/flop_phoenix/pagination.ex:164)
%Flop{
  after: nil,
  before: nil,
  first: nil,
  last: nil,
  limit: 50,
  offset: nil,
  order_by: [:occurred_at],
  order_directions: [:desc, :asc],
  page: nil,
  page_size: nil,
  decoded_cursor: nil,
  filters: []
}
[info] Sent 500 in 81ms
[error] ** (FunctionClauseError) no function clause matching in Flop.Phoenix.Pagination.pagination_type/1
    (flop_phoenix 0.25.0) lib/flop_phoenix/pagination.ex:164: Flop.Phoenix.Pagination.pagination_type(%Flop{after: nil, before: nil, first: nil, last: nil, limit: 50, offset: nil, order_by: [:occurred_at], order_directions: [:desc, :asc], page: nil, page_size: nil, decoded_cursor: nil, filters: []})
    (flop_phoenix 0.25.0) lib/flop_phoenix/pagination.ex:113: Flop.Phoenix.Pagination.new/2
    (flop_phoenix 0.25.0) lib/flop_phoenix.ex:905: Flop.Phoenix."pagination_for (overridable 1)"/1
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/tag_engine.ex:96: Phoenix.LiveView.TagEngine.component/3
    (flop_phoenix 0.25.0) lib/flop_phoenix.ex:520: anonymous fn/2 in Flop.Phoenix."pagination (overridable 1)"/1
    (bla 1.4.0) bla/index.html.heex:82: Bla.Index.render/1
    (elixir 1.18.3) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/diff.ex:412: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/diff.ex:555: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
    (elixir 1.18.3) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/diff.ex:412: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/diff.ex:555: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
    (elixir 1.18.3) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/diff.ex:412: Phoenix.LiveView.Diff.traverse/7
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/diff.ex:136: Phoenix.LiveView.Diff.render/3
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/static.ex:289: Phoenix.LiveView.Static.to_rendered_content_tag/4
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/static.ex:171: Phoenix.LiveView.Static.do_render/4
    (phoenix_live_view 1.0.10) lib/phoenix_live_view/controller.ex:39: Phoenix.LiveView.Controller.live_render/3


### Steps to reproduce

1. ...

### Expected behaviour

_No response_

### Actual behaviour

_No response_

### Elixir/Erlang version

Elixir 1.18.3 (compiled with Erlang/OTP 27)

### Flop and Ecto versions

* ecto 3.12.5 (Hex package) (mix)
* ecto_sql 3.12.1 (Hex package) (mix)
  locked at 3.12.1 (ecto_sql) aff5b958
* flop 0.26.1 (Hex package) (mix)
  locked at 0.26.1 (flop) 5fcab8a1
* flop_phoenix 0.25.0 (https://github.com/woylie/flop_phoenix.git) (mix)

### Additional context

_No response_

Pull request description:
fix: handle nil offset
resolves #468

Patch:
diff --git a/lib/flop_phoenix/pagination.ex b/lib/flop_phoenix/pagination.ex
index bdba368..30f185e 100644
--- a/lib/flop_phoenix/pagination.ex
+++ b/lib/flop_phoenix/pagination.ex
@@ -160,11 +160,6 @@ defmodule Flop.Phoenix.Pagination do
     %__MODULE__{pagination_type: pagination_type(flop)}
   end
 
-  defp pagination_type(%Flop{limit: limit, offset: offset})
-       when is_integer(limit) and is_integer(offset) do
-    :offset
-  end
-
   defp pagination_type(%Flop{page: page, page_size: page_size})
        when is_integer(page) and is_integer(page_size) do
     :page
@@ -178,6 +173,10 @@ defmodule Flop.Phoenix.Pagination do
     :last
   end
 
+  defp pagination_type(%Flop{limit: limit}) when is_integer(limit) do
+    :offset
+  end
+
   defp build_page_path_fun(_meta, nil), do: fn _ -> nil end
 
   defp build_page_path_fun(meta, path) do


Test patch:
diff --git a/test/flop_phoenix_test.exs b/test/flop_phoenix_test.exs
index fe13eea..a563cdc 100644
--- a/test/flop_phoenix_test.exs
+++ b/test/flop_phoenix_test.exs
@@ -91,6 +91,21 @@ defmodule Flop.PhoenixTest do
              """) == []
     end
 
+    test "renders pagination with flop without offset" do
+      # offset can be nil if default_pagination_type is not set and no
+      # pagination parameters are passed
+      assigns = %{
+        meta: build(:meta_on_first_page, flop: %Flop{limit: 50, offset: nil})
+      }
+
+      html =
+        parse_heex(~H"""
+        <Flop.Phoenix.pagination meta={@meta} path="/pets" />
+        """)
+
+      assert find_one(html, "nav:root")
+    end
+
     test "allows to set global attributes" do
       assigns = %{meta: build(:meta_on_first_page)}