FIX: Don't increment version if the reviewable operation fails

FIX: Don’t increment version if the reviewable operation fails

In fact, roll back the entire operation in the database.

diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index a823657..e8c8f0d 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 e821b15..0a32a3f 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

GitHub sha: 643c75c4