Created
June 5, 2017 09:58
-
-
Save tosbourn/caa2b2864ed8db2b926cf58c78c9ed5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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