Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Last active April 1, 2018 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankardevy/e9febbe754b0b54ff1b0e08e22aa9330 to your computer and use it in GitHub Desktop.
Save shankardevy/e9febbe754b0b54ff1b0e08e22aa9330 to your computer and use it in GitHub Desktop.
defmodule Mango.CRM.Customer do
use Ecto.Schema
import Ecto.Changeset
import Comeonin.Bcrypt, only: [hashpwsalt: 1]
alias Mango.CRM.Customer
schema "customers" do
field :email, :string
field :name, :string
field :password, :string, virtual: true
field :password_hash, :string
field :phone, :string
field :residence_area, :string
timestamps()
end
@doc false
def changeset(%Customer{} = customer, attrs) do
customer
|> cast(attrs, [:name, :email, :residence_area, :password, :phone])
|> validate_required([:name, :email, :residence_area, :password])
|> validate_format(:email, ~r/@/, message: "is invalid")
|> validate_length(:password, min: 6, max: 100)
|> unique_constraint(:email)
|> put_hashed_password
end
defp put_hashed_password(changeset) do
case changeset.valid? do
true ->
changes = changeset.changes
put_change(changeset, :password_hash, hashpwsalt(changes.password))
_ ->
changeset
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment