FIX: Only include pending/agreed scores in the total score

This should prevent posts from being hidden if a previous flag was
rejected and a new one was added.
This commit is contained in:
Robin Ward 2019-06-26 11:20:59 -04:00
parent c63268467e
commit eedec7d79b
2 changed files with 20 additions and 4 deletions

View File

@ -465,29 +465,45 @@ class Reviewable < ActiveRecord::Base
protected
def recalculate_score
# Recalculate the pending score and return it
result = DB.query(<<~SQL, id: self.id, pending: ReviewableScore.statuses[:pending])
# pending/agreed scores count
sql = <<~SQL
UPDATE reviewables
SET score = COALESCE((
SELECT sum(score)
FROM reviewable_scores AS rs
WHERE rs.reviewable_id = :id
AND rs.status IN (:pending, :agreed)
), 0.0)
WHERE id = :id
RETURNING score
SQL
result = DB.query(
sql,
id: self.id,
pending: ReviewableScore.statuses[:pending],
agreed: ReviewableScore.statuses[:agreed]
)
# Update topic score
DB.query(<<~SQL, topic_id: topic_id, pending: Reviewable.statuses[:pending])
sql = <<~SQL
UPDATE topics
SET reviewable_score = COALESCE((
SELECT SUM(score)
FROM reviewables AS r
WHERE r.topic_id = :topic_id
AND r.status IN (:pending, :approved)
), 0.0)
WHERE id = :topic_id
SQL
DB.query(
sql,
topic_id: topic_id,
pending: Reviewable.statuses[:pending],
approved: Reviewable.statuses[:approved]
)
self.score = result[0].score
end

View File

@ -35,7 +35,7 @@ RSpec.describe ReviewableScore, type: :model do
expect(rs.reload).to be_disagreed
expect(rs.reviewed_by).to eq(moderator)
expect(rs.reviewed_at).to be_present
expect(reviewable.score).to eq(4.0)
expect(reviewable.score).to eq(0.0)
end
it "increases the score by the post action type's score bonus" do