Rumamge.Ecto v1.0.0 Rummage.Ecto.Hooks.Paginate

Rummage.Ecto.Hooks.Paginate is the default pagination hook that comes shipped with Rummage.Ecto.

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

In the Ecto module:

defmodule SomeModule do
  use Ecto.Schema
  use Rummage.Ecto, paginate_hook: CustomHook
end

OR

Globally for all models in config.exs (NOT Recommended):

config :rummage_ecto,
  Rummage.Ecto,
  default_paginate: 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

Summary

Functions

Builds a paginate queryable on top of the given queryable from the rummage parameters from the given rummage struct

Functions

run(queryable, rummage)
run(Ecto.Query.t, map) :: {Ecto.Query.t, map}

Builds a paginate 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 “paginate”, it simply returns the queryable itself:

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

When the queryable passed is not just a struct:

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

When rummage struct passed has the key "paginate", but with a value of %{}, "" or [] it simply returns the queryable itself:

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

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

iex> alias Rummage.Ecto.Hooks.Paginate
iex> import Ecto.Query
iex> Paginate.run(Parent, %{"paginate" => []})
Parent

When rummage struct passed has the key “paginate”, with “per_page” and “page” keys it returns a paginated version of the queryable passed in as the argument:

iex> alias Rummage.Ecto.Hooks.Paginate
iex> import Ecto.Query
iex> rummage = %{"paginate" => %{"per_page" => "1", "page" => "1"}}
%{"paginate" => %{"page" => "1", "per_page" => "1"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Paginate.run(queryable, rummage)
#Ecto.Query<from p in "parents", limit: ^1, offset: ^0>

iex> alias Rummage.Ecto.Hooks.Paginate
iex> import Ecto.Query
iex> rummage = %{"paginate" => %{"per_page" => "5", "page" => "2"}}
%{"paginate" => %{"page" => "2", "per_page" => "5"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Paginate.run(queryable, rummage)
#Ecto.Query<from p in "parents", limit: ^5, offset: ^5>

When no "page" key is passed, it defaults to 1:

iex> alias Rummage.Ecto.Hooks.Paginate
iex> import Ecto.Query
iex> rummage = %{"paginate" => %{"per_page" => "10"}}
%{"paginate" => %{"per_page" => "10"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> Paginate.run(queryable, rummage)
#Ecto.Query<from p in "parents", limit: ^10, offset: ^0>