EctoFilter.Operators.Association behaviour (EctoFilter v0.3.1) View Source

Association operators.

Examples:

With "one" cardinality

iex> acme_org = Repo.insert!(%Organization{name: "Acme"})
iex> globex_org = Repo.insert!(%Organization{name: "Globex"})
iex> Repo.insert!(%User{first_name: "Bob", organization: acme_org})
iex> Repo.insert!(%User{first_name: "Alice", organization: globex_org})
iex> result =
...>   User
...>   |> EctoFilter.filter([{:organization, nil, [{:name, :like, "acme"}]}])
...>   |> Repo.all()
iex> length(result)
1
iex> hd(result).first_name
"Bob"

With "many" cardinality

iex> bob = Repo.insert!(%User{first_name: "Bob"})
iex> alice = Repo.insert!(%User{first_name: "Alice"})
iex> Repo.insert!(%Post{title: "Post about weather", author: bob})
iex> Repo.insert!(%Post{title: "Post about sports", author: alice})
iex> result =
...>   User
...>   |> EctoFilter.filter([{:posts, nil, [{:title, :like, "sports"}]}])
...>   |> Repo.all()
iex> length(result)
1
iex> hd(result).first_name
"Alice"

With "many" cardinality through another association

iex> weather_post = Repo.insert!(%Post{title: "Post about weather"})
iex> sports_post = Repo.insert!(%Post{title: "Post about sports"})
iex> bob = Repo.insert!(%User{first_name: "Bob"})
iex> alice = Repo.insert!(%User{first_name: "Alice"})
iex> Repo.insert!(%Comment{body: "Lorem ipsum", author: bob, post: weather_post})
iex> Repo.insert!(%Comment{body: "Dolor sit amet", author: alice, post: sports_post})
iex> result =
...>   Post
...>   |> EctoFilter.filter([{:comments_authors, nil, [{:first_name, :like, "bob"}]}])
...>   |> Repo.all()
iex> length(result)
1
iex> hd(result).title
"Post about weather"

Link to this section Summary

Link to this section Types

Specs

condition() :: {field :: atom(), nil, conditions :: [EctoFilter.condition()]}

Link to this section Callbacks

Link to this callback

apply(query, condition, type, context)

View Source

Specs

apply(
  query :: Ecto.Query.t(),
  condition :: condition(),
  type :: Ecto.Association.t(),
  context :: Ecto.Queriable.t()
) :: Ecto.Query.t()