Spec improvements

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-09-27 09:41:36 +02:00
parent cc3106e425
commit 41f086e2c9
21 changed files with 299 additions and 90 deletions

View File

@@ -6,6 +6,7 @@ defmodule Mobilizon.Users.Setting do
use Ecto.Schema
import Ecto.Changeset
alias Mobilizon.Users.{NotificationPendingNotificationDelay, User}
alias Mobilizon.Users.Setting.Location
@type t :: %__MODULE__{
timezone: String.t(),
@@ -40,8 +41,6 @@ defmodule Mobilizon.Users.Setting do
@attrs @required_attrs ++ @optional_attrs
@location_attrs [:name, :range, :geohash]
@primary_key {:user_id, :id, autogenerate: false}
schema "user_settings" do
field(:timezone, :string)
@@ -60,11 +59,7 @@ defmodule Mobilizon.Users.Setting do
field(:group_notifications, NotificationPendingNotificationDelay, default: :one_day)
field(:last_notification_sent, :utc_datetime)
embeds_one :location, Location, on_replace: :update, primary_key: false do
field(:name, :string)
field(:range, :integer)
field(:geohash, :string)
end
embeds_one(:location, Location, on_replace: :update)
belongs_to(:user, User, primary_key: true, type: :id, foreign_key: :id, define_field: false)
@@ -76,13 +71,7 @@ defmodule Mobilizon.Users.Setting do
def changeset(setting, attrs) do
setting
|> cast(attrs, @attrs)
|> cast_embed(:location, with: &location_changeset/2)
|> cast_embed(:location)
|> validate_required(@required_attrs)
end
@spec location_changeset(location, map) :: Ecto.Changeset.t()
def location_changeset(schema, params) do
schema
|> cast(params, @location_attrs)
end
end