use atoms for role changeset
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
shibao 2023-01-29 12:56:44 -05:00
parent 40f301ca71
commit 24f608163f
3 changed files with 4 additions and 4 deletions

View File

@ -112,7 +112,7 @@ defmodule Memex.Accounts do
|> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true)) |> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true))
|> Multi.insert(:add_user, fn %{users_count: count} -> |> Multi.insert(:add_user, fn %{users_count: count} ->
# if no registered users, make first user an admin # if no registered users, make first user an admin
role = if count == 0, do: "admin", else: "user" role = if count == 0, do: :admin, else: :user
User.registration_changeset(attrs) |> User.role_changeset(role) User.registration_changeset(attrs) |> User.role_changeset(role)
end) end)

View File

@ -50,7 +50,7 @@ defmodule Memex.Accounts.User do
@type new_user :: %__MODULE__{} @type new_user :: %__MODULE__{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_user()) @type changeset :: Changeset.t(t() | new_user())
@type role :: :user | :admin | String.t() @type role :: :user | :admin
@doc """ @doc """
A user changeset for registration. A user changeset for registration.
@ -84,7 +84,7 @@ defmodule Memex.Accounts.User do
""" """
@spec role_changeset(t() | new_user() | changeset(), role()) :: changeset() @spec role_changeset(t() | new_user() | changeset(), role()) :: changeset()
def role_changeset(user, role) do def role_changeset(user, role) do
user |> cast(%{"role" => role}, [:role]) user |> change(role: role)
end end
@spec validate_email(changeset()) :: changeset() @spec validate_email(changeset()) :: changeset()

View File

@ -29,7 +29,7 @@ defmodule Memex.Fixtures do
}) })
|> Accounts.register_user() |> Accounts.register_user()
|> unwrap_ok_tuple() |> unwrap_ok_tuple()
|> User.role_changeset("admin") |> User.role_changeset(:admin)
|> Repo.update!() |> Repo.update!()
end end