Hi everyone,
I’m learning how to extend Discourse serializers, and I ran into a behavior that I don’t fully understand.
I added this code to override PostSerializer#raw:
require_dependency "post_serializer"
class ::PostSerializer
def raw
if scope.can_edit?(object)
object.raw
else
object.raw&.truncate(300)
end
end
end
This works perfectly.
However, if I change it to use super:
require_dependency "post_serializer"
class ::PostSerializer
def raw
if scope.can_edit?(object)
super
else
object.raw&.truncate(300)
end
end
end
When scope.can_edit?(object) is true, the call to super causes /posts/<id>.json to return a 500 error.
I know I can avoid the problem by using object.raw instead of super, but I want to understand why super causes an exception here, especially because I also override other methods (cooked, post_stream, etc.) and super works fine in those cases.
Since I’m still new to Discourse internals, I would really appreciate an explanation of:
- what
superactually resolves to insidePostSerializer#raw, - why calling
superin this case leads to a 500 error, - and why
rawbehaves differently compared to methods likecooked.
Thanks in advance for any help!