This is similar to what my token_operator (GitHub - baldwindavid/token_operator: Helper to make clean keyword APIs to Phoenix context functions · GitHub) package does though a bit more opinionated and probably easier to understand.
It is for this reason that I’ve mostly moved away from using the pattern in the controller in favor of explicit function references.
# controller
Calendar.list_reservations([
&Calendar.filter_reservations_by_room(&1, rooms),
&Calendar.filter_reservations_by_day(&1, datetime),
&Calendar.preload_reservation_owner_and_company/1,
&Calendar.preload_reservation_room_and_location/1
])
In the context, I just run each of those functions in succession.
# context
def list_reservations(queries \\ []) do
Reservation
|> (fn query -> Enum.reduce(queries, query, & &1.(&2)) end).()
|> Repo.all()
end
Okay, really I wrap that ugly reduce part in a utility, but you get the idea.






















