Is there any template like feature in Ash.Resource action?

I have two similar actions like below.

create :create do
  change manage_relationship(:common1)
  change manage_relationship(:common2)
  change manage_relationship(:create_only)
end

update :update do
  change manage_relationship(:common1)
  change manage_relationship(:common2)
  change manage_relationship(:update_only)
end

Yeah, I can use global changes section to refactor these actions,
but then I cannot use AshPhoenix.Form auto?: true.
How can I refactor this?
Just use global changes and set form option manually?

Probably the simplest way would be with a macro. Extensions would likely be overkill for this particular case.

defmacrop shared_relationships do
  quote do
    change manage_relationship(:common1)
    change manage_relationship(:common2)
  end
end
create :create do
  shared_relationships()
  change manage_relationship(:create_only)
end

update :update do
  shared_relationships()
  change manage_relationship(:update_only)
end

We’ve explored and will likely have something dedicated for composing actions in this way at some point in the future.

1 Like