From 2f4ba6d96d783bfbe0476e66f2f5fc7984e0a168 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 18 Nov 2019 12:32:23 -0500 Subject: [PATCH] read site notices --- lib/philomena/site_notices.ex | 9 +++++-- lib/philomena/site_notices/site_notice.ex | 2 +- lib/philomena_web/plugs/site_notice_plug.ex | 25 +++++++++++++++++++ lib/philomena_web/router.ex | 1 + .../layout/_flash_warnings.html.slime | 9 +++++++ 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 lib/philomena_web/plugs/site_notice_plug.ex diff --git a/lib/philomena/site_notices.ex b/lib/philomena/site_notices.ex index e757466c..dcd8b04b 100644 --- a/lib/philomena/site_notices.ex +++ b/lib/philomena/site_notices.ex @@ -17,8 +17,13 @@ defmodule Philomena.SiteNotices do [%SiteNotice{}, ...] """ - def list_site_notices do - Repo.all(SiteNotice) + def active_site_notices do + now = DateTime.utc_now() + + SiteNotice + |> where([n], n.start_date < ^now and n.finish_date > ^now) + |> order_by(desc: :start_date) + |> Repo.all() end @doc """ diff --git a/lib/philomena/site_notices/site_notice.ex b/lib/philomena/site_notices/site_notice.ex index 42928ada..5888fbdc 100644 --- a/lib/philomena/site_notices/site_notice.ex +++ b/lib/philomena/site_notices/site_notice.ex @@ -15,7 +15,7 @@ defmodule Philomena.SiteNotices.SiteNotice do field :start_date, :naive_datetime field :finish_date, :naive_datetime - timestamps(inserted_at: :created_At) + timestamps(inserted_at: :created_at) end @doc false diff --git a/lib/philomena_web/plugs/site_notice_plug.ex b/lib/philomena_web/plugs/site_notice_plug.ex new file mode 100644 index 00000000..781d9ba0 --- /dev/null +++ b/lib/philomena_web/plugs/site_notice_plug.ex @@ -0,0 +1,25 @@ +defmodule PhilomenaWeb.SiteNoticePlug do + @moduledoc """ + This plug stores the current site-wide notices. + + ## Example + + plug PhilomenaWeb.SiteNoticePlug + """ + + alias Plug.Conn + alias Philomena.SiteNotices + + @doc false + @spec init(any()) :: any() + def init(opts), do: opts + + @doc false + @spec call(Plug.Conn.t(), any()) :: Plug.Conn.t() + def call(conn, _opts) do + notices = SiteNotices.active_site_notices() + + conn + |> Conn.assign(:site_notices, notices) + end +end \ No newline at end of file diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 5773bfc1..b7f2e008 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -15,6 +15,7 @@ defmodule PhilomenaWeb.Router do plug PhilomenaWeb.EnsureUserEnabledPlug plug PhilomenaWeb.CurrentBanPlug plug PhilomenaWeb.NotificationCountPlug + plug PhilomenaWeb.SiteNoticePlug plug PhilomenaWeb.FilterSelectPlug end diff --git a/lib/philomena_web/templates/layout/_flash_warnings.html.slime b/lib/philomena_web/templates/layout/_flash_warnings.html.slime index 5ddb0bcd..015cebe8 100644 --- a/lib/philomena_web/templates/layout/_flash_warnings.html.slime +++ b/lib/philomena_web/templates/layout/_flash_warnings.html.slime @@ -1,3 +1,12 @@ += for notice <- @conn.assigns.site_notices do + .flash.flash--site-notice + strong> + = notice.title + => notice.text + = if notice.link not in [nil, ""] do + a href=notice.link + = notice.link_text + noscript.flash.flash--warning strong You don't appear to have Javascript enabled ' If you're using an add-on like NoScript, please allow