DEV: `PostRevisor` helper methods to track topic title and raw revisions (#21918)

Not all revisions involve changes to the actual post/topic content. We
may want to know if a revisions includes the topic title or post raw.

Specifically introducing these for use in the Akismet plugin to
conditionally queue checks.
This commit is contained in:
Selase Krakani 2023-06-05 18:02:46 +00:00 committed by GitHub
parent 4313cd5298
commit f462347e12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -762,4 +762,17 @@ class PostRevisor
def guardian def guardian
@guardian ||= Guardian.new(@editor) @guardian ||= Guardian.new(@editor)
end end
def raw_changed?
@fields.has_key?(:raw) && @fields[:raw] != cached_original_raw && @post_successfully_saved
end
def topic_title_changed?
topic_changed? && @fields.has_key?(:title) && topic_diff.has_key?(:title) &&
!@topic_changes.errored?
end
def reviewable_content_changed?
raw_changed? || topic_title_changed?
end
end end

View File

@ -322,6 +322,7 @@ RSpec.describe PostRevisor do
subject.revise!(admin, raw: "new post body", tags: ["new-tag"]) subject.revise!(admin, raw: "new post body", tags: ["new-tag"])
expect(post.topic.reload.tags.map(&:name)).to contain_exactly("new-tag") expect(post.topic.reload.tags.map(&:name)).to contain_exactly("new-tag")
expect(post.post_revisions.reload.size).to eq(1) expect(post.post_revisions.reload.size).to eq(1)
expect(subject.raw_changed?).to eq(true)
subject.revise!(admin, raw: old_raw, tags: []) subject.revise!(admin, raw: old_raw, tags: [])
expect(post.topic.reload.tags.map(&:name)).to be_empty expect(post.topic.reload.tags.map(&:name)).to be_empty
@ -968,6 +969,8 @@ RSpec.describe PostRevisor do
post.reload post.reload
expect(post.topic.title).to eq(new_title) expect(post.topic.title).to eq(new_title)
expect(post.revisions.first.modifications["title"][1]).to eq(new_title) expect(post.revisions.first.modifications["title"][1]).to eq(new_title)
expect(subject.topic_title_changed?).to eq(true)
expect(subject.raw_changed?).to eq(false)
end end
it "revises and tracks changes of topic archetypes" do it "revises and tracks changes of topic archetypes" do
@ -983,17 +986,21 @@ RSpec.describe PostRevisor do
post.reload post.reload
expect(post.topic.archetype).to eq(new_archetype) expect(post.topic.archetype).to eq(new_archetype)
expect(post.revisions.first.modifications["archetype"][1]).to eq(new_archetype) expect(post.revisions.first.modifications["archetype"][1]).to eq(new_archetype)
expect(subject.raw_changed?).to eq(false)
end end
it "revises and tracks changes of topic tags" do it "revises and tracks changes of topic tags" do
subject.revise!(admin, tags: ["new-tag"]) subject.revise!(admin, tags: ["new-tag"])
expect(post.post_revisions.last.modifications).to eq("tags" => [[], ["new-tag"]]) expect(post.post_revisions.last.modifications).to eq("tags" => [[], ["new-tag"]])
expect(subject.raw_changed?).to eq(false)
subject.revise!(admin, tags: %w[new-tag new-tag-2]) subject.revise!(admin, tags: %w[new-tag new-tag-2])
expect(post.post_revisions.last.modifications).to eq("tags" => [[], %w[new-tag new-tag-2]]) expect(post.post_revisions.last.modifications).to eq("tags" => [[], %w[new-tag new-tag-2]])
expect(subject.raw_changed?).to eq(false)
subject.revise!(admin, tags: ["new-tag-3"]) subject.revise!(admin, tags: ["new-tag-3"])
expect(post.post_revisions.last.modifications).to eq("tags" => [[], ["new-tag-3"]]) expect(post.post_revisions.last.modifications).to eq("tags" => [[], ["new-tag-3"]])
expect(subject.raw_changed?).to eq(false)
end end
describe "#publish_changes" do describe "#publish_changes" do