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

In fact, roll back the entire operation in the database.
This commit is contained in:
Robin Ward 2019-05-10 09:53:25 -04:00
parent bf7594b327
commit 643c75c419
2 changed files with 8 additions and 6 deletions

View File

@ -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

View File

@ -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