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

Improves default lookup of cursor values for one-level deep joins #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
9 changes: 6 additions & 3 deletions lib/paginator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions test/paginator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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", %{
Expand Down