andreas_ementio

andreas_ementio

Extensions like AshArchival in a Base Resource

I have a base resource which is use by almost all of the resources in the project, the base resource contains extensions like AshPostgres and AshArchival which seems to work just fine.

Now i am trying to add a base filter for AshArchival.
Since i have never done anything with marcos i am a bit confused on how to approach this.

The base resource looks like this:

defmodule App.BaseResource do
  # ...	

  defmacro __using__(opts) do
    opts =
      opts
      |> Keyword.merge(
        [extensions: [AshPaperTrail.Resource, AshArchival.Resource]],
        fn _key, extensions, base_extensions ->
          Enum.uniq(extensions ++ base_extensions)
        end
      )
      |> Keyword.put(:data_layer, AshPostgres.DataLayer)

    quote do
      use Ash.Resource, unquote(opts)

      paper_trail do
        change_tracking_mode :changes_only
        belongs_to_actor :user, App.Accounts.User, domain: App.Accounts
        store_action_name? true
      end

      attributes do
        create_timestamp :inserted_at
        update_timestamp :updated_at
      end

      # 
      # resource do
      #   base_filter expr(is_nil(archived_at)) # <-- archived_at is an undefined variable
      # end
      # actions do
      #   read :archived do
      #     filter expr(not is_nil(archived_at)) # <-- 
      #   end

      postgres do
        table App.BaseResource.tablename(__MODULE__)
        repo App.Repo
        base_filter_sql "archived_at IS NULL"
      end
    end
  end
end


Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Use a fragment instead, where you won’t have macro nesting issues.

use Ash.Resource, unquote(Keyword.put(opts, :fragments, App.BaseResource.Archival))
defmodule MyApp.BaseResource.Archival do
  use Spark.Dsl.Fragment, of: [Ash.Resource], extensions: [AshArchival]

  ...put all your archival config here, in addition to the base filter.
end

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement