Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Created January 9, 2016 00:04
Show Gist options
  • Save terakilobyte/af953512280fd7a0ad45 to your computer and use it in GitHub Desktop.
Save terakilobyte/af953512280fd7a0ad45 to your computer and use it in GitHub Desktop.
defmodule Votio.Topic do
use Votio.Web, :model
schema "topics" do
field :title, :string
field :categories, :map
field :voted_by, {:array, :id}
timestamps
end
@required_fields ~w(title categories)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> unique_constraint(:title)
# Thanks Joef!
|> validate_change(:categories, fn(:categories, categories) ->
cond do
map_size(categories) < 2 -> [categories: "Must have at least two choices."]
true -> []
end
end)
|> validate_length(:title, min: 5, message: "Your title must be at least 5 characters")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment