pass attribute to expression in calculation

I have the following expression in a calculation. This works if the resource has the attribute starts_at. But how to make this expression generic that any attribute can be used.
use Ash.Resource.Calculation

def expression(_opts, _context) do
expr(fragment("to_char(?,'DD.MM.YYYY')", starts_at))
end
use Ash.Resource.Calculation

def expression(_opts, _context) do
expr(fragment("to_char(?,'DD.MM.YYYY')", starts_at))
end
Ive tried passing :starts_at in opts but with arguments: %{field: :starts_at}. but how it is not clear how to use it
calculate :display_starts_at,
:string,
{Project.Calculations.FormatDatetime, field: :starts_at}
calculate :display_starts_at,
:string,
{Project.Calculations.FormatDatetime, field: :starts_at}
2 Replies
sevenseacat
sevenseacat5w ago
it'll be a ref - eg. ref(opts[:field]) I think
Rise
RiseOP5w ago
yes that works!
I am still a bit shaky when it comes to how and when to use ref,arg, parent and i think there are some more.

Did you find this page helpful?