2020-02-18 08:57:00 +01:00
|
|
|
defmodule Mobilizon.Todos.Todo do
|
|
|
|
@moduledoc """
|
|
|
|
Represents a todo, or task
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2020-08-14 11:32:23 +02:00
|
|
|
import Mobilizon.Storage.Ecto, only: [ensure_url: 2, maybe_add_published_at: 1]
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Todos.TodoList
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
2021-09-28 19:40:37 +02:00
|
|
|
id: String.t(),
|
|
|
|
url: String.t(),
|
2020-02-18 08:57:00 +01:00
|
|
|
status: boolean(),
|
|
|
|
title: String.t(),
|
|
|
|
due_date: DateTime.t(),
|
|
|
|
todo_list: TodoList.t(),
|
|
|
|
creator: Actor.t(),
|
|
|
|
assigned_to: Actor.t(),
|
2020-08-14 11:32:23 +02:00
|
|
|
local: boolean,
|
|
|
|
published_at: DateTime.t()
|
2020-02-18 08:57:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
|
|
schema "todos" do
|
|
|
|
field(:status, :boolean, default: false)
|
|
|
|
field(:title, :string)
|
|
|
|
field(:url, :string)
|
|
|
|
field(:due_date, :utc_datetime)
|
|
|
|
field(:local, :boolean, default: true)
|
2020-08-14 11:32:23 +02:00
|
|
|
field(:published_at, :utc_datetime)
|
2020-02-18 08:57:00 +01:00
|
|
|
belongs_to(:todo_list, TodoList, type: :binary_id)
|
|
|
|
belongs_to(:creator, Actor)
|
|
|
|
belongs_to(:assigned_to, Actor)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
@required_attrs [:title, :creator_id, :url, :todo_list_id, :published_at]
|
2020-02-18 08:57:00 +01:00
|
|
|
@optional_attrs [:status, :due_date, :assigned_to_id, :local]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
|
|
|
@doc false
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
2020-02-18 08:57:00 +01:00
|
|
|
def changeset(todo, attrs) do
|
|
|
|
todo
|
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> ensure_url(:todo)
|
2020-08-14 11:32:23 +02:00
|
|
|
|> maybe_add_published_at()
|
2020-02-18 08:57:00 +01:00
|
|
|
|> validate_required(@required_attrs)
|
|
|
|
end
|
|
|
|
end
|