From 643c75c4199917c3e03bd3273f43bd761814b9e5 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 10 May 2019 09:53:25 -0400 Subject: [PATCH] FIX: Don't increment version if the reviewable operation fails In fact, roll back the entire operation in the database. --- app/models/reviewable.rb | 10 +++++----- spec/requests/reviewables_controller_spec.rb | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb index a823657710d..e8c8f0d97c2 100644 --- a/app/models/reviewable.rb +++ b/app/models/reviewable.rb @@ -249,12 +249,12 @@ class Reviewable < ActiveRecord::Base increment_version!(args[:version]) result = public_send(perform_method, performed_by, args) - if result.success? - update_count = transition_to(result.transition_to, performed_by) if result.transition_to - update_flag_stats(**result.update_flag_stats) if result.update_flag_stats + raise ActiveRecord::Rollback unless result.success? - recalculate_score if result.recalculate_score - end + update_count = transition_to(result.transition_to, performed_by) if result.transition_to + update_flag_stats(**result.update_flag_stats) if result.update_flag_stats + + recalculate_score if result.recalculate_score end if result && result.after_commit result.after_commit.call diff --git a/spec/requests/reviewables_controller_spec.rb b/spec/requests/reviewables_controller_spec.rb index e821b15648b..0a32a3f1283 100644 --- a/spec/requests/reviewables_controller_spec.rb +++ b/spec/requests/reviewables_controller_spec.rb @@ -260,10 +260,12 @@ describe ReviewablesController do it "can properly return errors" do qp = Fabricate(:reviewable_queued_post_topic, topic_id: -100) - put "/review/#{qp.id}/perform/approve_post.json?version=#{qp.version}" + version = qp.version + put "/review/#{qp.id}/perform/approve_post.json?version=#{version}" expect(response.code).to eq("422") result = ::JSON.parse(response.body) expect(result['errors']).to be_present + expect(qp.reload.version).to eq(version) end it "requires a version parameter" do