From 14a13dc19245f94ba779895a5c5b71e6d6762ec8 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Wed, 7 Jul 2021 11:45:00 -0500 Subject: [PATCH] FIX: Check type of existing reviewables when new reviewable is created (#13662) --- app/models/reviewable.rb | 4 ++-- spec/models/reviewable_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb index 6876462a756..e8998a1d143 100644 --- a/app/models/reviewable.rb +++ b/app/models/reviewable.rb @@ -139,8 +139,8 @@ class Reviewable < ActiveRecord::Base ) reviewable.created_new! - if target.blank? - # If there is no target there's no chance of a conflict + if target.blank? || !Reviewable.where(target: target, type: reviewable.type).exists? + # If there is no target, or no existing reviewable with matching target and type, there's no chance of a conflict reviewable.save! else # In this case, a reviewable might already exist for this (type, target_id) index. diff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rb index 9b9f32bf5d3..fb1cad30aca 100644 --- a/spec/models/reviewable_spec.rb +++ b/spec/models/reviewable_spec.rb @@ -78,6 +78,14 @@ RSpec.describe Reviewable, type: :model do expect(r1.pending?).to eq(true) expect(r0.pending?).to eq(false) end + + it "will create a new reviewable when an existing reviewable exists the same target with different type" do + r0 = Fabricate(:reviewable_queued_post) + r0.perform(admin, :approve_post) + + r1 = ReviewableFlaggedPost.needs_review!(created_by: admin, target: r0.target) + expect(r1.pending?).to eq(true) + end end context ".list_for" do