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

Link to this function

build_slug(sources, changeset)

View Source
@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.

Link to this function

get_sources(changeset, opts)

View Source
@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:

  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
@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:

  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
@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.