Skip to content

Instantly share code, notes, and snippets.

@tosbourn
Created June 5, 2017 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tosbourn/caa2b2864ed8db2b926cf58c78c9ed5d to your computer and use it in GitHub Desktop.
Save tosbourn/caa2b2864ed8db2b926cf58c78c9ed5d to your computer and use it in GitHub Desktop.
def filter(params) do
Post
|> order_by(^filter_order_by(params["order_by"]))
|> where(^filter_where(params))
|> where(^filter_published_at(params["published_at"]))
end
def filter_order_by("published_at_desc"), do: [desc: :published_at]
def filter_order_by("published_at"), do: [asc: :published_at]
def filter_order_by(_), do: []
def filter_where(params) do
for key <- [:author, :category],
value = params[Atom.to_string(key)],
do: {key, value}
end
def filter_published_at(date) when is_binary(date),
do: dynamic([p], p.published_at > ^date)
def filter_published_at(_date),
do: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment