From a22b397fc445407ed9d122bd6fa24b08e343fbb2 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 27 Nov 2019 21:57:25 -0500 Subject: [PATCH] show forum list --- lib/philomena_web/plugs/forum_list_plug.ex | 24 +++++++++++++++++++ lib/philomena_web/router.ex | 1 + .../layout/_header_navigation.html.slime | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 lib/philomena_web/plugs/forum_list_plug.ex diff --git a/lib/philomena_web/plugs/forum_list_plug.ex b/lib/philomena_web/plugs/forum_list_plug.ex new file mode 100644 index 00000000..b5e5c2ef --- /dev/null +++ b/lib/philomena_web/plugs/forum_list_plug.ex @@ -0,0 +1,24 @@ +defmodule PhilomenaWeb.ForumListPlug do + alias Plug.Conn + + alias Philomena.Forums.Forum + alias Philomena.Repo + alias Canada.Can + import Ecto.Query + + def init(opts), do: opts + + def call(conn, _opts) do + forums = lookup_visible_forums(conn.assigns.current_user) + + conn + |> Conn.assign(:forums, forums) + end + + # fixme: add caching! + defp lookup_visible_forums(user) do + Forum + |> Repo.all() + |> Enum.filter(&Can.can?(user, :show, &1)) + end +end \ No newline at end of file diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 12af60c5..a8e48934 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -16,6 +16,7 @@ defmodule PhilomenaWeb.Router do plug PhilomenaWeb.CurrentBanPlug plug PhilomenaWeb.NotificationCountPlug plug PhilomenaWeb.SiteNoticePlug + plug PhilomenaWeb.ForumListPlug plug PhilomenaWeb.FilterSelectPlug end diff --git a/lib/philomena_web/templates/layout/_header_navigation.html.slime b/lib/philomena_web/templates/layout/_header_navigation.html.slime index 4eed4612..4e96d206 100644 --- a/lib/philomena_web/templates/layout/_header_navigation.html.slime +++ b/lib/philomena_web/templates/layout/_header_navigation.html.slime @@ -15,6 +15,10 @@ span data-click-preventdefault="true" i.fa.fa-caret-down< .dropdown__content + = for forum <- @conn.assigns.forums do + a.header__link href=Routes.forum_path(@conn, :show, forum) + = forum.name + a.header__link href='/posts' i.fa.fa-fw.fa-search> | Post Search