From 6e6824070858484393b7324f9e1a4053cd7a13c1 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Wed, 4 Dec 2019 11:18:58 +0000 Subject: [PATCH] define put_email_hash function, #29 --- lib/app/ctx/person.ex | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/app/ctx/person.ex b/lib/app/ctx/person.ex index ac2a3848..adfcb4e9 100644 --- a/lib/app/ctx/person.ex +++ b/lib/app/ctx/person.ex @@ -22,18 +22,28 @@ defmodule App.Ctx.Person do end @doc false - def changeset(person, %{"email" => email} = attrs) do + def changeset(person, attrs) do person |> cast(attrs, [:username, :email, :givenName, :familyName, :password_hash, :key_id, :locale, :picture]) |> validate_required([:username, :email, :givenName, :familyName, :password_hash, :key_id]) - |> put_change(:email_hash, email ) + |> put_email_hash() end - def google_changeset(profile, %{"email" => email} = attrs) do + def google_changeset(profile, attrs) do profile |> cast(attrs, [:email, :givenName, :familyName, :picture, :locale]) |> validate_required([:email]) - |> put_change(:email_hash, email ) + |> put_email_hash() + end + + defp put_email_hash(changeset) do + case changeset do + %{valid?: true, changes: %{email: email}} -> + put_change(changeset, :email_hash, email) + + _ -> + changeset + end end @doc """