Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Last active August 3, 2018 01:39
Show Gist options
  • Save elbow-jason/9ed7b0a628a081c9e9c2db56235f6f95 to your computer and use it in GitHub Desktop.
Save elbow-jason/9ed7b0a628a081c9e9c2db56235f6f95 to your computer and use it in GitHub Desktop.
defmodule Bmgr.Transaction.SearchQuery do
import Ecto.Query
alias Bmgr.{Transaction}
@filters [:account, :status]
def build(filter_params) do
do_filter(Transaction, @filters, filter_params)
end
defp do_filter(query, filters, params) when is_list(filters) do
Enum.reduce(filters, query, fn (filter, acc) ->
do_filter(acc, filter, params)
end)
end
defp do_filter(query, :account, %{"account" => value}) do
from(t in query, preload: [entries: from(e in Entry, where: e.account_id == ^value)])
end
defp do_filter(query, :status, %{"status" => value}) do
from(t in query, where: t.status == ^value)
end
defp do_filter(query, _, _) do
query
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment