acrolink
How to write this in less possible code?
I am using something like this to filter Ecto.Query results:
def build_query(query, "order_by", order_by, _conn) when order_by != "" do
case order_by do
"desc_inserted_at" -> Ecto.Query.order_by(query, [p], [desc: p.inserted_at])
"asc_inserted_at" -> Ecto.Query.order_by(query, [p], [asc: p.inserted_at])
"desc_title" -> Ecto.Query.order_by(query, [p], [desc: p.title])
"asc_title" -> Ecto.Query.order_by(query, [p], [asc: p.title])
_ -> query
end
end
I will be passing to this method order_by variables with values like name, id, inserted_at, etc preceded by either desc or asc, e.g. desc_inserted_at.
My question, how to make it in less repetitive code, especially when it comes to specifying the direction desc or asc ? Thank you.
Marked As Solved
kokolegorille
It would be easier to have a dir and order…
eg.
[h, t] = order_by |> String.split("_")
dir = h |> String.to_atom
order = t |> Enum.join("_") |> String.to_atom
Ecto.Query.order_by(query, [p], Keyword.put([], dir, Keyword.get(p, order)))
That does not check input validity, but You might see what I mean. It is not tested…
BTW You can use Regex like this to get metadata
iex> Regex.named_captures ~r/(?<dir>asc|desc)_(?<order>.*)/, "desc_inserted_at"
%{"dir" => "desc", "order" => "inserted_at"}
UPDATE: I have should have written [h | t], thanks @blatyo for spotting typo
Also Liked
blatyo
I tend to use pattern matching in function heads. Here’s how I might approach it.
@order_by_fields ["inserted_at", "title"]
def build_query(query, "order_by", "", _conn), do: query
def build_query(query, "order_by", "desc_" <> field, _conn) when field in @order_by_fields do
Ecto.Query.order_by(query, [p], [desc: String.to_atom(field)])
end
def build_query(query, "order_by", "asc_" <> field, _conn) when field in @order_by_fields do
Ecto.Query.order_by(query, [p], [asc: String.to_atom(field)])
end
jordiee
I may be wrong here but is it not slightly dangerous to use String.to_atom on what I suspect is user passed strings. This opens up an attack vector for memory issues because atoms are not garbage collected. Again correct me if I am wrong.
blatyo
Looks like you meant [h | t] = order_by |> String.split("_")
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








