Mon Nov 1
I took the weekend to work through how to add a name
column to the scaffolding generated by mix phx.gen.auth
to more closely match Laravel Jetstream's profile update flow. In Jetstream, the photo, name, and email address are part of the same change request.
For gen.auth
, email changes are gated behind a validation request happening first. This makes sense, you want to make sure the email is valid and accessible before you change the user to it. The problem with this is that lumping the name into the same changset creates a slight code smell.
To carve out saving name involves somewhat of a side effect as we modify email_changeset
in lib/opportunity_knocks/accounts/user.ex
to allow the pattern %{changes: %{name: _}} = changeset -> changeset |> delete_change(:email)
in the case statement. This allows name to go through but we push email out of the change in the event both name and email are updated at the same time. This lets us change apply_user_email
in lib/opportunity_knocks/accounts.ex
to use the snippet:
|> case do
%{changes: %{name: _}} = changeset -> changeset |> Repo.update()
%{changes: %{email: _}} = changeset -> changeset |> Ecto.Changeset.apply_action(:update)
This is where the smell is particularly strong as I'm doing the same pattern matching to allow the change in the case of name
and ignore it for email
. I believe as I gain more Elixir knowledge I may refactor this but I also know how deviation can be due to my exposure to Laravel Shift. Shift uses a series of patches to perform framework upgrades. Files that are not altered get changed more easily and when a deviation is detected (merge conflict), Shift goes with its version so you are consistently applying your changes on top of the now improved version. This is the approach we would all take given time or time machines.