From d6b4e831906bb9ec3cdeab8aec30f70a26fbee03 Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Thu, 18 Mar 2021 14:20:27 -0500 Subject: [PATCH] When updating article scores, use new score for blackbox (#13009) Fixes #13008 Add a test case for blackbox stability This failed as expected on master and passed on this branch, when a reaction (which would change the article's calculated score during update) is added, the hotness should be stable, and not modified during the second call to `#update_score`. --- app/models/article.rb | 4 ++-- spec/models/article_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index ad638dfc3..0d71784d3 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -375,8 +375,8 @@ class Article < ApplicationRecord end def update_score - new_score = reactions.sum(:points) + Reaction.where(reactable_id: user_id, reactable_type: "User").sum(:points) - update_columns(score: new_score, + self.score = reactions.sum(:points) + Reaction.where(reactable_id: user_id, reactable_type: "User").sum(:points) + update_columns(score: score, comment_score: comments.sum(:score), hotness_score: BlackBox.article_hotness_score(self), spaminess_rating: BlackBox.calculate_spaminess(self)) diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index a34216ef5..6121405af 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -1008,4 +1008,13 @@ RSpec.describe Article, type: :model do expect(article.plain_html).not_to include("highlight__panel") end end + + describe "#update_score" do + it "stably sets the correct blackbox values" do + create(:reaction, reactable: article, points: 1) + + article.update_score + expect { article.update_score }.not_to change { article.reload.hotness_score } + end + end end