From a7d9386af81bc42c12cfd229bafd0063f2a1c361 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 20 Dec 2019 12:26:38 -0500 Subject: [PATCH] mod faves page --- .../controllers/image/favorite_controller.ex | 27 +++++++++++++++++++ .../controllers/image/favorites_controller.ex | 11 -------- lib/philomena_web/router.ex | 2 +- .../templates/image/_options.html.slime | 2 +- .../templates/image/favorite/index.html.slime | 24 +++++++++++++++++ .../image/favorites/index.html.slime | 7 ----- .../views/image/favorite_view.ex | 3 +++ .../views/image/favorites_view.ex | 3 --- 8 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 lib/philomena_web/controllers/image/favorite_controller.ex delete mode 100644 lib/philomena_web/controllers/image/favorites_controller.ex create mode 100644 lib/philomena_web/templates/image/favorite/index.html.slime delete mode 100644 lib/philomena_web/templates/image/favorites/index.html.slime create mode 100644 lib/philomena_web/views/image/favorite_view.ex delete mode 100644 lib/philomena_web/views/image/favorites_view.ex diff --git a/lib/philomena_web/controllers/image/favorite_controller.ex b/lib/philomena_web/controllers/image/favorite_controller.ex new file mode 100644 index 00000000..8d216cf1 --- /dev/null +++ b/lib/philomena_web/controllers/image/favorite_controller.ex @@ -0,0 +1,27 @@ +defmodule PhilomenaWeb.Image.FavoriteController do + use PhilomenaWeb, :controller + + alias Philomena.Images.Image + alias Philomena.Repo + + plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true, preload: [faves: :user] + plug :load_votes_if_authorized + + def index(conn, _params) do + render(conn, "index.html", layout: false) + end + + defp load_votes_if_authorized(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :hide, conn.assigns.image) do + true -> + image = Repo.preload(conn.assigns.image, upvotes: :user, downvotes: :user) + + conn + |> assign(:image, image) + |> assign(:has_votes, true) + + false -> + assign(conn, :has_votes, false) + end + end +end diff --git a/lib/philomena_web/controllers/image/favorites_controller.ex b/lib/philomena_web/controllers/image/favorites_controller.ex deleted file mode 100644 index 8ffbe97f..00000000 --- a/lib/philomena_web/controllers/image/favorites_controller.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule PhilomenaWeb.Image.FavoritesController do - use PhilomenaWeb, :controller - - alias Philomena.Images.Image - - plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true, preload: [faves: :user] - - def index(conn, _params) do - render(conn, "index.html", layout: false, image: conn.assigns.image) - end -end \ No newline at end of file diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 3e5b7b3c..92aa7851 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -287,7 +287,7 @@ defmodule PhilomenaWeb.Router do resources "/navigate", Image.NavigateController, only: [:index] resources "/reports", Image.ReportController, only: [:new, :create] resources "/reporting", Image.ReportingController, only: [:show], singleton: true - resources "/favorites", Image.FavoritesController, only: [:index] + resources "/favorites", Image.FavoriteController, only: [:index] end scope "/tags", Tag, as: :tag do resources "/autocomplete", AutocompleteController, only: [:show], singleton: true diff --git a/lib/philomena_web/templates/image/_options.html.slime b/lib/philomena_web/templates/image/_options.html.slime index 1dec4a2c..b779fe7f 100644 --- a/lib/philomena_web/templates/image/_options.html.slime +++ b/lib/philomena_web/templates/image/_options.html.slime @@ -8,7 +8,7 @@ a href="#" data-click-tab="sharing" i.fa.fa-share> | Share - a href="#" data-click-tab="favoriters" data-load-tab=Routes.image_favorites_path(@conn, :index, @image) + a href="#" data-click-tab="favoriters" data-load-tab=Routes.image_favorite_path(@conn, :index, @image) i.fa.fa-star> | List favoriters = if display_mod_tools? do diff --git a/lib/philomena_web/templates/image/favorite/index.html.slime b/lib/philomena_web/templates/image/favorite/index.html.slime new file mode 100644 index 00000000..5bbdf7ea --- /dev/null +++ b/lib/philomena_web/templates/image/favorite/index.html.slime @@ -0,0 +1,24 @@ +h5 + ' Faved by + => @image.faves_count + = pluralize("user", "users", @image.faves_count) + += for fave <- Enum.sort_by(@image.faves, & &1.user.name) do + => link fave.user.name, to: Routes.profile_path(@conn, :show, fave.user), class: "interaction-user-list-item" + += if @has_votes do + h5 + ' Upvoted by + => @image.upvotes_count + = pluralize("user", "users", @image.faves_count) + + = for upvote <- Enum.sort_by(@image.upvotes, & &1.user.name) do + => link upvote.user.name, to: Routes.profile_path(@conn, :show, upvote.user), class: "interaction-user-list-item" + + h5 + ' Downvoted by + => @image.downvotes_count + = pluralize("user", "users", @image.downvotes_count) + + = for downvote <- Enum.sort_by(@image.downvotes, & &1.user.name) do + => link downvote.user.name, to: Routes.profile_path(@conn, :show, downvote.user), class: "interaction-user-list-item" diff --git a/lib/philomena_web/templates/image/favorites/index.html.slime b/lib/philomena_web/templates/image/favorites/index.html.slime deleted file mode 100644 index 3bdd899f..00000000 --- a/lib/philomena_web/templates/image/favorites/index.html.slime +++ /dev/null @@ -1,7 +0,0 @@ -h5 - ' Faved by - => @image.faves_count - = pluralize("user", "users", @image.faves_count) - -= for fave <- Enum.sort_by(@image.faves, & &1.user.name) do - => link fave.user.name, to: Routes.profile_path(@conn, :show, fave.user), class: "interaction-user-list-item" \ No newline at end of file diff --git a/lib/philomena_web/views/image/favorite_view.ex b/lib/philomena_web/views/image/favorite_view.ex new file mode 100644 index 00000000..c61cfea2 --- /dev/null +++ b/lib/philomena_web/views/image/favorite_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Image.FavoriteView do + use PhilomenaWeb, :view +end diff --git a/lib/philomena_web/views/image/favorites_view.ex b/lib/philomena_web/views/image/favorites_view.ex deleted file mode 100644 index d5311618..00000000 --- a/lib/philomena_web/views/image/favorites_view.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule PhilomenaWeb.Image.FavoritesView do - use PhilomenaWeb, :view -end