ecto_autoslug_field v0.1.2 EctoAutoslugField.Slug

This module defines all the required functions and modules to work with.

Examples

To create a simple ‘Slug’ field do:

defmodule MyCustomSlug do
  use EctoAutoslugField.Slug, from: :name_field, to: :slug_field
end

It is also possible to override get_sources/2 and build_slug/1 functions which are part of the AutoslugField’s API.

More complex example with the optional sources and custom slug generation function:

defmodule MyComplexSlug do
  use EctoAutoslugField.Slug, to: :slug_field

  def get_sources(changeset, _opts) do
    basic_fields = [:name, :surname]

    if is_company_info_set(changeset) do
      # We want to track changes in the person's company:
      basic_fields ++ [:company, :position]
    else
      basic_fields
    end
  end

  def build_slug(sources) do
    super(sources)  # Calls the `SlugGenerator.build_slug/1`
    |> String.replace("-", "+")
  end
end

It is also possible to always change your slug, even if it was already set:

defmodule ThisSlugShouldChange do
  use EctoAutoslugField.Slug, from: :some_field,
    to: :slug_field, always_change: true
end

Be careful with this option, since cool URIs do not change.