From d8b425bdf672f80fe6b1e1b24937e438313d546e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 17 Dec 2019 13:12:58 -0500 Subject: [PATCH] tag detail page --- .../controllers/tag/detail_controller.ex | 49 +++++++++++++++++++ lib/philomena_web/router.ex | 1 + .../templates/tag/_tag_info_row.html.slime | 1 + .../templates/tag/detail/_filters.html.slime | 31 ++++++++++++ .../templates/tag/detail/index.html.slime | 17 +++++++ lib/philomena_web/views/tag/detail_view.ex | 3 ++ 6 files changed, 102 insertions(+) create mode 100644 lib/philomena_web/controllers/tag/detail_controller.ex create mode 100644 lib/philomena_web/templates/tag/detail/_filters.html.slime create mode 100644 lib/philomena_web/templates/tag/detail/index.html.slime create mode 100644 lib/philomena_web/views/tag/detail_view.ex diff --git a/lib/philomena_web/controllers/tag/detail_controller.ex b/lib/philomena_web/controllers/tag/detail_controller.ex new file mode 100644 index 00000000..3893f6db --- /dev/null +++ b/lib/philomena_web/controllers/tag/detail_controller.ex @@ -0,0 +1,49 @@ +defmodule PhilomenaWeb.Tag.DetailController do + use PhilomenaWeb, :controller + + alias Philomena.Tags.Tag + alias Philomena.Filters.Filter + alias Philomena.Users.User + alias Philomena.Repo + import Ecto.Query + + plug :verify_authorized + plug :load_resource, model: Tag, id_name: "tag_id", id_field: "slug", persisted: true + + def index(conn, _params) do + tag = conn.assigns.tag + + filters_spoilering = + Filter + |> where([f], fragment("? @> ARRAY[?]::integer[]", f.spoilered_tag_ids, ^tag.id)) + |> preload(:user) + |> Repo.all() + + filters_hiding = + Filter + |> where([f], fragment("? @> ARRAY[?]::integer[]", f.hidden_tag_ids, ^tag.id)) + |> preload(:user) + |> Repo.all() + + users_watching = + User + |> where([u], fragment("? @> ARRAY[?]::integer[]", u.watched_tag_ids, ^tag.id)) + |> Repo.all() + + render( + conn, + "index.html", + title: "Tag Usage for Tag `#{tag.name}'", + filters_spoilering: filters_spoilering, + filters_hiding: filters_hiding, + users_watching: users_watching + ) + end + + defp verify_authorized(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :edit, %Tag{}) do + true -> conn + _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + end + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 50e5b264..80ce4109 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -174,6 +174,7 @@ defmodule PhilomenaWeb.Router do resources "/tags", TagController, only: [] do resources "/watch", Tag.WatchController, only: [:create, :delete], singleton: true + resources "/details", Tag.DetailController, only: [:index] end resources "/avatar", AvatarController, only: [:edit, :update, :delete], singleton: true diff --git a/lib/philomena_web/templates/tag/_tag_info_row.html.slime b/lib/philomena_web/templates/tag/_tag_info_row.html.slime index 5f985d25..77398940 100644 --- a/lib/philomena_web/templates/tag/_tag_info_row.html.slime +++ b/lib/philomena_web/templates/tag/_tag_info_row.html.slime @@ -10,6 +10,7 @@ = link "Tag changes", to: Routes.tag_tag_change_path(@conn, :index, @tag), class: "detail-link" = if manages_tags?(@conn) do = link "Edit details", to: Routes.tag_path(@conn, :edit, @tag), class: "detail-link" + = link "Usage", to: Routes.tag_detail_path(@conn, :index, @tag), class: "detail-link" br diff --git a/lib/philomena_web/templates/tag/detail/_filters.html.slime b/lib/philomena_web/templates/tag/detail/_filters.html.slime new file mode 100644 index 00000000..b09071a8 --- /dev/null +++ b/lib/philomena_web/templates/tag/detail/_filters.html.slime @@ -0,0 +1,31 @@ +table.table + thead + tr + th Filter + th Type + th Owner + th Spoilers + th Hides + tbody + = for filter <- @filters do + tr + td = link filter.name, to: Routes.filter_path(@conn, :show, filter) + td + = cond do + - filter.system -> + ' System + + - filter.public -> + ' Public + + - true -> + ' Private + + td + = if filter.user do + = link filter.user.name, to: Routes.profile_path(@conn, :show, filter.user) + - else + ' No user associated + + td = length(filter.spoilered_tag_ids) + td = length(filter.hidden_tag_ids) diff --git a/lib/philomena_web/templates/tag/detail/index.html.slime b/lib/philomena_web/templates/tag/detail/index.html.slime new file mode 100644 index 00000000..8a11358a --- /dev/null +++ b/lib/philomena_web/templates/tag/detail/index.html.slime @@ -0,0 +1,17 @@ +h1 + ' Tag Usage for + = link @tag.name, to: Routes.tag_path(@conn, :show, @tag) + +h3 Filters that spoiler this tag: += render PhilomenaWeb.Tag.DetailView, "_filters.html", filters: @filters_spoilering, conn: @conn + +h3 Filters that hide this tag: += render PhilomenaWeb.Tag.DetailView, "_filters.html", filters: @filters_hiding, conn: @conn + +h3 + | Users that watch this tag ( + = length(@users_watching) + | ) + += for u <- @users_watching do + = link u.name, to: Routes.profile_path(@conn, :show, u), class: "interaction-user-list-item" diff --git a/lib/philomena_web/views/tag/detail_view.ex b/lib/philomena_web/views/tag/detail_view.ex new file mode 100644 index 00000000..68a3e4ef --- /dev/null +++ b/lib/philomena_web/views/tag/detail_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Tag.DetailView do + use PhilomenaWeb, :view +end