Rummage.Ecto v2.0.0 Rummage.Ecto.Hook behaviour View Source
This module defines a behaviour that Rummage.Ecto.Hooks have to follow.
This module also defines a __using__ macro which mandates certain
behaviours for a Hook module to follow.
Native hooks that come with Rummage.Ecto follow this behaviour.
Custom Search, Sort and Paginate hooks should follow this behaviour
as well, in order for them to work well with Rummage.Ecto
Usage
- This is the preferred way of creating a Custom Hook. Using
Rummage.Ecto.Hook.__using__/1macro, it can be ensured thatrun/2andformat_params/2functions have been implemented.
defmodule MyCustomHook do
use Rummage.Ecto.Hook
def run(queryable, params), do: queryable
def format_params(querable, params, opts), do: params
end- A Custom Hook can also be created by using
Rummage.Ecto.Hook@behviour
defmodule MyCustomHook do
@behviour Rummage.Ecto.Hook
def run(queryable, params), do: queryable
def format_params(querable, params, opts), do: params
end Link to this section Summary
Functions
This macro allows us to write rummage hooks in an easier way. This includes
a @behaviour module attribute and defines raisable callback implementations
for the hook using this module. It also makes run/2 and format_params/3
overridable and expects them to be defined in the hook.
Callbacks
All callback invoked by Rummage.Ecto which applies a set of translations
to params passed to the hook. This is responsible for making sure that
the params passed to the hook's run/2 function are santized.
All callback invoked by Rummage.Ecto which applies a set of translations
to an ecto query, based on operations defined in the hook.
Link to this section Functions
This macro allows us to write rummage hooks in an easier way. This includes
a @behaviour module attribute and defines raisable callback implementations
for the hook using this module. It also makes run/2 and format_params/3
overridable and expects them to be defined in the hook.
Usage:
defmodule MyHook do
use Rummage.Ecto.Hook
def run(queryable, params), do: "do something"
def format_params(q, params, opts), do: "do something"
endFor a better example, check out Rummage.Ecto.Hook.Paginate or any other
hooks defined in Rummage.Ecto
Link to this section Callbacks
Specs
format_params(Ecto.Query.t(), map(), keyword()) :: map()
All callback invoked by Rummage.Ecto which applies a set of translations
to params passed to the hook. This is responsible for making sure that
the params passed to the hook's run/2 function are santized.
Specs
run(Ecto.Query.t(), map()) :: Ecto.Query.t()
All callback invoked by Rummage.Ecto which applies a set of translations
to an ecto query, based on operations defined in the hook.