mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 21:13:10 +12:00
fi: fetch_count/4
use the query after applying before_action
hooks (#922)
--------- Co-authored-by: Zach Daniel <zachary.s.daniel@gmail.com>
This commit is contained in:
parent
b75327445c
commit
7d7bcaa3d7
2 changed files with 18 additions and 2 deletions
|
@ -314,7 +314,6 @@ defmodule Ash.Actions.Read do
|
|||
pre_authorization_query <- query,
|
||||
{:ok, query} <- authorize_query(query, opts),
|
||||
query_before_pagination <- query,
|
||||
{:ok, query} <- paginate(query, action, opts[:page], opts[:skip_pagination?]),
|
||||
query <-
|
||||
Ash.Actions.Read.Calculations.deselect_known_forbidden_fields(
|
||||
query,
|
||||
|
@ -376,6 +375,7 @@ defmodule Ash.Actions.Read do
|
|||
),
|
||||
query <- Map.put(query, :filter, filter),
|
||||
query <- Ash.Query.unset(query, :calculations),
|
||||
{%{valid?: true} = query, before_notifications} <- run_before_action(query),
|
||||
{:ok, count} <-
|
||||
fetch_count(
|
||||
query,
|
||||
|
@ -383,7 +383,7 @@ defmodule Ash.Actions.Read do
|
|||
relationship_path_filters,
|
||||
opts
|
||||
),
|
||||
{%{valid?: true} = query, before_notifications} <- run_before_action(query),
|
||||
{:ok, query} <- paginate(query, action, opts[:page], opts[:skip_pagination?]),
|
||||
{:ok, data_layer_query} <-
|
||||
Ash.Query.data_layer_query(query, data_layer_calculations: data_layer_calculations),
|
||||
{:ok, results} <-
|
||||
|
|
|
@ -61,6 +61,11 @@ defmodule Ash.Actions.PaginationTest do
|
|||
pagination keyset?: true, countable: true
|
||||
end
|
||||
|
||||
read :keyset_before_action do
|
||||
prepare(before_action(&Ash.Query.filter(&1, name in ["0", "1", "2"])))
|
||||
pagination keyset?: true, countable: true
|
||||
end
|
||||
|
||||
read :optional_keyset do
|
||||
pagination keyset?: true, countable: true, required?: false
|
||||
end
|
||||
|
@ -483,6 +488,17 @@ defmodule Ash.Actions.PaginationTest do
|
|||
Api.read!(User, action: :keyset, page: [count: true, limit: 1, after: keyset])
|
||||
end
|
||||
|
||||
test "can get the full count when asking for records after a specific keyset use the query after applying `before_action` hooks" do
|
||||
%{results: [%{__metadata__: %{keyset: keyset}}], count: 3} =
|
||||
Api.read!(User, action: :keyset_before_action, page: [count: true, limit: 1])
|
||||
|
||||
assert %{count: 3} =
|
||||
Api.read!(User,
|
||||
action: :keyset_before_action,
|
||||
page: [count: true, limit: 1, after: keyset]
|
||||
)
|
||||
end
|
||||
|
||||
test "an invalid keyset returns an appropriate error" do
|
||||
assert_raise(Ash.Error.Invalid, ~r/Invalid value provided as a keyset/, fn ->
|
||||
Api.read!(User, action: :keyset, page: [limit: 1, after: "~"])
|
||||
|
|
Loading…
Reference in a new issue