removed EctoEnum in favor of Ecto.Enum

This commit is contained in:
Adam Piontek 2021-03-22 15:41:11 -04:00
parent aabcd9e029
commit 3a6e3e8eed
6 changed files with 4 additions and 50 deletions

View file

@ -1,7 +1,6 @@
defmodule Shift73k.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
import EctoEnum
import Shift73k.Util.Dt, only: [weekdays: 0]
alias Shift73k.Shifts.Templates.ShiftTemplate
@ -13,8 +12,6 @@ defmodule Shift73k.Accounts.User do
admin: "Can delete users and change user roles"
]
defenum(RolesEnum, :role, Keyword.keys(@roles))
@max_email 254
@min_password 8
@max_password 80
@ -28,7 +25,7 @@ defmodule Shift73k.Accounts.User do
field(:hashed_password, :string)
field(:confirmed_at, :naive_datetime)
field(:role, RolesEnum, default: :user)
field(:role, Ecto.Enum, values: Keyword.keys(@roles), default: :user)
field(:week_start_at, Ecto.Enum, values: weekdays(), default: :monday)
has_many(:shift_templates, ShiftTemplate)
@ -69,13 +66,6 @@ defmodule Shift73k.Accounts.User do
|> validate_password(opts)
end
# def update_changeset(user, attrs, opts \\ [])
# def update_changeset(user, %{"password" => ""} = attrs, _),
# do: update_changeset_no_pw(user, attrs)
# def update_changeset(user, %{password: ""} = attrs, _), do: update_changeset_no_pw(user, attrs)
def update_changeset(user, attrs, opts) do
user
|> cast(attrs, [:email, :password, :role])
@ -84,21 +74,10 @@ defmodule Shift73k.Accounts.User do
|> validate_password_not_required(opts)
end
# def update_changeset_no_pw(user, attrs) do
# user
# |> cast(attrs, [:email, :role])
# |> validate_role()
# |> validate_email()
# end
defp role_validator(:role, role) do
(RolesEnum.valid_value?(role) && []) || [role: "invalid user role"]
end
defp validate_role(changeset) do
changeset
|> validate_required([:role])
|> validate_change(:role, &role_validator/2)
|> validate_inclusion(:role, Keyword.keys(@roles), message: "invalid user role")
end
defp validate_email_format(changeset) do

View file

@ -2,8 +2,6 @@ defmodule Shift73k.Shifts.Shift do
use Ecto.Schema
import Ecto.Changeset
alias Shift73k.Shifts.Templates.ShiftTemplate
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "shifts" do

View file

@ -1,10 +1,8 @@
defmodule Shift73kWeb.ShiftAssignLive.Index do
use Shift73kWeb, :live_view
alias Ecto.Multi
alias Shift73k.Repo
alias Shift73k.Shifts
alias Shift73k.Shifts.Shift
alias Shift73k.Shifts.Templates
alias Shift73k.Shifts.Templates.ShiftTemplate

View file

@ -48,7 +48,6 @@ defmodule Shift73k.MixProject do
{:gettext, "~> 0.11"},
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
{:ecto_enum, "~> 1.4"},
{:bamboo, "~> 2.0"},
{:bamboo_smtp, "~> 4.0"},
{:scrivener_ecto, "~> 2.0"},

View file

@ -8,8 +8,9 @@ defmodule Shift73k.Repo.Migrations.CreateUsersAuthTables do
add(:id, :binary_id, primary_key: true)
add(:email, :citext, null: false)
add(:hashed_password, :string, null: false)
add(:week_start_at, :string, null: false)
add(:role, :string, null: false)
add(:confirmed_at, :naive_datetime)
add(:week_start_at, :string, null: false)
timestamps()
end

View file

@ -1,21 +0,0 @@
defmodule Shift73k.Repo.Migrations.AddRoleToUsers do
use Ecto.Migration
alias Shift73k.Accounts.User.RolesEnum
def up do
RolesEnum.create_type()
alter table(:users) do
add(:role, RolesEnum.type(), null: false)
end
end
def down do
alter table(:users) do
remove(:role)
end
RolesEnum.drop_type()
end
end