diff --git a/lib/paginator.ex b/lib/paginator.ex index a787192..25b74ae 100644 --- a/lib/paginator.ex +++ b/lib/paginator.ex @@ -236,14 +236,17 @@ defmodule Paginator do iex> Paginator.default_fetch_cursor_value(%Paginator.Customer{id: 1, address: %Paginator.Address{city: "London"}}, {:address, :city}) "London" + + iex> Paginator.default_fetch_cursor_value(%Paginator.Payment{id: 1, customer: %Paginator.Customer{id: 2}}, {:customer, :id}) + 2 """ @spec default_fetch_cursor_value(map(), atom() | {atom(), atom()}) :: any() def default_fetch_cursor_value(schema, {binding, field}) when is_atom(binding) and is_atom(field) do - case Map.get(schema, field) do - nil -> Map.get(schema, binding) |> Map.get(field) - value -> value + case Map.get(schema, binding) do + nil -> Map.get(schema, field) + assoc -> Map.get(assoc, field) end end diff --git a/test/paginator_test.exs b/test/paginator_test.exs index aff864c..fdf37e7 100644 --- a/test/paginator_test.exs +++ b/test/paginator_test.exs @@ -740,6 +740,9 @@ defmodule PaginatorTest do encode_cursor([p1.charged_at, p1.id]) assert Paginator.cursor_for_record(p7, amount: :asc) == encode_cursor([p7.amount]) + + assert Paginator.cursor_for_record(p7, [{{:customer, :id}, :asc}]) == + encode_cursor([p7.customer.id]) end test "per-record cursor generation with custom cursor value function", %{