Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Created February 11, 2016 22:19
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 paulcsmith/e87f1c17e8f73f411d3a to your computer and use it in GitHub Desktop.
Save paulcsmith/e87f1c17e8f73f411d3a to your computer and use it in GitHub Desktop.
defmodule MyApp.Changeset do
def cast(model_or_changeset, :empty, required, optional) do
Ecto.Changeset.cast(model_or_changeset, :empty, required, optional)
end
def cast(model_or_changeset, params, required, optional) do
param_keys = params |> Map.keys |> Enum.map(&to_string/1)
allowed_params = (required ++ optional) |> Enum.map(&to_string/1)
disallowed_params = param_keys -- allowed_params
if length(disallowed_params) > 0 do
Logger.debug """
Some params were removed from the changeset.
Given params: #{inspect param_keys}
Allowed params: #{inspect allowed_params}
Disallowed params: #{inspect disallowed_params}
"""
end
Ecto.Changeset.cast(model_or_changeset, params, required, optional)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment