Automatically create slugs for Ecto schemas.
Branch: master
Clone or download
dependabot and sobolevn Bump ecto from 3.0.6 to 3.0.7 (#68)
Bumps [ecto](https://github.com/elixir-ecto/ecto) from 3.0.6 to 3.0.7.
- [Release notes](https://github.com/elixir-ecto/ecto/releases)
- [Changelog](https://github.com/elixir-ecto/ecto/blob/master/CHANGELOG.md)
- [Commits](elixir-ecto/ecto@v3.0.6...v3.0.7)

Signed-off-by: dependabot[bot] <support@dependabot.com>
Latest commit bb011ae Feb 7, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Version 0.5.1 release May 21, 2018
test Add Ecto 3 support (#61) Dec 27, 2018
.credo.exs Updates dependencies Jun 11, 2017
.editorconfig Adds Credo support Feb 1, 2017
.gitignore Add support for numeric source fields (#18) Nov 21, 2017
.travis.yml Update Travis to also test Elixir 1.6, 1.7 and OTP 21.0 (#60) Dec 27, 2018
CHANGELOG.md Version 1.0.0 release Dec 28, 2018
CONTRIBUTING.md Version 0.3.1 release Jul 19, 2017
LICENSE.md
README.md Version 1.0.0 release Dec 28, 2018
mix.exs Version 1.0.0 release Dec 28, 2018
mix.lock Bump ecto from 3.0.6 to 3.0.7 (#68) Feb 7, 2019

README.md

EctoAutoslugField

Build Status Coverage Status Hex Version License

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

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

Installation

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

Options

There are several options to configure.

Required:

  • :to - represents the slug field name where to save value to

Optional:

  • :from - represents the source fields from which to build slug, if this option is not set you have to override get_sources/2 function
  • :always_change - if this option is set slug will be recreated from the given sources each time maybe_generate_slug function is called

Functions

  • get_sources/2 - this function is used to get sources for the slug, docs.
  • build_slug/2 - this function is a place to modify the result slug, docs.

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(%Article{} = article, attrs) do
    article
    |> cast(attrs, [: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.

License

MIT. Please see LICENSE.md for licensing details.