Skip to content

Instantly share code, notes, and snippets.

@sobolevn
Created July 19, 2017 14:07
Show Gist options
  • Save sobolevn/4a68cf56b27f279c452f5f9937083280 to your computer and use it in GitHub Desktop.
Save sobolevn/4a68cf56b27f279c452f5f9937083280 to your computer and use it in GitHub Desktop.
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
@doc false
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment