Action Text Attachment¶ ↑
Attachments serialize attachables to HTML or plain text.
class Person < ApplicationRecord include ActionText::Attachable end attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk..."
Methods
- C
- F
- N
- T
- W
Constants
| ATTRIBUTES | = | %w( sgid content-type url href filename filesize width height previewable presentation caption content ) |
Attributes
| [R] | attachable | |
| [R] | node |
Class Public methods
fragment_by_canonicalizing_attachments(content) Link
from_attachable(attachable, attributes = {}) Link
from_attachables(attachables) Link
from_attributes(attributes, attachable = nil) Link
from_node(node, attachable = nil) Link
new(node, attachable) Link
Instance Public methods
caption() Link
full_attributes() Link
to_html() Link
Converts the attachment to HTML.
attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk...
to_markdown(attachment_links: false) Link
Converts the attachment to Markdown.
By default, ActiveStorage blob attachments render as bracketed text:
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_markdown # => "[racecar.jpg]"
Use the caption when set:
attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom") attachment.to_markdown # => "[Vroom vroom]"
When attachment_links is true and a rendering context is available (e.g., controller or mailer action), ActiveStorage blob attachments generate Markdown links with URLs.
# Image blob attachment.to_markdown(attachment_links: true) # => "" # Non-image blob attachment.to_markdown(attachment_links: true) # => "[report.pdf](http://example.com/rails/active_storage/blobs/...)"
Remote images always render as Markdown links regardless of attachment_links:
content = ActionText::Content.new('<action-text-attachment content-type="image/jpeg" url="https://example.com/photo.jpg" caption="A photo"></action-text-attachment>') content.to_markdown # => ""
The presentation can be overridden by implementing the attachable_markdown_representation method:
class Person < ApplicationRecord include ActionText::Attachable def attachable_markdown_representation(caption, attachment_links: false) MarkdownConversion.markdown_link("@#{name}", Rails.application.routes.url_helpers.person_url(self)) end end attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_markdown # => "[@Javan](http://example.com/people/1)"
to_plain_text() Link
Converts the attachment to plain text.
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_plain_text # => "[racecar.jpg]"
Use the caption when set:
attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom") attachment.to_plain_text # => "[Vroom vroom]"
The presentation can be overridden by implementing the attachable_plain_text_representation method:
class Person < ApplicationRecord include ActionText::Attachable def attachable_plain_text_representation "[#{name}]" end end attachable = Person.create! name: "Javan" attachment = ActionText::Attachment.from_attachable(attachable) attachment.to_plain_text # => "[Javan]"