2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events.Tag do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2019-09-13 01:01:17 +02:00
|
|
|
Represents a tag for events.
|
2018-01-14 17:56:50 +01:00
|
|
|
"""
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
import Ecto.Changeset
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events.Tag.TitleSlug
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Events.TagRelation
|
2019-09-13 01:01:17 +02:00
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
title: String.t(),
|
|
|
|
slug: TitleSlug.Type.t(),
|
2019-09-13 01:55:45 +02:00
|
|
|
related_tags: [t]
|
2019-09-13 01:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:title, :slug]
|
|
|
|
@attrs @required_attrs
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
schema "tags" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:title, :string)
|
|
|
|
field(:slug, TitleSlug.Type)
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2019-09-13 01:55:45 +02:00
|
|
|
many_to_many(:related_tags, __MODULE__, join_through: TagRelation)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2019-09-13 01:01:17 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def changeset(%__MODULE__{} = tag, attrs) do
|
2017-12-08 09:58:14 +01:00
|
|
|
tag
|
2019-09-13 01:01:17 +02:00
|
|
|
|> cast(attrs, @attrs)
|
2018-01-13 23:33:03 +01:00
|
|
|
|> TitleSlug.maybe_generate_slug()
|
2019-09-13 01:01:17 +02:00
|
|
|
|> validate_required(@required_attrs)
|
2018-01-13 23:33:03 +01:00
|
|
|
|> TitleSlug.unique_constraint()
|
2019-10-18 11:21:31 +02:00
|
|
|
|> validate_length(:title, min: 2, max: 20)
|
|
|
|
|> validate_length(:slug, min: 2, max: 20)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|