2018-01-13 23:33:03 +01:00
|
|
|
defmodule Eventos.Groups.Group.TitleSlug do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Slug generation for groups
|
|
|
|
"""
|
2018-01-13 23:33:03 +01:00
|
|
|
alias Eventos.Groups.Group
|
|
|
|
import Ecto.Query
|
|
|
|
alias Eventos.Repo
|
|
|
|
use EctoAutoslugField.Slug, from: :title, to: :slug
|
|
|
|
|
|
|
|
def build_slug(sources, changeset) do
|
|
|
|
slug = super(sources, changeset)
|
|
|
|
build_unique_slug(slug, changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp build_unique_slug(slug, changeset) do
|
|
|
|
query = from g in Group,
|
|
|
|
where: g.slug == ^slug
|
|
|
|
|
|
|
|
case Repo.one(query) do
|
|
|
|
nil -> slug
|
|
|
|
_story ->
|
|
|
|
slug
|
2018-01-14 17:56:50 +01:00
|
|
|
|> Eventos.Slug.increment_slug()
|
2018-01-13 23:33:03 +01:00
|
|
|
|> build_unique_slug(changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Eventos.Groups.Group do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents a group
|
|
|
|
"""
|
2018-01-13 23:33:03 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
alias Eventos.Groups.{Group, Member, Request}
|
|
|
|
alias Eventos.Accounts.Account
|
|
|
|
alias Eventos.Groups.Group.TitleSlug
|
|
|
|
|
|
|
|
schema "groups" do
|
|
|
|
field :description, :string
|
|
|
|
field :suspended, :boolean, default: false
|
|
|
|
field :title, :string
|
|
|
|
field :slug, TitleSlug.Type
|
|
|
|
field :uri, :string
|
|
|
|
field :url, :string
|
|
|
|
many_to_many :accounts, Account, join_through: Member
|
|
|
|
has_many :requests, Request
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Group{} = group, attrs) do
|
|
|
|
group
|
|
|
|
|> cast(attrs, [:title, :description, :suspended, :url, :uri])
|
|
|
|
|> validate_required([:title, :description, :suspended, :url, :uri])
|
|
|
|
|> TitleSlug.maybe_generate_slug()
|
|
|
|
|> TitleSlug.unique_constraint()
|
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|