FIX: reset `edit_reason` in posts when creating a new version.

FIX: reset edit_reason in posts when creating a new version.

Previously when not updating it the “edit reason” message was stuck on the client-side.

diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 3dd7459b50..c5c61679f6 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -359,7 +359,7 @@ class PostRevisor
     end
 
     POST_TRACKED_FIELDS.each do |field|
-      @post.public_send("#{field}=", @fields[field]) if @fields.has_key?(field)
+      @post.public_send("#{field}=", @fields[field]) if @fields.has_key?(field) || (should_create_new_version? && field == "edit_reason")
     end
 
     @post.last_editor_id = @editor.id
diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb
index 228b5fbe0b..7d4f4fc74a 100644
--- a/spec/components/post_revisor_spec.rb
+++ b/spec/components/post_revisor_spec.rb
@@ -208,6 +208,19 @@ describe PostRevisor do
         expect(post.revisions.count).to eq(1)
       end
 
+      it "resets the edit_reason attribute in post model" do
+        SiteSetting.editing_grace_period = 5
+        post = Fabricate(:post, raw: 'hello world')
+        revisor = PostRevisor.new(post)
+        revisor.revise!(post.user, { raw: 'hello world123456789', edit_reason: 'this is my reason' }, revised_at: post.updated_at + 1.second)
+        post.reload
+        expect(post.edit_reason).to eq('this is my reason')
+
+        revisor.revise!(post.user, { raw: 'hello world4321' }, revised_at: post.updated_at + 6.second)
+        post.reload
+        expect(post.edit_reason).not_to be_present
+      end
+
       it "does not create a new version if an edit reason is provided and its the same as the current edit reason" do
         post = Fabricate(:post, raw: 'hello world', edit_reason: 'this is my reason')
         revisor = PostRevisor.new(post)

GitHub sha: a0e0b1ef

This line is super long now. I think it would be better if we moved the if before the line to make it easier to read.

2 Likes

The follow-up commit which I added not yet showing up here in review.discourse.

2 Likes

https://review.discourse.org/t/dev-improve-the-code-readability/8960