Ash FrameworkAF
Ash Framework2mo ago
13 replies
Joan Gavelán

How to compose filters across multiple preparations?

I want FilterByRoutes and EnsureCurrentPlace to result in a single filter. I'm currently trying this but is not working:

# place.ex
read :list_map_places do
  argument :active_routes, {:array, :uuid}
  argument :current_place_id, :uuid

  prepare MyApp.Preparations.FilterByRoutes

  prepare MyApp.Preparations.EnsureCurrentPlace do
    where present(:current_place_id)
  end
end

# preparations/filter_by_routes.ex
def prepare(query, _opts, _context) do
  active_routes = query.arguments[:active_routes] || []
  Ash.Query.build(query, filter: expr(exists(routes, id in ^active_routes)))
end

# preparations/ensure_current_place.ex
def prepare(query, _opts, _context) do
  current_place_id = query.arguments.current_place_id
  Ash.Query.build(query, filter: expr(^query.filter or id == ^current_place_id))
end
Was this page helpful?