Ash FrameworkAF
Ash Framework3y ago
49 replies
Blibs

Action macro with Ash queries

hey Zack, in the #ideas channel, you gave the idea of creating a macro in case I want to move an action logic to another module https://discord.com/channels/711271361523351632/712035333432147989/1075932613061116036

I did that and it seems to work great, but when I add some Ash.Query macros to it, then it stops working. The obvious reason should be that the macro is not finding require Ash.Query but I can't find a way to add it to make it work. This is what I have right now:

defmodule Marketplace.Markets.Property.Offer.Actions.MakeOwnOffersOld do
  @moduledoc false

  defmacro read(name: name) do
    quote location: :keep do
      read unquote(name) do
        argument :property_id, :uuid, allow_nil?: false

        filter expr(
                 status == :submitted and offeror_id == ^actor(:id) and
                   property_id == ^arg(:property_id)
               )

        manual fn _, query, _ ->
          {_, updated_offers} = Marketplace.Repo.update_all(query, set: [status: :old])

          {:ok, updated_offers}
        end
      end
    end
  end
end


defmodule Marketplace.Markets.Property.Offer.Actions do
  alias Marketplace.Markets.Property.Offer.Actions

  defmacro __using__(_opts) do
    quote location: :keep do
      require Actions.MakeOwnOffersOld
    end
  end
end


defmodule Marketplace.Markets.Property.Offer do
  alias Marketplace.Markets.Property.Offer.Actions

  use Actions

  use Ash.Resource,
    ...

  actions do
    Actions.MakeOwnOffersOld.read(name: :make_own_offers_old)
  end

  ...
end
Was this page helpful?