Skip to content

Instantly share code, notes, and snippets.

@samdvr

samdvr/event.ex Secret

Created May 4, 2017 12:04
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 samdvr/49187575fc60c140046d20594f6593ea to your computer and use it in GitHub Desktop.
Save samdvr/49187575fc60c140046d20594f6593ea to your computer and use it in GitHub Desktop.
Elixir DateTime
defmodule Repro.Event do
use Repro.Web, :model
schema "events" do
field :title, :string
field :location, :string
field :date, Ecto.Date
field :description, :string
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title, :location, :date, :description])
|> validate_required([:title, :location, :date, :description])
|> validate_change(:date, &must_be_future/2)
end
defp must_be_future(_, value) do
Ecto.DateTime.compare(Ecto.DateTime.from_date(value), Ecto.DateTime.utc)
|> get_error
end
defp get_error(comparison) when comparison == :lt do
[date: "cant be in the past"]
end
defp get_error(_), do: []
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment