Flop.Schema protocol (Flop v0.8.1) View Source

This protocol allows you to define the sortable and filterable fields in your Ecto schemas.

Usage

Derive Flop.Schema in your Ecto schema.

defmodule Flop.Pet do
  use Ecto.Schema

  @derive {Flop.Schema,
           filterable: [:name, :species],
           sortable: [:name, :age],
           max_limit: 20}

  schema "pets" do
    field :name, :string
    field :age, :integer
    field :species, :string
  end
end

After that, you can pass the module as the :for option to Flop.validate/2.

iex> Flop.validate(%Flop{order_by: [:name]}, for: Flop.Pet)
{:ok,
 %Flop{
   filters: [],
   limit: nil,
   offset: nil,
   order_by: [:name],
   order_directions: nil,
   page: nil,
   page_size: nil
 }}

iex> {:error, changeset} = Flop.validate(
...>   %Flop{order_by: [:species]}, for: Flop.Pet
...> )
iex> changeset.valid?
false
iex> changeset.errors
[
  order_by: {"has an invalid entry",
   [validation: :subset, enum: [:name, :age]]}
]

Defining default and maximum limits

To define a default or maximum limit, you can set the default_limit and max_limit option when deriving Flop.Schema. The maximum limit will be validated and the default limit applied by Flop.validate/1.

@derive {Flop.Schema,
          filterable: [:name, :species],
          sortable: [:name, :age],
          max_limit: 100,
          default_limit: 50}

Defining a default sort order

To define a default sort order, you can set the default_order_by and default_order_directions options when deriving Flop.Schema. The default values are applied by Flop.validate/1. If no order directions are set, :asc is assumed for all fields.

@derive {Flop.Schema,
          default_order_by: [:name, :age],
          default_order_directions: [:asc, :desc]}

Link to this section Summary

Functions

Returns the default limit of a schema.

Returns the default order of a schema.

Returns the filterable fields of a schema.

Returns the maximum limit of a schema.

Returns the sortable fields of a schema.

Link to this section Types

Link to this section Functions

Link to this function

default_limit(data)

View Source (since 0.3.0)

Specs

default_limit(any()) :: pos_integer() | nil

Returns the default limit of a schema.

iex> Flop.Schema.default_limit(%Flop.Fruit{})
50
Link to this function

default_order(data)

View Source (since 0.7.0)

Specs

default_order(any()) :: %{
  order_by: [atom()],
  order_directions: [Flop.order_direction()]
}

Returns the default order of a schema.

iex> Flop.Schema.default_order(%Flop.Fruit{})
%{order_by: [:name], order_directions: [:asc]}

Specs

filterable(any()) :: [atom()]

Returns the filterable fields of a schema.

iex> Flop.Schema.filterable(%Flop.Pet{})
[:name, :species]
Link to this function

max_limit(data)

View Source (since 0.2.0)

Specs

max_limit(any()) :: pos_integer() | nil

Returns the maximum limit of a schema.

iex> Flop.Schema.max_limit(%Flop.Pet{})
20

Specs

sortable(any()) :: [atom()]

Returns the sortable fields of a schema.

iex> Flop.Schema.sortable(%Flop.Pet{})
[:name, :age]