Add post visibility and days_until_live helpers
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user