Abusing the topic a little (if you prefer, I’ll open a new one).
Is there a way to move this expr to an individual module and use it like this?
I have this calculate
calculate :sales_total_amount, :integer do
calculation expr(
sum(:transactions,
field: :sell_price,
query: [
filter: [
expr(
fragment(
"st_dwithin(?, ?::geography, ?)",
property_geography,
^arg(:point),
^arg(:distance_in_meters)
) and
fragment(
"(? between ? and ?)",
buy_date,
^arg(:start_date),
^arg(:end_date)
) and
not is_nil(sell_price)
)
]
]
)
)
argument :start_date, :date, allow_nil?: false
argument :end_date, :date, allow_nil?: false
argument :distance_in_meters, :float, allow_nil?: false
argument :point, :geo_any, allow_nil?: false
end
I would like to have something like
calculate :sales_total_amount, :integer do
calculation CalculateHere
argument :start_date, :date, allow_nil?: false
argument :end_date, :date, allow_nil?: false
argument :distance_in_meters, :float, allow_nil?: false
argument :point, :geo_any, allow_nil?: false
end
and a module CalculateHere similar with Ash.Calculation like:
defmodule CalculateHere do
@moduledoc false
use Ash.Calculation
import Ash.Query
# it does not work, how to do it?
def select(query, _opts, _context) do
start_date = get_argument(query, :start_date)
end_date = Ash.Query.get_argument(query, :end_date)
query
|> Ash.Query.filter(
expr(
count(:transactions,
query: [
filter: [
# expr(buy_date > ^arg(:start_date) and buy_date < ^arg(:end_date))
expr(
fragment(
"(? between ? and ?)",
buy_date,
^start_date,
^end_date
) and ...
)
]
]
)
)
)
end
end
If you have an example or documentation talking about it, I would really appreciate it.