switched to 'native' Ecto.Enum for user week_starts_at field
This commit is contained in:
parent
4c673508b5
commit
aabcd9e029
6 changed files with 15 additions and 16 deletions
|
@ -2,8 +2,8 @@ defmodule Shift73k.Accounts.User do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import EctoEnum
|
import EctoEnum
|
||||||
|
import Shift73k.Util.Dt, only: [weekdays: 0]
|
||||||
|
|
||||||
alias Shift73k.EctoEnums.WeekdayEnum
|
|
||||||
alias Shift73k.Shifts.Templates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
alias Shift73k.Shifts.Shift
|
alias Shift73k.Shifts.Shift
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ defmodule Shift73k.Accounts.User do
|
||||||
field(:confirmed_at, :naive_datetime)
|
field(:confirmed_at, :naive_datetime)
|
||||||
|
|
||||||
field(:role, RolesEnum, default: :user)
|
field(:role, RolesEnum, default: :user)
|
||||||
field(:week_start_at, WeekdayEnum, default: 1)
|
field(:week_start_at, Ecto.Enum, values: weekdays(), default: :monday)
|
||||||
|
|
||||||
has_many(:shift_templates, ShiftTemplate)
|
has_many(:shift_templates, ShiftTemplate)
|
||||||
belongs_to(:fave_shift_template, ShiftTemplate)
|
belongs_to(:fave_shift_template, ShiftTemplate)
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
defmodule Shift73k.EctoEnums do
|
|
||||||
import EctoEnum
|
|
||||||
|
|
||||||
@weekdays [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
|
|
||||||
|> Enum.with_index(1)
|
|
||||||
|
|
||||||
defenum(WeekdayEnum, @weekdays)
|
|
||||||
end
|
|
|
@ -2,5 +2,9 @@ defmodule Shift73k.Util.Dt do
|
||||||
@app_vars Application.get_env(:shift73k, :app_global_vars, time_zone: "America/New_York")
|
@app_vars Application.get_env(:shift73k, :app_global_vars, time_zone: "America/New_York")
|
||||||
@app_time_zone @app_vars[:time_zone]
|
@app_time_zone @app_vars[:time_zone]
|
||||||
|
|
||||||
|
@weekdays [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
|
||||||
|
|
||||||
def app_time_zone, do: @app_time_zone
|
def app_time_zone, do: @app_time_zone
|
||||||
|
|
||||||
|
def weekdays, do: @weekdays
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
defmodule Shift73kWeb.UserLive.Settings.WeekStart do
|
defmodule Shift73kWeb.UserLive.Settings.WeekStart do
|
||||||
use Shift73kWeb, :live_component
|
use Shift73kWeb, :live_component
|
||||||
|
import Shift73k.Util.Dt, only: [weekdays: 0]
|
||||||
|
|
||||||
alias Shift73k.EctoEnums.WeekdayEnum
|
|
||||||
alias Shift73k.Accounts
|
alias Shift73k.Accounts
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -14,13 +14,12 @@ defmodule Shift73kWeb.UserLive.Settings.WeekStart do
|
||||||
end
|
end
|
||||||
|
|
||||||
def week_start_options do
|
def week_start_options do
|
||||||
{week_start_at, _} = WeekdayEnum.__enum_map__() |> hd()
|
week_start = Date.beginning_of_week(Date.utc_today(), hd(weekdays()))
|
||||||
week_start = Date.beginning_of_week(Date.utc_today(), week_start_at)
|
|
||||||
|
|
||||||
week_start
|
week_start
|
||||||
|> Date.range(Date.add(week_start, 6))
|
|> Date.range(Date.add(week_start, 6))
|
||||||
|> Enum.map(&Calendar.strftime(&1, "%A"))
|
|> Enum.map(&Calendar.strftime(&1, "%A"))
|
||||||
|> Enum.zip(Keyword.keys(WeekdayEnum.__enum_map__()))
|
|> Enum.zip(weekdays())
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
|
@ -8,7 +8,7 @@ defmodule Shift73k.Repo.Migrations.CreateUsersAuthTables do
|
||||||
add(:id, :binary_id, primary_key: true)
|
add(:id, :binary_id, primary_key: true)
|
||||||
add(:email, :citext, null: false)
|
add(:email, :citext, null: false)
|
||||||
add(:hashed_password, :string, null: false)
|
add(:hashed_password, :string, null: false)
|
||||||
add(:week_start_at, :integer, null: false)
|
add(:week_start_at, :string, null: false)
|
||||||
add(:confirmed_at, :naive_datetime)
|
add(:confirmed_at, :naive_datetime)
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -55,6 +55,10 @@ extra_mock_users = ~s([
|
||||||
{"email":"kat@73k.us","password":"katkatA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true}
|
{"email":"kat@73k.us","password":"katkatA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true}
|
||||||
])
|
])
|
||||||
|
|
||||||
|
# for random week_start_at values
|
||||||
|
[head | tail] = Shift73k.Util.Dt.weekdays()
|
||||||
|
week_starts = [head | Enum.drop(tail, 4)]
|
||||||
|
|
||||||
mock_users =
|
mock_users =
|
||||||
extra_mock_users
|
extra_mock_users
|
||||||
|> Jason.decode!()
|
|> Jason.decode!()
|
||||||
|
@ -66,7 +70,7 @@ mock_users =
|
||||||
email: e["email"],
|
email: e["email"],
|
||||||
role: String.to_existing_atom(e["role"]),
|
role: String.to_existing_atom(e["role"]),
|
||||||
hashed_password: Bcrypt.hash_pwd_salt(e["password"]),
|
hashed_password: Bcrypt.hash_pwd_salt(e["password"]),
|
||||||
week_start_at: :rand.uniform(2),
|
week_start_at: Enum.at(week_starts, Enum.random(0..2)),
|
||||||
inserted_at: add_dt,
|
inserted_at: add_dt,
|
||||||
updated_at: add_dt,
|
updated_at: add_dt,
|
||||||
confirmed_at: (e["confirmed_at"] && NaiveDateTime.add(add_dt, 300, :second)) || nil
|
confirmed_at: (e["confirmed_at"] && NaiveDateTime.add(add_dt, 300, :second)) || nil
|
||||||
|
|
Loading…
Reference in a new issue