Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Ash: 2 - Actions

ash_tutorial/actions.livemd

Ash: 2 - Actions

Application.put_env(:ash, :validate_domain_resource_inclusion?, false)
Application.put_env(:ash, :validate_domain_config_inclusion?, false)

Mix.install([{:ash, "~> 3.0"}], consolidate_protocols: false)

What is an Action?

Resources HomeQuerying

Actions are much simpler: an action describes a CRUD operation that can be performed on a resource, but using whatever language makes the most sense for your given resource. For example, you may have a publish action on a BlogPost resource that changes the resource to be publicly visible. Under CRUD, this would be an update operation, but calling it the “publish action” more fully conveys what the operation does.

An action is defined on a resource in an actions do ... end block.

  actions do
    create :create do
      accept [:name]
    end
  end

Adding that to our Resource module from earlier,

defmodule Tutorial.Profile do
  use Ash.Resource,
    domain: Tutorial

  attributes do
    uuid_primary_key :id
    attribute :name, :string
  end

  actions do
    create :create do
      accept [:name]
    end
  end
end

defmodule Tutorial do
  use Ash.Domain

  resources do
    resource Tutorial.Profile
  end
end

We can now use for_create/3 to create a changeset.

Tutorial.Profile
|> Ash.Changeset.for_create(:create, %{name: "My Name"})
Resources HomeQuerying