Ecto Filters v0.2.2 Ecto.Filters View Source

Adds a macro add_filter and private function apply_filter/2 to transform request params into ecto query expressions.

Example

use Ecto.Filters

add_filter(:comment_body, fn value, query ->
  query
  |> join(:left, [p], c in assoc(p, :comments), as: :comments)
  |> where([comments: comments], ilike(comments.body, ^value))
end)

apply_filters(Post, %{"q" => %{"comment_body" => "some text"}}) |> MyRepo.all()
[%Post{title: "Ecto Filters"}, ...]

Configure the Search Key

By default Ecto.Filters looks for :q as the filter parameter key. This can be set by use Ecto.Filters, key: :search.

Link to this section Summary

Link to this section Functions

Link to this macro

add_filter(key, fun) View Source (macro)