mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Introduce plugin modifiers for post.cooked and topic.fancy_titles (#31261)
Related:
40fd82e2d1
This PR introduces three new plugin modifiers attached to
- `basic_post_serializer.cooked`
- `basic_topic_serializer.fancy_title`
- `topic_view_serializer.fancy_title`
Implementation note: I had wanted to add them in the `Post` and `Topic`
models themselves, but they do not directly provide access to the
request's scope which is needed for the use case.
This commit is contained in:
parent
65d7ea2dbc
commit
5f00ae2ca9
@ -34,7 +34,9 @@ class BasicPostSerializer < ApplicationSerializer
|
||||
I18n.t("flagging.user_must_edit")
|
||||
end
|
||||
else
|
||||
object.filter_quotes(@parent_post)
|
||||
cooked = object.filter_quotes(@parent_post)
|
||||
modified = DiscoursePluginRegistry.apply_modifier(:basic_post_serializer_cooked, cooked, self)
|
||||
modified || cooked
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -3,4 +3,10 @@
|
||||
# The most basic attributes of a topic that we need to create a link for it.
|
||||
class BasicTopicSerializer < ApplicationSerializer
|
||||
attributes :id, :title, :fancy_title, :slug, :posts_count
|
||||
|
||||
def fancy_title
|
||||
f = object.fancy_title
|
||||
modified = DiscoursePluginRegistry.apply_modifier(:topic_serializer_fancy_title, f, self)
|
||||
modified || f
|
||||
end
|
||||
end
|
||||
|
@ -317,4 +317,10 @@ class TopicViewSerializer < ApplicationSerializer
|
||||
def include_visibility_reason_id?
|
||||
object.topic.visibility_reason_id.present?
|
||||
end
|
||||
|
||||
def fancy_title
|
||||
f = object.topic.fancy_title
|
||||
modified = DiscoursePluginRegistry.apply_modifier(:topic_view_serializer_fancy_title, f, self)
|
||||
modified || f
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@
|
||||
RSpec.describe BasicPostSerializer do
|
||||
describe "#name" do
|
||||
let(:user) { Fabricate.build(:user) }
|
||||
let(:post) { Fabricate.build(:post, user: user) }
|
||||
let(:post) { Fabricate.build(:post, user: user, cooked: "Hur dur I am a cooked raw") }
|
||||
let(:serializer) { BasicPostSerializer.new(post, scope: Guardian.new, root: false) }
|
||||
let(:json) { serializer.as_json }
|
||||
|
||||
@ -16,5 +16,22 @@ RSpec.describe BasicPostSerializer do
|
||||
SiteSetting.enable_names = false
|
||||
expect(json[:name]).to be_blank
|
||||
end
|
||||
|
||||
describe "#cooked" do
|
||||
it "returns the post's cooked" do
|
||||
expect(json[:cooked]).to eq(post.cooked)
|
||||
end
|
||||
|
||||
it "returns the modified cooked when register modified" do
|
||||
plugin = Plugin::Instance.new
|
||||
modifier = :basic_post_serializer_cooked
|
||||
proc = Proc.new { "X" }
|
||||
DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)
|
||||
|
||||
expect(json[:cooked]).to eq("X")
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
25
spec/serializers/basic_topic_serializer_spec.rb
Normal file
25
spec/serializers/basic_topic_serializer_spec.rb
Normal file
@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
describe BasicTopicSerializer do
|
||||
fab!(:topic) { Fabricate(:topic, title: "Hur dur this is a title") }
|
||||
|
||||
describe "#fancy_title" do
|
||||
it "returns the fancy title" do
|
||||
json = BasicTopicSerializer.new(topic).as_json
|
||||
|
||||
expect(json[:basic_topic][:fancy_title]).to eq(topic.title)
|
||||
end
|
||||
|
||||
it "returns the fancy title with a modifier" do
|
||||
plugin = Plugin::Instance.new
|
||||
modifier = :topic_serializer_fancy_title
|
||||
proc = Proc.new { "X" }
|
||||
DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)
|
||||
json = BasicTopicSerializer.new(topic).as_json
|
||||
|
||||
expect(json[:basic_topic][:fancy_title]).to eq("X")
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)
|
||||
end
|
||||
end
|
||||
end
|
@ -645,4 +645,25 @@ RSpec.describe TopicViewSerializer do
|
||||
expect(json[:topic_timer][:id]).to eq(topic_timer.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#fancy_title" do
|
||||
it "returns the fancy title" do
|
||||
topic.update!(title: "Hur dur this is a title")
|
||||
json = serialize_topic(topic, user)
|
||||
|
||||
expect(json[:fancy_title]).to eq("Hur dur this is a title")
|
||||
end
|
||||
|
||||
it "returns the fancy title with a modifier" do
|
||||
plugin = Plugin::Instance.new
|
||||
modifier = :topic_view_serializer_fancy_title
|
||||
proc = Proc.new { "X" }
|
||||
DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)
|
||||
json = serialize_topic(topic, user)
|
||||
|
||||
expect(json[:fancy_title]).to eq("X")
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user