Skip to content

Instantly share code, notes, and snippets.

@dgladkov
Created June 24, 2016 12:32
Show Gist options
  • Save dgladkov/b2dae866792ba9953b0070dec5ecf4b9 to your computer and use it in GitHub Desktop.
Save dgladkov/b2dae866792ba9953b0070dec5ecf4b9 to your computer and use it in GitHub Desktop.
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :email, :string
field :encrypted_password, :string
field :is_admin, :boolean, default: false
field :password, :string, virtual: true
timestamps
end
@allowed_fields [
:email,
:encrypted_password,
:is_admin,
:password,
]
@sensitive_fields [
:is_admin,
]
def changeset(model, params \\ :empty) do
model
|> cast(params, @allowed_fields)
end
def secure_changeset(model, params \\ :empty) do
Enum.reduce(@sensitive_fields, changeset(model, params), &(delete_change(&2, &1)))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment