Rumamge.Ecto v1.2.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:

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

OR

Globally for all models in config.exs:

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, Rummage.Ecto.CustomHooks.SimplePaginate

Summary

Functions

Implementation of before_hook for Rummage.Ecto.Hooks.Paginate. This function takes a queryable, rummage struct and an opts map. Using those it calculates the total_count and max_page for the paginate hook

Builds a paginate 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.Paginate. This function takes a queryable, rummage struct and an opts map. Using those it calculates the total_count and max_page for the paginate hook.

Examples

iex> alias Rummage.Ecto.Hooks.Paginate
iex> alias Rummage.Ecto.Category
iex> Paginate.before_hook(Category, %{}, %{})
%{}

iex> alias Rummage.Ecto.Hooks.Paginate
iex> alias Rummage.Ecto.Category
iex> Ecto.Adapters.SQL.Sandbox.checkout(Rummage.Ecto.Repo)
iex> Rummage.Ecto.Repo.insert(%Category{category_name: "Category 1"})
iex> Rummage.Ecto.Repo.insert(%Category{category_name: "Category 2"})
iex> Rummage.Ecto.Repo.insert(%Category{category_name: "Category 3"})
iex> rummage = %{"paginate" => %{"per_page" => "1", "page" => "1"}}
iex> Paginate.before_hook(Category, rummage, %{})
%{"paginate" => %{"max_page" => "3", "page" => "1", "per_page" => "1", "total_count" => "3"}}
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>