Home

Awesome

EctoAutoslugField

Build Status Coverage Status Module Version Hex Docs License

ecto_autoslug_field is a reusable Ecto library which can automatically create slugs from other fields. We use slugify as a default slug-engine.

We only depend on the ecto package (we do not deal with ecto_sql at all). We support ecto >= 3.7 and ecto < 4!

See this blog post for more information.

Installation

def deps do
  [
    {:ecto_autoslug_field, "~> 3.1"}
  ]
end

Options

There are several options to configure.

Required:

Optional:

Functions

Examples

The simplest example:

defmodule EctoSlugs.Blog.Article.TitleSlug do
  use EctoAutoslugField.Slug, from: :title, to: :slug
end

defmodule EctoSlugs.Blog.Article do
  use Ecto.Schema
  import Ecto.Changeset
  alias EctoSlugs.Blog.Article
  alias EctoSlugs.Blog.Article.TitleSlug

  schema "blog_articles" do
    field :breaking, :boolean, default: false
    field :content, :string
    field :title, :string

    field :slug, TitleSlug.Type

    timestamps()
  end

  def changeset(model, params \\ :invalid) do
    model
    |> cast(params, [:title, :content, :breaking])
    |> validate_required([:title, :content])
    |> unique_constraint(:title)
    |> TitleSlug.maybe_generate_slug()
    |> TitleSlug.unique_constraint()
  end
end

See this tutorial for some more examples.

Changelog

See CHANGELOG.md.

Copyright and License

Copyright (c) 2016 Nikita Sobolev

This library is released under the MIT License. See the LICENSE.md file for further details.