mobilizon.chapril.org-mobil.../lib/eventos/events/event.ex
Thomas Citharel 9df9a795c7 Add state and fix casting
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-01-15 11:50:26 +01:00

71 lines
2.0 KiB
Elixir

defmodule Eventos.Events.Event.TitleSlug do
@moduledoc """
Generates a slug for an event title
"""
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
|> Eventos.Slug.increment_slug()
|> build_unique_slug(changeset)
end
end
end
defmodule Eventos.Events.Event do
@moduledoc """
Represents an event
"""
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.{Event, Participant, Request, Tag, Session, Track}
alias Eventos.Events.Event.TitleSlug
alias Eventos.Accounts.Account
schema "events" do
field :begins_on, Timex.Ecto.DateTimeWithTimezone
field :description, :string
field :ends_on, Timex.Ecto.DateTimeWithTimezone
field :title, :string
field :geom, Geo.Geometry
field :slug, TitleSlug.Type
field :state, :integer, default: 0
field :status, :integer, default: 0
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
many_to_many :participants, Account, join_through: Participant
has_many :event_request, Request
has_many :tracks, Track
has_many :sessions, Session
timestamps()
end
@doc false
def changeset(%Event{} = event, attrs) do
event
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at])
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id])
|> TitleSlug.maybe_generate_slug()
|> TitleSlug.unique_constraint()
end
end