View Source EctoAutoslugField.SlugBase (ecto_autoslug_field v3.1.0)
This module defines all functions Slug
module uses.
Summary
Functions
This function is used to build the slug itself.
This function is used to get sources for the slug.
This function is used to generate slug.
This is just a helper function to check for uniqueness.
Functions
@spec build_slug(Keyword.t(), Ecto.Changeset.t() | nil) :: String.t() | nil
This function is used to build the slug itself.
This function is a place to modify the result slug.
For convenience you can call super(sources, changeset)
which will return the slug binary.
super(sources)
uses slugify
,
but you can completely change slug-engine to your own.
Note: this function will only be called if sources
is not empty.
Also important this function will be called only
once for the normal workflow. And every time for :always_change
.
So you can do some heavy computations.
If for some reason slug should not be set -
just return nil
or empty binary
.
It should return a binary
or nil
.
@spec get_sources(Ecto.Changeset.t(), Keyword.t()) :: [atom() | binary()]
This function is used to get sources for the slug.
There can be many use cases when this behavior is required, here are the brief examples:
- Conditional slug sources
- Add any data from different sources
This function should return list
of atoms or binaries, or nil
.
When processing the returned list:
atom
-key is supposed to identify the model fieldbinary
-key is treated as a data itself, it won't be changed
@spec maybe_generate_slug(Ecto.Changeset.t(), atom() | list() | none(), Keyword.t()) :: Ecto.Changeset.t()
This function is used to generate slug.
It is called 'maybe' since it may not generate slug for several reasons:
- It was already created and
:always_change
option was not set - The source fields for the slug were empty
This function takes changeset as an input and returns changeset.
@spec unique_constraint(Ecto.Changeset.t(), atom(), Keyword.t()) :: Ecto.Changeset.t()
This is just a helper function to check for uniqueness.
It basically just wraps Ecto.Changeset
and set a proper field name.