2018-01-13 23:33:03 +01:00
|
|
|
defmodule Eventos.Events.Event.TitleSlug do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Generates a slug for an event title
|
|
|
|
"""
|
2018-01-13 23:33:03 +01:00
|
|
|
alias Eventos.Events.Event
|
|
|
|
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 e in Event,
|
|
|
|
where: e.slug == ^slug
|
|
|
|
|
|
|
|
case Repo.one(query) do
|
|
|
|
nil -> slug
|
|
|
|
_event ->
|
|
|
|
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
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
defmodule Eventos.Events.Event do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents an event
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-01-15 12:04:09 +01:00
|
|
|
alias Eventos.Events.{Event, Participant, Request, Tag, Category, Session, Track}
|
2018-01-13 23:33:03 +01:00
|
|
|
alias Eventos.Events.Event.TitleSlug
|
2017-12-08 09:58:14 +01:00
|
|
|
alias Eventos.Accounts.Account
|
|
|
|
|
|
|
|
schema "events" do
|
2018-01-13 23:33:03 +01:00
|
|
|
field :begins_on, Timex.Ecto.DateTimeWithTimezone
|
2017-12-08 09:58:14 +01:00
|
|
|
field :description, :string
|
2018-01-13 23:33:03 +01:00
|
|
|
field :ends_on, Timex.Ecto.DateTimeWithTimezone
|
2017-12-08 09:58:14 +01:00
|
|
|
field :title, :string
|
2018-01-13 23:33:03 +01:00
|
|
|
field :geom, Geo.Geometry
|
|
|
|
field :slug, TitleSlug.Type
|
|
|
|
field :state, :integer, default: 0
|
2018-01-15 11:50:26 +01:00
|
|
|
field :status, :integer, default: 0
|
2018-01-13 23:33:03 +01:00
|
|
|
field :public, :boolean, default: true
|
|
|
|
field :thumbnail, :string
|
|
|
|
field :large_image, :string
|
|
|
|
field :publish_at, Timex.Ecto.DateTimeWithTimezone
|
|
|
|
belongs_to :organizer, Account, [foreign_key: :organizer_id]
|
|
|
|
has_many :tags, Tag
|
2018-01-15 12:04:09 +01:00
|
|
|
belongs_to :category, Category
|
2018-01-13 23:33:03 +01:00
|
|
|
many_to_many :participants, Account, join_through: Participant
|
|
|
|
has_many :event_request, Request
|
|
|
|
has_many :tracks, Track
|
|
|
|
has_many :sessions, Session
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Event{} = event, attrs) do
|
|
|
|
event
|
2018-01-15 12:04:09 +01:00
|
|
|
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id, :category_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at])
|
|
|
|
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id, :category_id])
|
2018-01-13 23:33:03 +01:00
|
|
|
|> TitleSlug.maybe_generate_slug()
|
|
|
|
|> TitleSlug.unique_constraint()
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|