PhoenixBricks.Filter (phoenix_bricks v0.2.0) View Source

Defines a Filter Schema

Examples

defmodule RecordFilter do
  use PhoenixBricks.Filter,
      filters: [
        field_matcher: :string
      ]
end

It defines a schema which fields could be used in a search form.

Search changeset

def index(conn, params) do
  filters = Map.get(params, "filters", %{})

  conn
  |> assign(:changeset, RecordFilter.changeset(%RecordFilter{}, filters))
  |> render("index.html")
end
<%= form_for @conn, Routes.session_path(@conn, :create), [method: :post, as: :user], fn f -> %>
  <div class="form-group">
    <%= label f, :field_matcher %>
    <%= text_input f, :field_matcher %>
  </div>

  <div class="form-group">kab
    <%= submit "Search" %>
  </div>
<% end %>

Convertion from map of params to a list of filtered scopes

iex> params = %{"filters" => %{"field_matcher" => "value"}}
iex> RecordFilter.convert_params_to_scopes(params)
iex> [field_matcher: "value"]