Rumamge.Ecto v0.1.1 Rummage.Ecto.Hooks.Sort

Rummage.Ecto.Hooks.Sort is the default sort hook that comes shipped with Rummage.

This module can be overridden with a custom module while using Rummage.Ecto in Ecto struct module.

Summary

Functions

Builds a sort query on top of the given query from the rummage parameters from the given rummage struct

Functions

run(query, rummage)

Builds a sort query on top of the given query from the rummage parameters from the given rummage struct.

Examples

When rummage struct passed doesn’t have the key “sort”, it simply returns the query itself:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> Sort.run(Parent, %{})
Parent

When the query passed is not just a struct:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> query = from u in "parents"
#Ecto.Query<from p in "parents">
iex>  Sort.run(query, %{})
#Ecto.Query<from p in "parents">

When rummage struct passed has the key “sort”, with “field” and “order” it returns a sorted version of the query passed in as the argument:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> rummage = %{"sort" => ["field_1.asc"]}
%{"sort" => ["field_1.asc"]}
iex> query = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Sort.run(query, rummage)
#Ecto.Query<from p in "parents", order_by: [asc: p.field_1]>