From 1aadd1966306c8ac577ab7f412c42309ba48d844 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 21 Jan 2020 19:42:57 -0600 Subject: [PATCH] BURs: fix broken [bur:] links in forum posts. Fix bulk update requests generating invalid [bur:] links in forum posts. The id was missing because the BUR created the forum topic in a before_create hook, which created the post before the BUR was saved so the BUR didn't have an id yet. Fix regression caused by b4ce2d83. --- app/models/bulk_update_request.rb | 3 ++- test/unit/bulk_update_request_test.rb | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index a42a951f6..fea7992bf 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -15,7 +15,7 @@ class BulkUpdateRequest < ApplicationRecord validate :validate_script, :on => :create before_validation :initialize_attributes, :on => :create before_validation :normalize_text - before_create :create_forum_topic + after_create :create_forum_topic after_save :update_notice scope :pending_first, -> { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) } @@ -97,6 +97,7 @@ class BulkUpdateRequest < ApplicationRecord CurrentUser.as(user) do self.forum_topic = ForumTopic.create(title: title, category_id: 1, creator: user) unless forum_topic.present? self.forum_post = forum_topic.posts.create(body: reason_with_link, creator: user) unless forum_post.present? + save end end diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index 9be6cd2da..ddde19c8a 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -104,9 +104,11 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase end should "create a forum topic" do - assert_difference("ForumTopic.count", 1) do - BulkUpdateRequest.create(:title => "abc", :reason => "zzz", :script => "create alias aaa -> bbb", :skip_secondary_validations => true) - end + bur = create(:bulk_update_request, reason: "zzz", script: "create alias aaa -> bbb") + + assert_equal(true, bur.forum_post.present?) + assert_match(/\[bur:#{bur.id}\]/, bur.forum_post.body) + assert_match(/zzz/, bur.forum_post.body) end context "that has an invalid alias" do