From 3ae890c193dceca215553cd910c6f6456f9901fe Mon Sep 17 00:00:00 2001 From: shibao Date: Sun, 29 Jan 2023 12:57:07 -0500 Subject: [PATCH] use atoms for role changeset --- lib/cannery/accounts.ex | 2 +- lib/cannery/accounts/user.ex | 4 ++-- test/support/fixtures.ex | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cannery/accounts.ex b/lib/cannery/accounts.ex index f088fac8..35bcea16 100644 --- a/lib/cannery/accounts.ex +++ b/lib/cannery/accounts.ex @@ -112,7 +112,7 @@ defmodule Cannery.Accounts do |> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true)) |> Multi.insert(:add_user, fn %{users_count: count} -> # 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) end) diff --git a/lib/cannery/accounts/user.ex b/lib/cannery/accounts/user.ex index ef98e000..237e5f3d 100644 --- a/lib/cannery/accounts/user.ex +++ b/lib/cannery/accounts/user.ex @@ -48,7 +48,7 @@ defmodule Cannery.Accounts.User do @type new_user :: %User{} @type id :: UUID.t() @type changeset :: Changeset.t(t() | new_user()) - @type role :: :admin | :user | String.t() + @type role :: :admin | :user @doc """ A user changeset for registration. @@ -81,7 +81,7 @@ defmodule Cannery.Accounts.User do """ @spec role_changeset(t() | new_user() | changeset(), role()) :: changeset() def role_changeset(user, role) do - user |> cast(%{"role" => role}, [:role]) + user |> change(role: role) end @spec validate_email(changeset()) :: changeset() diff --git a/test/support/fixtures.ex b/test/support/fixtures.ex index 63254c85..6c0be31c 100644 --- a/test/support/fixtures.ex +++ b/test/support/fixtures.ex @@ -43,7 +43,7 @@ defmodule Cannery.Fixtures do }) |> Accounts.register_user() |> unwrap_ok_tuple() - |> User.role_changeset("admin") + |> User.role_changeset(:admin) |> Repo.update!() end