ActionText supports custom attachments using Signed Global IDs. We can use this to embed any database record in Rails such as @mentions for users.
For HTML, Rails will use the to_partial_path
to render the HTML template for the attachment. For a user, this would be app/views/users/_user.html.erb
.
With Plain Text output, Rails will default to the caption specified on the attachable. Typically, this is nil because we don't need a caption for an @mention.
By defining the attachable_plain_text_representation
method, we can specify the default value for the plain text ActionText output.
class User
include ActionText::Attachable
# When ActionText rendering mentions in plain text
def attachable_plain_text_representation(caption = nil)
caption || name
end
And that's it! Simply render your ActionText content as plain text and it will use this method for rendering.