简体   繁体   中英

How do I generalize this Ecto/Elixir function?

I've got the following, which applies a WHERE statement to an ecto query if platform exists.

  defp maybe_platform(queryable, false), do: queryable
  defp maybe_platform(queryable, platform) do
      queryable
      |> where([c], c.platform == ^platform)
  end

How can I generalise this code for any variable (instead of just platform )? (it's the c.platform bit I'm struggling with)

You need to use Ecto field https://hexdocs.pm/ecto/Ecto.Query.API.html#field/2

Your function would look something like this

defp maybe_field(queryable, field_name, field_value) do
    queryable
    |> where([c], field(c, ^field_name) == ^field_value)
end

and then

(from m in MyModel)
|> maybe_field(:platform, "platform_name_or_variable")
|> maybe_field(:another_field, "some value")
|> maybe_field(:is_platform_enabled, true)
|> Repo.all

You can use keywords as filter. Have a look at Ecto.Query#where/3 :

from(c in City, where: c.country == "Sweden")
from(c in City, where: [country: "Sweden"])

Ecto.Query.where/3 accepts keyword list as filters. Elixir keyword list is just a wrapper around Erlang lists of tuples of two elements each. That said, somewhat like that would do:

defp maybe_where(queryable, _param, false), do: queryable
defp maybe_where(queryable, param, value) do
  filters = [{param, value}]
  where(queryable, ^filters)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM