From bae2aa2ad254d440c2c3bec97d41f3209a48156f Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 18 Jul 2023 13:02:29 -0400 Subject: [PATCH] fix: only depend on calcs in runtime calcs --- lib/ash/actions/read.ex | 20 ++++++++++++++++---- lib/ash/code_interface.ex | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/ash/actions/read.ex b/lib/ash/actions/read.ex index daa4f3e7..36098551 100644 --- a/lib/ash/actions/read.ex +++ b/lib/ash/actions/read.ex @@ -1120,6 +1120,7 @@ defmodule Ash.Actions.Read do calculation_dependency_requests( ash_query, calculation_dependencies, + calculations_at_runtime, request_opts, path, error_path, @@ -1640,6 +1641,7 @@ defmodule Ash.Actions.Read do defp calculation_dependency_requests( query, calculation_deps, + runtime_calculations, request_opts, path, error_path, @@ -1679,12 +1681,12 @@ defmodule Ash.Actions.Read do [] :calculation -> - relationship_path = Enum.map(dep.path, &elem(&1, 0)) + if dep.path == [] && calculation_in_runtime_calcs?(dep, runtime_calculations) do + relationship_path = Enum.map(dep.path, &elem(&1, 0)) - actual_data_path = - path ++ [:calculation_results, {dep.calculation.name, dep.calculation.load}] + actual_data_path = + path ++ [:calculation_results, {dep.calculation.name, dep.calculation.load}] - if dep.path == [] do [ Request.new( resource: query.resource, @@ -1934,6 +1936,16 @@ defmodule Ash.Actions.Read do end) end + def calculation_in_runtime_calcs?(dep, runtime_calculations) do + Enum.any?(runtime_calculations, fn runtime_calc -> + dep.calculation.load == runtime_calc.load and + dep.calculation.module == runtime_calc.module and + dep.calculation.name == runtime_calc.name and + clean_calc_context(dep.calculation) == + clean_calc_context(runtime_calc) + end) + end + defp calc_dep_path(path, dep) do path ++ [:calc_dep, dep, :data] end diff --git a/lib/ash/code_interface.ex b/lib/ash/code_interface.ex index 823f5044..5c105448 100644 --- a/lib/ash/code_interface.ex +++ b/lib/ash/code_interface.ex @@ -550,7 +550,7 @@ defmodule Ash.CodeInterface do :update -> subject = quote do: changeset - subject_args = quote do: [record] + subject_args = quote do: [%{__struct__: unquote(resource)} = record] resolve_subject = quote do @@ -569,7 +569,7 @@ defmodule Ash.CodeInterface do :destroy -> subject = quote do: changeset - subject_args = quote do: [record] + subject_args = quote do: [%{__struct__: unquote(resource)} = record] resolve_subject = quote do