Rumamge.Ecto v1.2.0 Rummage.Ecto.Hooks.Sort

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

Usage: For a regular sort:

This returns a queryable which upon running will give a list of Parent(s) sorted by ascending field_1

alias Rummage.Ecto.Hooks.Sort

sorted_queryable = Sort.run(Parent, %{"sort" => %{"assoc" => [], "field" => "field_1.asc"}})

For a case-insensitive sort:

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

alias Rummage.Ecto.Hooks.Sort

sorted_queryable = Sort.run(Parent, %{"sort" => %{"assoc" => [], "field" => "field_1.asc.ci"}})

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

In the Ecto module:

Rummage.Ecto.rummage(queryable, rummage, sort: CustomHook)

OR

Globally for all models in config.exs:

config :rummage_ecto,
  Rummage.Ecto,
  default_sort: CustomHook

The CustomHook must implement behaviour Rummage.Ecto.Hook. For examples of CustomHook, check out some custom_hooks that are shipped with elixir: Rummage.Ecto.CustomHooks.SimpleSearch, Rummage.Ecto.CustomHooks.SimpleSort, Rummage.Ecto.CustomHooks.SimplePaginate

Summary

Functions

Implementation of before_hook for Rummage.Ecto.Hooks.Sort. 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

before_hook(queryable, rummage, opts)
before_hook(Ecto.Query.t, map, map) :: map

Implementation of before_hook for Rummage.Ecto.Hooks.Sort. 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.Hooks.Sort
iex> Sort.before_hook(Parent, %{}, %{})
%{}
run(queryable, rummage)
run(Ecto.Query.t, map) :: {Ecto.Query.t, map}

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.Hooks.Sort
iex> import Ecto.Query
iex> Sort.run(Parent, %{})
Parent

When the queryable passed is not just a struct:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex>  Sort.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.Hooks.Sort
iex> import Ecto.Query
iex> Sort.run(Parent, %{"sort" => {}})
Parent

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

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

When rummage struct passed has the key "sort", but empty associations array it just orders it by the passed queryable:

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

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

When no order is specified, it returns the queryable itself:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> rummage = %{"sort" => %{"assoc" => [], "field" => "field_1"}}
%{"sort" => %{"assoc" => [],
  "field" => "field_1"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Sort.run(queryable, rummage)
#Ecto.Query<from p in subquery(from p in "parents")>

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.Hooks.Sort
iex> import Ecto.Query
iex> rummage = %{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1.asc"}}
%{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1.asc"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Sort.run(queryable, rummage)
#Ecto.Query<from p0 in subquery(from p in "parents"), join: p1 in assoc(p0, :parent), join: p2 in assoc(p1, :parent), order_by: [asc: p2.field_1]>


iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> rummage = %{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1.desc"}}
%{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1.desc"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Sort.run(queryable, rummage)
#Ecto.Query<from p0 in subquery(from p in "parents"), join: p1 in assoc(p0, :parent), join: p2 in assoc(p1, :parent), order_by: [desc: p2.field_1]>

When no order is specified even with the associations, it returns the queryable itself:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> rummage = %{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1"}}
%{"sort" => %{"assoc" => ["parent", "parent"],
  "field" => "field_1"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Sort.run(queryable, rummage)
#Ecto.Query<from p0 in subquery(from p in "parents"), join: p1 in assoc(p0, :parent), join: p2 in assoc(p1, :parent)>

When rummage struct passed has case-insensitive sort, it returns a sorted version of the queryable with case_insensitive arguments:

iex> alias Rummage.Ecto.Hooks.Sort
iex> import Ecto.Query
iex> rummage = %{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1.asc.ci"}}
%{"sort" => %{"assoc" => ["parent", "parent"], "field" => "field_1.asc.ci"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Sort.run(queryable, rummage)
#Ecto.Query<from p0 in subquery(from p in "parents"), join: p1 in assoc(p0, :parent), join: p2 in assoc(p1, :parent), order_by: [asc: fragment("lower(?)", p2.field_1)]>