Rumamge.Ecto v1.2.0 Rummage.Ecto.CustomHooks.KeysetPaginate
Rummage.Ecto.CustomHooks.KeysetPaginate
is a custom paginate hook that comes shipped
with Rummage.Ecto
.
This module can be used by overriding the default paginate module. This can be done in the following ways:
In the Rummage.Ecto
call:
Rummage.Ecto.rummage(queryable, rummage, paginate: Rummage.Ecto.CustomHooks.KeysetPaginate)
OR
Globally for all models in config.exs
:
config :rummage_ecto,
Rummage.Ecto,
default_paginate: Rummage.Ecto.CustomHooks.KeysetPaginate
Summary
Functions
Implementation of before_hook
for Rummage.Ecto.CustomHooks.KeysetPaginate
. 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
Implementation of before_hook
for Rummage.Ecto.CustomHooks.KeysetPaginate
. 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.CustomHooks.KeysetPaginate
iex> alias Rummage.Ecto.Category
iex> KeysetPaginate.before_hook(Category, %{}, %{})
%{}
iex> alias Rummage.Ecto.CustomHooks.KeysetPaginate
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> KeysetPaginate.before_hook(Category, rummage, %{})
%{"paginate" => %{"max_page" => "3", "page" => "1", "per_page" => "1", "total_count" => "3"}}
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.CustomHooks.KeysetPaginate
iex> import Ecto.Query
iex> KeysetPaginate.run(Parent, %{})
Parent
When the queryable passed is not just a struct:
iex> alias Rummage.Ecto.CustomHooks.KeysetPaginate
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> KeysetPaginate.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.CustomHooks.KeysetPaginate
iex> import Ecto.Query
iex> KeysetPaginate.run(Parent, %{"paginate" => %{}})
Parent
iex> alias Rummage.Ecto.CustomHooks.KeysetPaginate
iex> import Ecto.Query
iex> KeysetPaginate.run(Parent, %{"paginate" => ""})
Parent
iex> alias Rummage.Ecto.CustomHooks.KeysetPaginate
iex> import Ecto.Query
iex> KeysetPaginate.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.CustomHooks.KeysetPaginate
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> KeysetPaginate.run(queryable, rummage)
#Ecto.Query<from p in "parents", limit: ^1, offset: ^0>
iex> alias Rummage.Ecto.CustomHooks.KeysetPaginate
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> KeysetPaginate.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.CustomHooks.KeysetPaginate
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> KeysetPaginate.run(queryable, rummage)
#Ecto.Query<from p in "parents", limit: ^10, offset: ^0>