From 49474d516e3fa41bb6da37f090e45fdd542afa45 Mon Sep 17 00:00:00 2001 From: Spencer Date: Fri, 24 Jan 2020 08:06:52 -0800 Subject: [PATCH] Allow comments from deleted articles to be viewed (#5199) [deploy] --- app/controllers/comments_controller.rb | 14 +++-- .../deleted_commentable_comment.html.erb | 63 +++++++++++++++++++ spec/requests/comments_spec.rb | 24 +++++++ 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 app/views/comments/deleted_commentable_comment.html.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 8b57b398c..39e56e1f9 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,21 +16,23 @@ class CommentsController < ApplicationController if @podcast @user = @podcast - (@commentable = @user.podcast_episodes.find_by(slug: params[:slug])) || not_found + @commentable = @user.podcast_episodes.find_by(slug: params[:slug]) if @user.podcast_episodes else @user = User.find_by(username: params[:username]) || Organization.find_by(slug: params[:username]) || not_found @commentable = @root_comment&.commentable || - @user.articles.find_by(slug: params[:slug]) || - not_found + @user.articles.find_by(slug: params[:slug]) || nil @article = @commentable - not_found unless @commentable.published + + not_found if @commentable && !@commentable.published end - @commentable_type = @commentable.class.name + @commentable_type = @commentable.class.name if @commentable - set_surrogate_key_header "comments-for-#{@commentable.id}-#{@commentable_type}" + set_surrogate_key_header "comments-for-#{@commentable.id}-#{@commentable_type}" if @commentable + + render :deleted_commentable_comment unless @commentable end # GET /comments/1 diff --git a/app/views/comments/deleted_commentable_comment.html.erb b/app/views/comments/deleted_commentable_comment.html.erb new file mode 100644 index 000000000..1d78e9194 --- /dev/null +++ b/app/views/comments/deleted_commentable_comment.html.erb @@ -0,0 +1,63 @@ +
+
+

Comment from a deleted article or podcast

+
+ <% @user ||= @root_comment.user %> + <% if @root_comment.deleted %> +
+
+ [deleted] +
+
+ <% else %> +
+ <% decorated_comment = @root_comment.decorate %> + + <% if decorated_comment.low_quality %> +
+ <%= image_tag(cl_image_path(asset_path("/assets/sloan.png"), + type: "fetch", + width: 50, + height: 50, + crop: "imagga_scale", + quality: "auto", + flags: "progressive", + fetch_format: "auto", + sign_url: true), class: "sloan", alt: "Sloan, the sloth mascot", loading: "lazy") %> + Comment marked as low quality/non-constructive by the community + View code of conduct +
+ <% end %> + +
"> + + <%= @user.username %> profile image + + + <%= @user.name %> + + + + <% if @user.twitter_username.present? %> + + <%= image_tag("twitter-logo.svg", class: "icon-img", alt: "twitter logo") %> + <% end %> + <% if @user.github_username.present? %> + + <%= image_tag("github-logo.svg", class: "icon-img", alt: "github logo") %> + + <% end %> + <%= render "comments/comment_date", decorated_comment: decorated_comment %> +
+
"> + <%= sanitize @root_comment.processed_html %> + +
+
+
+ <% end %> +
+
diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb index d2abcfcb3..7f7afecd1 100644 --- a/spec/requests/comments_spec.rb +++ b/spec/requests/comments_spec.rb @@ -171,6 +171,30 @@ RSpec.describe "Comments", type: :request do expect { get comment.path }.to raise_error("Not Found") end end + + context "when the article is deleted" do + before do + comment + article.destroy + get comment.path + end + + it "renders deleted article comment view" do + expect(response.body).to include("Comment from a deleted article or podcast") + end + end + + context "when the podcast is deleted" do + before do + podcast_comment + podcast_episode.destroy + get podcast_comment.path + end + + it "renders deleted article comment view" do + expect(response.body).to include("Comment from a deleted article or podcast") + end + end end describe "GET /:username/:slug/comments/:id_code/edit" do