cannery/lib/cannery/tags/tag.ex

53 lines
1.4 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule Cannery.Tags.Tag do
2022-01-22 21:40:29 -05:00
@moduledoc """
Tags are added to containers to help organize, and can include custom-defined
text and bg colors.
"""
2021-09-02 23:31:14 -04:00
use Ecto.Schema
import Ecto.Changeset
2022-01-22 21:40:29 -05:00
alias Ecto.{Changeset, UUID}
alias Cannery.{Accounts.User, Tags.Tag}
2021-09-02 23:31:14 -04:00
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "tags" do
field :name, :string
2021-09-12 19:26:04 -04:00
field :bg_color, :string
2021-09-10 00:41:06 -04:00
field :text_color, :string
2022-01-22 21:40:29 -05:00
belongs_to :user, User
2021-09-02 23:31:14 -04:00
timestamps()
end
2022-01-22 21:40:29 -05:00
@type t :: %Tag{
2022-01-31 20:08:01 -05:00
id: id(),
2021-09-12 19:26:04 -04:00
name: String.t(),
bg_color: String.t(),
text_color: String.t(),
2022-02-09 23:21:42 -05:00
user: User.t() | nil,
2022-01-31 20:08:01 -05:00
user_id: User.id(),
2021-09-12 19:26:04 -04:00
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
2022-01-22 21:40:29 -05:00
@type new_tag() :: %Tag{}
2022-01-31 20:08:01 -05:00
@type id() :: UUID.t()
2022-01-22 21:40:29 -05:00
2021-09-02 23:31:14 -04:00
@doc false
2022-02-14 20:51:09 -05:00
@spec create_changeset(new_tag(), attrs :: map()) :: Changeset.t(new_tag())
2022-02-09 23:21:42 -05:00
def create_changeset(tag, attrs) do
2021-09-02 23:31:14 -04:00
tag
2021-09-10 22:43:12 -04:00
|> cast(attrs, [:name, :bg_color, :text_color, :user_id])
|> validate_required([:name, :bg_color, :text_color, :user_id])
2021-09-02 23:31:14 -04:00
end
2022-02-09 23:21:42 -05:00
@doc false
@spec update_changeset(t() | new_tag(), attrs :: map()) :: Changeset.t(t() | new_tag())
def update_changeset(tag, attrs) do
tag
|> cast(attrs, [:name, :bg_color, :text_color])
|> validate_required([:name, :bg_color, :text_color, :user_id])
end
2021-09-02 23:31:14 -04:00
end