2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Addresses.Address do
|
2019-09-08 03:05:30 +02:00
|
|
|
@moduledoc """
|
|
|
|
Represents an address for an event or a group.
|
|
|
|
"""
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
use Ecto.Schema
|
2019-09-08 03:05:30 +02:00
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
import Ecto.Changeset
|
2019-09-08 03:05:30 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2019-09-08 03:05:30 +02:00
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
country: String.t(),
|
|
|
|
locality: String.t(),
|
|
|
|
region: String.t(),
|
|
|
|
description: String.t(),
|
|
|
|
floor: String.t(),
|
|
|
|
geom: Geo.PostGIS.Geometry.t(),
|
|
|
|
postal_code: String.t(),
|
|
|
|
street: String.t(),
|
|
|
|
url: String.t(),
|
|
|
|
origin_id: String.t(),
|
|
|
|
events: [Event.t()]
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:url]
|
|
|
|
@optional_attrs [
|
2019-07-30 10:35:29 +02:00
|
|
|
:description,
|
|
|
|
:floor,
|
|
|
|
:geom,
|
|
|
|
:country,
|
|
|
|
:locality,
|
|
|
|
:region,
|
|
|
|
:postal_code,
|
|
|
|
:street,
|
2019-08-22 15:57:44 +02:00
|
|
|
:origin_id
|
2019-07-30 10:35:29 +02:00
|
|
|
]
|
2019-09-08 03:05:30 +02:00
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
schema "addresses" do
|
2019-03-22 15:51:23 +01:00
|
|
|
field(:country, :string)
|
|
|
|
field(:locality, :string)
|
|
|
|
field(:region, :string)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:description, :string)
|
|
|
|
field(:floor, :string)
|
2018-08-24 12:31:41 +02:00
|
|
|
field(:geom, Geo.PostGIS.Geometry)
|
2019-03-22 15:51:23 +01:00
|
|
|
field(:postal_code, :string)
|
|
|
|
field(:street, :string)
|
2019-07-30 10:35:29 +02:00
|
|
|
field(:url, :string)
|
2019-08-22 15:57:44 +02:00
|
|
|
field(:origin_id, :string)
|
2019-09-08 03:05:30 +02:00
|
|
|
|
|
|
|
has_many(:events, Event, foreign_key: :physical_address_id)
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def changeset(%__MODULE__{} = address, attrs) do
|
2018-01-17 11:39:01 +01:00
|
|
|
address
|
2019-07-30 10:35:29 +02:00
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> set_url()
|
2019-09-08 03:05:30 +02:00
|
|
|
|> validate_required(@required_attrs)
|
2019-07-30 10:35:29 +02:00
|
|
|
end
|
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec set_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
2019-07-30 10:35:29 +02:00
|
|
|
defp set_url(%Ecto.Changeset{changes: changes} = changeset) do
|
2019-09-21 23:59:07 +02:00
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
url = Map.get(changes, :url, "#{MobilizonWeb.Endpoint.url()}/address/#{uuid}")
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
put_change(changeset, :url, url)
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|