EctoAutoslugField.SlugBase (ecto_autoslug_field v3.0.0) View Source

This module defines all functions Slug module uses.

Link to this section 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.

Link to this section Functions

Link to this function

build_slug(sources, changeset)

View Source

Specs

build_slug(Keyword.t(), Ecto.Changeset.t() | nil) :: String.t()

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 Slugger, 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.

Link to this function

get_sources(changeset, opts)

View Source

Specs

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:

  1. Conditional slug sources
  2. Add any data from different sources

This function should return list of atoms or binaries, or nil.

When processing the returned list:

  1. atom-key is supposed to identify the model field
  2. binary-key is treated as a data itself, it won't be changed
Link to this function

maybe_generate_slug(changeset, sources, opts)

View Source

Specs

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:

  1. It was already created and :always_change option was not set
  2. The source fields for the slug were empty

This function takes changeset as an input and returns changeset.

Link to this function

unique_constraint(changeset, to, opts \\ [])

View Source

Specs

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.