2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Addresses.Address do
|
2018-01-17 11:39:01 +01:00
|
|
|
@moduledoc "An address for an event or a group"
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
|
|
|
alias Mobilizon.Events.Event
|
2018-11-12 23:30:47 +01:00
|
|
|
# alias Mobilizon.Actors.Actor
|
2019-07-30 10:35:29 +02:00
|
|
|
@attrs [
|
|
|
|
:description,
|
|
|
|
:floor,
|
|
|
|
:geom,
|
|
|
|
:country,
|
|
|
|
:locality,
|
|
|
|
:region,
|
|
|
|
:postal_code,
|
|
|
|
:street,
|
|
|
|
:url
|
|
|
|
]
|
|
|
|
@required [
|
|
|
|
:url
|
|
|
|
]
|
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)
|
|
|
|
has_many(:event, Event, foreign_key: :physical_address_id)
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Address{} = address, attrs) do
|
|
|
|
address
|
2019-07-30 10:35:29 +02:00
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> set_url()
|
|
|
|
|> validate_required(@required)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp set_url(%Ecto.Changeset{changes: changes} = changeset) do
|
|
|
|
url =
|
|
|
|
Map.get(changes, :url, MobilizonWeb.Endpoint.url() <> "/address/#{Ecto.UUID.generate()}")
|
|
|
|
|
|
|
|
put_change(changeset, :url, url)
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|