Add post visibility and days_until_live helpers

This commit is contained in:
Willem van den Ende
2026-04-01 20:24:33 +00:00
parent 17a0f2709c
commit 037e9f86ff
3 changed files with 74 additions and 0 deletions
+17
View File
@@ -44,6 +44,23 @@ defmodule Blogex.Post do
published: boolean()
}
@type visibility :: :draft | :scheduled | :live
@doc "Returns the visibility of a post: :draft, :scheduled, or :live."
def visibility(%__MODULE__{published: false}), do: :draft
def visibility(%__MODULE__{published: true, date: date}) do
if Date.after?(date, Date.utc_today()), do: :scheduled, else: :live
end
@doc "Returns days until a scheduled post goes live, or nil."
def days_until_live(%__MODULE__{} = post) do
case visibility(post) do
:scheduled -> Date.diff(post.date, Date.utc_today())
_ -> nil
end
end
@doc """
Build callback for NimblePublisher.