Ash FrameworkAF
Ash Framework4mo ago
9 replies
edgar

No actor in after_action context

I wondering what is the correct or recommended way of passing the actor down to a after_change callback. I've tried to use the context but the actor was always
nil
. I've ended up doing this:

  actions do
    defaults [:read, :destroy]

    create :create do
      primary? true
      accept [:name, :description, :metadata, :warehouse_id]

      validate present(:name)
      validate present(:warehouse_id)

      change after_action(fn changeset, model, _context ->
               # Access actor from the correct location in changeset.context
               case get_in(changeset.context, [:private, :actor]) do
                 nil ->
                   {:error, "Actor context is required for creating initial model version"}

                 actor ->
                   # Create initial empty version
                   empty_model = %{
                     "entities" => [],
                     "entity_relationships" => [],
                     "metadata" => %{},
                     "version_notes" => "Initial empty model"
                   }

                   _version =
                     Astrobee.DataPipeline.ModelVersion
                     |> Ash.Changeset.for_create(:create, %{
                       ontology_id: model.id,
                       model: empty_model,
                       commit_message: "Initial version"
                     })
                     |> Ash.create!(actor: actor)

                   {:ok, model}
               end
             end)
    end
  end


But I'm not sure if I'm doing it properly and I'm missing something here?
Was this page helpful?