Fix #3554: approving BUR with nil forum_post_id doesn't update forum.
Wrap `approve!` and `reject!` in transactions so that if there's an error in approving or rejecting a BUR, it leaves the BUR's status unchanged instead of updating the BUR but not updating the forum.
This commit is contained in:
@@ -90,10 +90,12 @@ class BulkUpdateRequest < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def approve!(approver)
|
def approve!(approver)
|
||||||
CurrentUser.scoped(approver) do
|
transaction do
|
||||||
AliasAndImplicationImporter.new(script, forum_topic_id, "1", true).process!
|
CurrentUser.scoped(approver) do
|
||||||
update(status: "approved", approver: CurrentUser.user, skip_secondary_validations: true)
|
AliasAndImplicationImporter.new(script, forum_topic_id, "1", true).process!
|
||||||
forum_updater.update("The #{bulk_update_request_link} (forum ##{forum_post.id}) has been approved by @#{approver.name}.", "APPROVED")
|
update(status: "approved", approver: CurrentUser.user, skip_secondary_validations: true)
|
||||||
|
forum_updater.update("The #{bulk_update_request_link} (forum ##{forum_post.id}) has been approved by @#{approver.name}.", "APPROVED")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue AliasAndImplicationImporter::Error => x
|
rescue AliasAndImplicationImporter::Error => x
|
||||||
@@ -118,8 +120,10 @@ class BulkUpdateRequest < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def reject!(rejector)
|
def reject!(rejector)
|
||||||
forum_updater.update("The #{bulk_update_request_link} (forum ##{forum_post.id}) has been rejected by @#{rejector.name}.", "REJECTED")
|
transaction do
|
||||||
update_attribute(:status, "rejected")
|
update(status: "rejected")
|
||||||
|
forum_updater.update("The #{bulk_update_request_link} (forum ##{forum_post.id}) has been rejected by @#{rejector.name}.", "REJECTED")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def bulk_update_request_link
|
def bulk_update_request_link
|
||||||
|
|||||||
@@ -112,15 +112,21 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
|
|||||||
@req = FactoryGirl.create(:bulk_update_request, :script => "create alias AAA -> BBB", :forum_topic_id => @topic.id, :forum_post_id => @post.id, :title => "[bulk] hoge")
|
@req = FactoryGirl.create(:bulk_update_request, :script => "create alias AAA -> BBB", :forum_topic_id => @topic.id, :forum_post_id => @post.id, :title => "[bulk] hoge")
|
||||||
end
|
end
|
||||||
|
|
||||||
should "handle errors gracefully" do
|
should "gracefully handle validation errors during approval" do
|
||||||
@req.stubs(:update).raises(AliasAndImplicationImporter::Error.new("blah"))
|
@req.stubs(:update).raises(AliasAndImplicationImporter::Error.new("blah"))
|
||||||
assert_difference("ForumPost.count", 1) do
|
assert_difference("ForumPost.count", 1) do
|
||||||
@req.approve!(@admin)
|
@req.approve!(@admin)
|
||||||
end
|
end
|
||||||
|
|
||||||
@topic.reload
|
assert_equal("pending", @req.reload.status)
|
||||||
@post.reload
|
assert_match(/\[FAILED\]/, @topic.reload.title)
|
||||||
assert_match(/\[FAILED\]/, @topic.title)
|
end
|
||||||
|
|
||||||
|
should "leave the BUR pending if there is an unexpected error during approval" do
|
||||||
|
@req.forum_updater.stubs(:update).raises(RuntimeError.new("blah"))
|
||||||
|
assert_raises(RuntimeError) { @req.approve!(@admin) }
|
||||||
|
|
||||||
|
assert_equal("pending", @req.reload.status)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "downcase the text" do
|
should "downcase the text" do
|
||||||
|
|||||||
Reference in New Issue
Block a user