How do I set default_accepts to a resource through Ash extension Transformer?

How do I set default_accepts dynamically using dsl in an extension? I want to dynamically add a field to default accepts in an extension. I searched in the documentation, but I could not find how to.

Something like the following but for the resource default accept: ash_double_entry/lib/account/transformers/add_structure.ex at v1.0.6 · ash-project/ash_double_entry · GitHub

Setting default_accept might be tricky, but you’ll want to do two main things:

  1. make sure the transformer that is doing this runs before the transfer who’s job is to “expand” the default accept.
# in your transformer module

@impl true
def before?(Ash.Resource.Transformers.DefaultAccept), do: true
def before?(_), do: false
  1. use Spark.Dsl.Transformer.set_option
Spark.Dsl.Transformer.set_option(dsl_state, [:actions], :default_accept], your_default_accept)
2 Likes

Thanks, @zachdaniel. I tried this but, it overrides the existing defaults. I was hoping that there’s a way of adding the field to existing default_accepts.

Ah, I see. Unfortunately, the way it’s designed is not conducive to that change right now. Because we have a single transformer that both sets the “default default accept” and sets the accept on each corresponding action. What you will have to do (not ideal, but the only good way to do it), is copy the logic from our core transformer for determining the default accept, and then add your fields. For example:

    public_attribute_names =
      dsl_state
      |> Transformer.get_entities([:attributes])
      |> Enum.filter(&(&1.public? && &1.writable?))
      |> Enum.map(& &1.name)

    default_default_accept =
      if Ash.Resource.Info.embedded?(dsl_state) do
        :*
      else
        []
      end

    default_accept =
      Transformer.get_option(
        dsl_state,
        [:actions],
        :default_accept
      ) || default_default_accept

    dsl_state =
      Transformer.set_option(dsl_state, [:actions], :default_accept, Enum.uniq(default_accept ++ your_adding_attrs))

The code is adapted from here: Ash.Resource.Transformers.DefaultAccept — ash v2.21.15