From 7f5ae482dbd71688cd09917009f5728f025a9c7d Mon Sep 17 00:00:00 2001 From: Adam Piontek Date: Wed, 7 Apr 2021 14:00:07 -0400 Subject: [PATCH] filter post list to reject future dates, allowing scheduled posts --- lib/home73k/blog.ex | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/home73k/blog.ex b/lib/home73k/blog.ex index d533c43..e1cbee8 100644 --- a/lib/home73k/blog.ex +++ b/lib/home73k/blog.ex @@ -24,10 +24,22 @@ defmodule Home73k.Blog do @posts Enum.sort_by(posts, & &1.date, {:desc, NaiveDateTime}) @post_count length(@posts) - @tags posts |> Stream.flat_map(& &1.tags) |> Stream.uniq() |> Enum.sort() - def list_posts, do: @posts - def list_tags, do: @tags + + defp list_all_posts, do: @posts + defp list_posts_reject_future, do: list_all_posts() |> Enum.reject(&(&1.date <= NaiveDateTime.utc_now())) + + # Enum reject posts with future date, allowing scheduled posts + def list_posts, do: list_posts_reject_future() + + # List tags, but not for future p + def list_tags do + list_all_posts() + |> Stream.reject(&(&1.date <= NaiveDateTime.utc_now())) + |> Stream.flat_map(& &1.tags) + |> Stream.uniq() + |> Enum.sort() + end def post_count, do: @post_count