mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
test: add a test for calc -> calc -> attribute chain
chore: simplify some code
This commit is contained in:
parent
bacd54a01b
commit
1db7025b41
2 changed files with 40 additions and 18 deletions
|
@ -964,23 +964,12 @@ defmodule Ash.Query do
|
|||
|> Enum.map(fn
|
||||
{load, args} ->
|
||||
if resource_calculation = Ash.Resource.Info.calculation(query.resource, load) do
|
||||
{module, opts} = resource_calculation.calculation
|
||||
|
||||
with {:ok, args} <- validate_calculation_arguments(resource_calculation, args),
|
||||
{:ok, calculation} <-
|
||||
Calculation.new(
|
||||
resource_calculation.name,
|
||||
module,
|
||||
opts,
|
||||
{resource_calculation.type, resource_calculation.constraints},
|
||||
Map.put(args, :context, query.context),
|
||||
resource_calculation.filterable?,
|
||||
resource_calculation.load
|
||||
) do
|
||||
select_and_load_calc(resource_calculation, %{calculation | load: load}, query)
|
||||
else
|
||||
_ ->
|
||||
case resource_calc_to_calc(query, load, resource_calculation, args) do
|
||||
{:error, _} ->
|
||||
{load, args}
|
||||
|
||||
{:ok, calc} ->
|
||||
calc
|
||||
end
|
||||
else
|
||||
if relationship = Ash.Resource.Info.relationship(query.resource, load) do
|
||||
|
@ -1015,9 +1004,9 @@ defmodule Ash.Query do
|
|||
end
|
||||
|
||||
@doc false
|
||||
def resource_calc_to_calc(query, name, resource_calculation) do
|
||||
def resource_calc_to_calc(query, name, resource_calculation, args \\ %{}) do
|
||||
with %{calculation: {module, opts}} <- resource_calculation,
|
||||
{:ok, args} <- validate_calculation_arguments(resource_calculation, %{}),
|
||||
{:ok, args} <- validate_calculation_arguments(resource_calculation, args),
|
||||
{:ok, calculation} <-
|
||||
Calculation.new(
|
||||
resource_calculation.name,
|
||||
|
|
|
@ -162,6 +162,24 @@ defmodule Ash.Test.CalculationTest do
|
|||
end
|
||||
end
|
||||
|
||||
defmodule FullNamePlusFirstName do
|
||||
use Ash.Calculation
|
||||
|
||||
def load(_, _, _) do
|
||||
[:full_name]
|
||||
end
|
||||
|
||||
def select(_, _, _) do
|
||||
[:first_name]
|
||||
end
|
||||
|
||||
def calculate(records, _, _) do
|
||||
Enum.map(records, fn record ->
|
||||
record.full_name <> " #{record.first_name}"
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule FriendsNames do
|
||||
use Ash.Calculation
|
||||
|
||||
|
@ -231,6 +249,8 @@ defmodule Ash.Test.CalculationTest do
|
|||
:string,
|
||||
BestFriendsFirstNamePlusStuff
|
||||
|
||||
calculate :full_name_plus_first_name, :string, FullNamePlusFirstName
|
||||
|
||||
calculate :full_name_plus_full_name,
|
||||
:string,
|
||||
{ConcatWithLoad, keys: [:full_name, :full_name]}
|
||||
|
@ -697,4 +717,17 @@ defmodule Ash.Test.CalculationTest do
|
|||
|
||||
assert best_friends_best_friends_first_names == ["bf: ", "bf: "]
|
||||
end
|
||||
|
||||
test "loading a calculation with selects that loads a calculation with selects works" do
|
||||
full_names_plus_first_names =
|
||||
User
|
||||
|> Ash.Query.select([])
|
||||
|> Ash.Query.load([
|
||||
:full_names_plus_first_names
|
||||
])
|
||||
|> Api.read!()
|
||||
|> Enum.map(& &1.full_names_plus_first_names)
|
||||
|> Enum.sort()
|
||||
|> IO.inspect()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue