Skip to content

Commit

Permalink
Guard bust_comment against nil commentable (forem#2991)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhymes authored and benhalpern committed May 29, 2019
1 parent c510f15 commit a99c072
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
40 changes: 23 additions & 17 deletions app/labor/cache_buster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,30 @@ def bust(path)
end

def bust_comment(commentable, username)
bust("/") if Article.published.order("hotness_score DESC").limit(3).pluck(:id).include?(commentable.id)
if commentable.decorate.cached_tag_list_array.include?("discuss") &&
commentable.featured_number.to_i > 35.hours.ago.to_i
bust("/")
bust("/?i=i")
bust("?i=i")
end
bust("#{commentable.path}/comments/")
bust(commentable.path.to_s)
commentable.comments.includes(:user).find_each do |c|
bust(c.path)
bust(c.path + "?i=i")
if commentable
bust("/") if Article.published.order("hotness_score DESC").limit(3).pluck(:id).include?(commentable.id)
if commentable.decorate.cached_tag_list_array.include?("discuss") &&
commentable.featured_number.to_i > 35.hours.ago.to_i
bust("/")
bust("/?i=i")
bust("?i=i")
end
bust("#{commentable.path}/comments/")
bust(commentable.path.to_s)
commentable.comments.includes(:user).find_each do |c|
bust(c.path)
bust(c.path + "?i=i")
end
bust("#{commentable.path}/comments/*")
end
bust("#{commentable.path}/comments/*")
bust("/#{username}")
bust("/#{username}/comments")
bust("/#{username}/comments?i=i")
bust("/#{username}/comments/?i=i")

return unless username

paths = [
"/#{username}", "/#{username}/comments",
"/#{username}/comments?i=i", "/#{username}/comments/?i=i"
]
paths.each { |path| bust(path) }
end

def bust_article(article)
Expand Down
31 changes: 21 additions & 10 deletions spec/labor/cache_buster_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
require "rails_helper"

RSpec.describe CacheBuster do
let(:cache_buster) { described_class.new }
let(:user) { create(:user) }
let(:article) { create(:article, user_id: user.id) }
let(:comment) { create(:comment, user_id: user.id, commentable_id: article.id) }

it "busts comment" do
commentable = Article.find(comment.commentable_id)
username = User.find(comment.user_id).username
described_class.new.bust_comment(commentable, username)
end
describe "#bust_comment" do
it "busts comment" do
cache_buster.bust_comment(comment.commentable, user.username)
end

it "works if commentable is missing" do
cache_buster.bust_comment(nil, user.username)
end

it "busts article" do
described_class.new.bust_article(article)
it "works if username is missing" do
cache_buster.bust_comment(comment.commentable, "")
end
end

it "busts featured article" do
article.update(featured: true)
described_class.new.bust_article(article)
describe "#bust_article" do
it "busts article" do
cache_buster.bust_article(article)
end

it "busts featured article" do
article.update_columns(featured: true)
cache_buster.bust_article(article)
end
end
end

0 comments on commit a99c072

Please sign in to comment.