Passing arguments to calculation with Ash.Expr

Hi!

I’m using a query builder which I built based on AshPhoenix.FilterForm.

There I am dynamically creating filter-predicates with the help of Ash.Expr

this works fine for attributes, aggregates, calculations (w/o arguments):

e.g:

path=[]
field="full_name"
value="Zach Daniel"

f=Ash.Query.filter(resource, Ash.Expr.expr(^Ash.Expr.ref(path, field)) == ^value)

But what if I need to add calculation arguments with this syntax?

Again, this works fine if I hard-code calculation name and arguments:

value="Zach Daniel"
separator=" "

f=Ash.Query.filter(resource, full_name(separator: ^separator) == ^value )

➜ But how could I pass the calculation arguments with the Ash.Expr syntax above?

That’s a good question :slight_smile: TBH I don’t think we have a good way to do that.

I’ve pushed the first step towards improving this up to main if you’d like to try it out. This is not ergonomic at all, but it at least provides you a way to do it that should be stable. I’d like to add a shortcut for this in the future using the ref/2 function.

calculation = Ash.Query.Calculation.from_resource_calculation!(Resource, :name, args: %{...the arguments})

ref = %Ash.Query.Ref{attribute: calculation, relationship_path: path}

Ash.Query.filter(resource, ^ref == ^value)
1 Like

swift as always, you’re a machine !

thanks a lot, works for me.