A platform for community discussion. Free, open, simple.
FIX: Make PostRevisor more consistent (#14841)
Dan Ungureanuec3758b5736b
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rbindex 42b70d00380..f3b8aa7333e 100644--- a/lib/post_revisor.rb+++ b/lib/post_revisor.rb@@ -531,9 +531,9 @@ class PostRevisormodifications.each_key do |field|if revision.modifications.has_key?(field)- old_value = revision.modifications[field][0].to_s- new_value = modifications[field][1].to_s- if old_value != new_value+ old_value = revision.modifications[field][0]+ new_value = modifications[field][1]+ if old_value.to_s != new_value.to_srevision.modifications[field] = [old_value, new_value]elserevision.modifications.delete(field)@@ -545,6 +545,7 @@ class PostRevisor# should probably do this before saving the post!if revision.modifications.empty?revision.destroy+ @post.last_editor_id = PostRevision.where(post_id: @post.id).order(number: :desc).pluck_first(:user_id) || @post.user_id@post.version -= 1@post.public_version -= 1@post.savediff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rbindex 207f2177830..d0c318a2c11 100644--- a/spec/components/post_revisor_spec.rb+++ b/spec/components/post_revisor_spec.rb@@ -148,6 +148,22 @@ describe PostRevisor dosubject { PostRevisor.new(post) }+ it 'destroys last revision if edit is undone' do+ old_raw = post.raw++ subject.revise!(admin, raw: 'new post body', tags: ['new-tag'])+ expect(post.topic.reload.tags.map(&:name)).to contain_exactly('new-tag')+ expect(post.post_revisions.reload.size).to eq(1)++ subject.revise!(admin, raw: old_raw, tags: [])+ expect(post.topic.reload.tags.map(&:name)).to be_empty+ expect(post.post_revisions.reload.size).to eq(0)++ subject.revise!(admin, raw: 'next post body', tags: ['new-tag'])+ expect(post.topic.reload.tags.map(&:name)).to contain_exactly('new-tag')+ expect(post.post_revisions.reload.size).to eq(1)+ end+describe 'with the same body' doit "doesn't change version" doexpect {@@ -703,6 +719,17 @@ describe PostRevisor doexpect(post.revisions.first.modifications["archetype"][1]).to eq(new_archetype)end+ it "revises and tracks changes of topic tags" do+ subject.revise!(admin, tags: ['new-tag'])+ expect(post.post_revisions.last.modifications).to eq('tags' => [[], ['new-tag']])++ subject.revise!(admin, tags: ['new-tag', 'new-tag-2'])+ expect(post.post_revisions.last.modifications).to eq('tags' => [[], ['new-tag', 'new-tag-2']])++ subject.revise!(admin, tags: ['new-tag-3'])+ expect(post.post_revisions.last.modifications).to eq('tags' => [[], ['new-tag-3']])+ end+context "#publish_changes" dolet!(:post) { Fabricate(:post, topic: topic) }