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

View File

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