Compare commits
No commits in common. "725df0552189a1b77b3b35ea82a3bf3fc454ba63" and "75fcbb1e6571c24f06822ba2526ec49267f12c01" have entirely different histories.
725df05521
...
75fcbb1e65
@ -112,7 +112,7 @@ defmodule Cannery.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)
|
||||||
|
@ -48,7 +48,7 @@ defmodule Cannery.Accounts.User do
|
|||||||
@type new_user :: %User{}
|
@type new_user :: %User{}
|
||||||
@type id :: UUID.t()
|
@type id :: UUID.t()
|
||||||
@type changeset :: Changeset.t(t() | new_user())
|
@type changeset :: Changeset.t(t() | new_user())
|
||||||
@type role :: :admin | :user
|
@type role :: :admin | :user | String.t()
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
A user changeset for registration.
|
A user changeset for registration.
|
||||||
@ -81,7 +81,7 @@ defmodule Cannery.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 |> change(role: role)
|
user |> cast(%{"role" => role}, [:role])
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec validate_email(changeset()) :: changeset()
|
@spec validate_email(changeset()) :: changeset()
|
||||||
|
@ -21,8 +21,8 @@ defmodule Cannery.Fixtures do
|
|||||||
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
|
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
|
||||||
def valid_user_password, do: "hello world!"
|
def valid_user_password, do: "hello world!"
|
||||||
|
|
||||||
@spec user_fixture() :: User.t()
|
@spec user_fixture() :: Accounts.User.t()
|
||||||
@spec user_fixture(attrs :: map()) :: User.t()
|
@spec user_fixture(attrs :: map()) :: Accounts.User.t()
|
||||||
def user_fixture(attrs \\ %{}) do
|
def user_fixture(attrs \\ %{}) do
|
||||||
attrs
|
attrs
|
||||||
|> Enum.into(%{
|
|> Enum.into(%{
|
||||||
@ -33,8 +33,8 @@ defmodule Cannery.Fixtures do
|
|||||||
|> unwrap_ok_tuple()
|
|> unwrap_ok_tuple()
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec admin_fixture() :: User.t()
|
@spec admin_fixture() :: Accounts.User.t()
|
||||||
@spec admin_fixture(attrs :: map()) :: User.t()
|
@spec admin_fixture(attrs :: map()) :: Accounts.User.t()
|
||||||
def admin_fixture(attrs \\ %{}) do
|
def admin_fixture(attrs \\ %{}) do
|
||||||
attrs
|
attrs
|
||||||
|> Enum.into(%{
|
|> Enum.into(%{
|
||||||
@ -43,7 +43,7 @@ defmodule Cannery.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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user