Skip to content
Draft
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
3 changes: 3 additions & 0 deletions lib/ash/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ defmodule Ash.Resource do
@impl Spark.Dsl
def handle_before_compile(_opts) do
quote do
Ash.Resource.TypeInference.change_dispatcher_definition(__MODULE__)
|> Code.eval_quoted([], __ENV__)

require Ash.Schema

Ash.Schema.define_schema()
Expand Down
33 changes: 24 additions & 9 deletions lib/ash/resource/change/change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ defmodule Ash.Resource.Change do
when this change was configured on a resource, and the context, which currently only has
the actor.
"""

require Ash.BehaviourHelpers

defstruct [
:change,
:on,
Expand Down Expand Up @@ -100,14 +103,18 @@ defmodule Ash.Resource.Change do
@spec change(module(), Ash.Changeset.t(), Keyword.t(), Ash.Resource.Change.Context.t()) ::
Ash.Changeset.t()
def change(module, changeset, opts, context) do
Ash.BehaviourHelpers.call_and_validate_return(
module,
:change,
[changeset, opts, context],
[%Ash.Changeset{}],
behaviour: __MODULE__,
callback_name: "change/3"
)
action_name = changeset.action && changeset.action.name

result =
changeset.resource.__ash_dispatch_change__(
action_name,
module,
changeset,
opts,
context
)

Ash.BehaviourHelpers.check_type!(module, result, [%Ash.Changeset{}])
end

@doc false
Expand Down Expand Up @@ -459,8 +466,16 @@ defmodule Ash.Resource.Change do
end
end

defmacro __before_compile__(_) do
defmacro __before_compile__(env) do
type_inference_definitions =
Ash.Resource.TypeInference.capture_definitions(env.module)

quote do
@doc false
def __ash_type_inference_definitions__ do
unquote(Macro.escape(type_inference_definitions))
end

if !Module.defines?(__MODULE__, {:change, 3}, :def) &&
!Module.defines?(__MODULE__, {:atomic, 3}, :def) &&
!Module.defines?(__MODULE__, {:batch_change, 3}, :def) do
Expand Down
1 change: 1 addition & 0 deletions lib/ash/resource/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ defmodule Ash.Resource.Dsl do
],
target: Ash.Resource.Actions.Action,
no_depend_modules: [:touches_resources, :error_handler],
lifted_function_transform: {Ash.Resource.TypeInference, :transform_lifted_function, []},
schema: Ash.Resource.Actions.Action.opt_schema(),
transform: {Ash.Resource.Actions.Action, :transform, []},
entities: [
Expand Down
13 changes: 13 additions & 0 deletions lib/ash/resource/preparation/preparation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ defmodule Ash.Resource.Preparation do
defmacro __using__(_) do
quote do
@behaviour Ash.Resource.Preparation
@before_compile Ash.Resource.Preparation

import Ash.Expr
require Ash.Query
Expand All @@ -155,4 +156,16 @@ defmodule Ash.Resource.Preparation do
defoverridable init: 1, supports: 1
end
end

defmacro __before_compile__(env) do
type_inference_definitions =
Ash.Resource.TypeInference.capture_definitions(env.module)

quote do
@doc false
def __ash_type_inference_definitions__ do
unquote(Macro.escape(type_inference_definitions))
end
end
end
end
Loading
Loading