Created
December 21, 2022 02:18
-
-
Save mazz/682464fdfc7a3d3fee9dc5d082b9f2db to your computer and use it in GitHub Desktop.
why :membership_type and :role are not added to the changeset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_changeset(membership, attrs) do | |
dbg(membership) | |
dbg(attrs) | |
# attrs #=> %{is_paid: false, membership_type: "personal", role: "admin"} | |
to_atom_attrs = | |
Enum.map(attrs, fn {k, v} -> | |
if is_atom(v) do | |
{k, v} | |
else | |
{k, String.to_existing_atom(v)} | |
end | |
end) | |
|> Enum.into(%{}) | |
dbg(to_atom_attrs) | |
# to_atom_attrs #=> %{is_paid: false, membership_type: :personal, role: :admin} | |
membership | |
|> cast(to_atom_attrs, [:role, :is_paid, :membership_type]) | |
|> validate_required([:role]) | |
|> validate_inclusion(:role, @role_options) | |
|> prepare_changes(fn changeset -> | |
current_role = membership.role | |
new_role = get_change(changeset, :role) | |
dbg(new_role) | |
# new_role #=> nil | |
# changeset #=> #Ecto.Changeset< | |
# action: :update, | |
# changes: %{is_paid: false}, | |
# errors: [], | |
# data: #Markably.Orgs.Membership<>, | |
# valid?: true | |
# > | |
if current_role == @admin_role && new_role != current_role do | |
validate_at_least_one_admin(changeset) | |
else | |
changeset | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.