viniciuslm

viniciuslm

How use filter with ash.calculation?

defmodule Project.Calculations.ActiveCustomer do
  use Ash.Calculation

  alias Project.Customer
  
  @impl true
  def calculate(records, _opts, %{}) do
    Enum.map(records, fn record ->
      active_customer?(record)
    end)
  end

  defp active_customer?(%Client{amount: amount, status: status})
       when amount > 0 and status == :active,
       do: true

  defp active_customer?(_), do: false
end
efmodule Project.Customer do
  use Ash.Resource, data_layer: AshPostgres.DataLayer

  alias Project.Calculations.ActiveCustomer

  postgres do
    table("customer")

    repo(Project.Repo)
  end

  attributes do
    uuid_primary_key :id
    create_timestamp :inserted_at
    update_timestamp :updated_at
    attribute :name, :string, do: allow_nil? false
    attribute :identificacao_cliente, :string, do: allow_nil? false 
    attribute :status, :boolean, do: allow_nil? false

    attribute :amount, :integer do
      allow_nil? false
      default 0
    end
  end


  code_interface do
    define_for(Project.Operaction)
    define :by_id_and_active_customer, args: [:id], action: :by_id_and_active_customer
  end

  actions do
    defaults([:read])

    read :by_id_and_active_customer do
      prepare(build(load: :active_customer?))
      argument(:id, :uuid, allow_nil?: false)
      get?(true)
      filter(expr(id == ^arg(:id) and active_customer? == true))
    end
  end

  calculations do
    calculate :active_customer?, :boolean, ActiveCustomer
  end
end

when execute Project.Customer.by_id_and_active_customer “48804d19-4747-45ae-96c3-9738cf724f4d”, than show an error:

(exit) an exception was raised:
    ** (UndefinedFunctionError) function Project.Calculations.ActiveCustomer.expression/2 is undefined or private

I readed the manual, but it haven’t example for use with expression.

https://hexdocs.pm/ash/Ash.Calculation.html

I need to use calculation, because, i have more code. I simplified the code.

Can someone help me?

Most Liked

zachdaniel

zachdaniel

Creator of Ash

Correct, you cannot use that in a filter. You can do a lot using expressions, so consider if that is possible. Keep in mind that you can use fragment to embed raw sql into an expression, i.e expr(fragment("SOME ? SQL ? WITH ? SAFE ? INTERPOLATION", field1, field2))

Where Next?

Popular in Questions 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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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 31265 143
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

We're in Beta

About us Mission Statement