Rumamge.Ecto v1.2.0 Rummage.Ecto.CustomHooks.SimpleSort
Rummage.Ecto.CustomHooks.SimpleSort
is a custom sort hook that comes shipped
with Rummage.Ecto
.
Usage: For a regular sort:
alias Rummage.Ecto.CustomHooks.SimpleSort
# This returns a queryable which upon running will give a list of `Parent`(s)
# sorted by ascending field_1
sorted_queryable = SimpleSort.run(Parent, %{"sort" => "field_1.asc"})
For a case-insensitive sort:
alias Rummage.Ecto.CustomHooks.SimpleSort
# This returns a queryable which upon running will give a list of `Parent`(s)
# sorted by ascending case insensitive field_1
# Keep in mind that case insensitive can only be called for text fields
sorted_queryable = SimpleSort.run(Parent, %{"sort" => "field_1.asc.ci"})
This module can be used by overriding the default sort module. This can be done in the following ways:
In the Ecto
module:
Rummage.Ecto.rummage(queryable, rummage, sort: Rummage.Ecto.CustomHooks.SimpleSort)
OR
Globally for all models in config.exs
(NOT Recommended):
config :rummage_ecto,
Rummage.Ecto,
default_sort: Rummage.Ecto.CustomHooks.SimpleSort
Summary
Functions
Implementation of before_hook
for Rummage.Ecto.CustomHooks.SimpleSort
. This just returns back rummage
at this point.
It doesn’t matter what queryable
or opts
are, it just returns back rummage
Builds a sort queryable
on top of the given queryable
from the rummage parameters
from the given rummage
struct
Functions
Implementation of before_hook
for Rummage.Ecto.CustomHooks.SimpleSort
. This just returns back rummage
at this point.
It doesn’t matter what queryable
or opts
are, it just returns back rummage
.
Examples
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> SimpleSort.before_hook(Parent, %{}, %{})
%{}
Builds a sort queryable
on top of the given queryable
from the rummage parameters
from the given rummage
struct.
Examples
When rummage struct
passed doesn’t have the key "sort"
, it simply returns the
queryable
itself:
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> SimpleSort.run(Parent, %{})
Parent
When the queryable
passed is not just a struct
:
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSort.run(queryable, %{})
#Ecto.Query<from p in "parents">
When rummage struct
passed has the key "sort"
, but with a value of {}
, ""
or []
it simply returns the queryable
itself:
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> SimpleSort.run(Parent, %{"sort" => {}})
Parent
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> SimpleSort.run(Parent, %{"sort" => ""})
Parent
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> SimpleSort.run(Parent, %{"sort" => []})
Parent
When rummage struct
passed has the key "sort"
, with field
and order
it returns a sorted version of the queryable
passed in as the argument:
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> rummage = %{"sort" => "field_1.asc"}
%{"sort" => "field_1.asc"}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSort.run(queryable, rummage)
#Ecto.Query<from p in "parents", order_by: [asc: p.field_1]>
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> rummage = %{"sort" => "field_1.desc"}
%{"sort" => "field_1.desc"}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSort.run(queryable, rummage)
#Ecto.Query<from p in "parents", order_by: [desc: p.field_1]>
When no order
is specified, it returns the queryable
itself:
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> rummage = %{"sort" => "field_1"}
%{"sort" => "field_1"}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSort.run(queryable, rummage)
#Ecto.Query<from p in "parents", order_by: []>
When rummage struct
passed has case-insensitive
sort, it returns
a sorted version of the queryable
with case_insensitive
arguments:
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> rummage = %{"sort" => "field_1.asc.ci"}
%{"sort" => "field_1.asc.ci"}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSort.run(queryable, rummage)
#Ecto.Query<from p in "parents", order_by: [asc: fragment("lower(?)", ^:field_1)]>
iex> alias Rummage.Ecto.CustomHooks.SimpleSort
iex> import Ecto.Query
iex> rummage = %{"sort" => "field_1.desc.ci"}
%{"sort" => "field_1.desc.ci"}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSort.run(queryable, rummage)
#Ecto.Query<from p in "parents", order_by: [desc: fragment("lower(?)", ^:field_1)]>