View Source Torch.Helpers (Torch v4.1.0)
Provides helper functions for Torch-generated controllers.
Link to this section Summary
Functions
Paginates a given Ecto.Queryable
using Scrivener.
Determines how the query for an index action should be sorted.
Removes any "un-set" boolean parameters from the filter params list.
Link to this section Types
Specs
params() :: map()
Link to this section Functions
Specs
paginate(Ecto.Queryable.t(), Ecto.Repo.t(), params(), Keyword.t()) :: Scrivener.Page.t()
Paginates a given Ecto.Queryable
using Scrivener.
This is a very thin wrapper around Scrivener.paginate/2
, so see the Scrivener
Ecto documentation for more details.
Parameters
query
: AnEcto.Queryable
to paginate.repo
: Your Repo module.params
: Parameters from yourconn
. For example%{"page" => 1}
.settings
: A list of settings for Scrivener, including:page_size
.
Examples
paginate(query, Repo, params, [page_size: 15])
# => %Scrivener.Page{...}
Specs
Determines how the query for an index action should be sorted.
Relies on the "sort_field"
and "sort_direction"
parameters to be passed.
By default, it sorts by :id
in ascending order.
Examples
iex> sort(%{"sort_field" => "name", "sort_direction" => "desc"})
{:desc, :name}
iex> sort(%{})
{:asc, :id}
In a query pipeline, use in conjunction with Ecto.Query.order_by/3
:
order_by(query, ^sort(params))
Specs
Removes any "un-set" boolean parameters from the filter params list.
Due to the nature of boolean params (on/off) it becomes hard to include the "filter on true" and "filter on false" states while also including a third option of "don't filter at all" on this boolean argument. Since the parameter is always sent in the filter form (due to the checkbox).
We need a way to encode 3 states for a boolean field (on, off, any|ignore).
This function takes a list of boolean field names, and will remove from the params argument, any matching boolean fields whose current value is set to "any" (which is the default placeholder Torch UI uses to signify this third boolean state).
Examples
iex> strip_unset_booleans(%{}, "post", [])
%{}
iex> strip_unset_booleans(%{"post" => %{"title_contains" => "foo"}}, "post", [])
%{"post" => %{"title_contains" => "foo"}}
iex> strip_unset_booleans(%{"post" => %{"title_equals" => "true"}}, "post", [:title])
%{"post" => %{"title_equals" => "true"}}
iex> strip_unset_booleans(%{"post" => %{"title_equals" => "any"}}, "post", [:title])
%{"post" => %{}}
iex> strip_unset_booleans(%{"post" => %{"name_contains" => "foo", "title_equals" => "any"}}, "post", [:title])
%{"post" => %{"name_contains" => "foo"}}