It’s essentially adding to the information that Discourse serializes out when sending that model to the client. For example, you may see a request to this endpoint when loading up a topic page:
http://meta.discourse.org/t/2349082394.json
which returns some payload like this:
{
title: "Some topic title",
archetype: 'regular',
category_id: 7,
post_stream: { posts: [...] },
posts_count: 15,
(etc etc.. lots more info about the topic in question)
}
Which is the json representation of a Discourse topic.
If I want to know more about the topic because of additional ‘stuff’ I do in my plugin, I can put in a line like
add_to_serializer :topic, :flag_name do
object.flag_name # <- NB that 'object' here is the topic being serialized
end
(Also, by default, this method simply sends the specified message to ‘object’, meaning that
add_to_serializer :topic, :flag_name
is the same as
add_to_serializer :topic, :flag_name do
object.flag_name
end
)
Once I’ve added the method to the serializer, I can end up with JSON output which includes that additional info
{
title: "Some topic title",
archetype: 'regular',
category_id: 7,
post_stream: { posts: [...] },
posts_count: 15,
flag_name: "some flag name"
...
}
Also note that you could achieve the same thing by overwriting the TopicSerializer class (which is exactly what the code snippet you’ve provided is doing)
# plugin.rb
TopicSerializer.class_eval do
attributes :flag_name
def flag_name
object.flag_name
end
end