class ActionText::Attachment
attachment.to_html # => “<action-text-attachment sgid="BAh7CEk…”
attachment = ActionText::Attachment.from_attachable(attachable)
attachable = Person.create! name: “Javan”
end
include ActionText::Attachable
class Person < ApplicationRecord
Attachments serialize attachables to HTML or plain text.
# Action Text Attachment
def attachable_attributes
def attachable_attributes @attachable_attributes ||= (attachable.try(:to_rich_text_attributes) || {}).stringify_keys end
def caption
def caption node_attributes["caption"].presence end
def fragment_by_canonicalizing_attachments(content)
def fragment_by_canonicalizing_attachments(content) fragment_by_minifying_attachments(fragment_by_converting_trix_attachments(content)) end
def from_attachable(attachable, attributes = {})
def from_attachable(attachable, attributes = {}) if node = node_from_attributes(attachable.to_rich_text_attributes(attributes)) new(node, attachable) end end
def from_attachables(attachables)
def from_attachables(attachables) Array(attachables).filter_map { |attachable| from_attachable(attachable) } end
def from_attributes(attributes, attachable = nil)
def from_attributes(attributes, attachable = nil) if node = node_from_attributes(attributes) from_node(node, attachable) end end
def from_node(node, attachable = nil)
def from_node(node, attachable = nil) new(node, attachable || ActionText::Attachable.from_node(node)) end
def full_attributes
def full_attributes node_attributes.merge(attachable_attributes).merge(sgid_attributes) end
def initialize(node, attachable)
def initialize(node, attachable) @node = node @attachable = attachable end
def inspect
def inspect "#<#{self.class.name} attachable=#{attachable.inspect}>" end
def node_attributes
def node_attributes @node_attributes ||= ATTRIBUTES.to_h { |name| [ name.underscore, node[name] ] }.compact end
def node_from_attributes(attributes)
def node_from_attributes(attributes) if attributes = process_attributes(attributes).presence ActionText::HtmlConversion.create_element(tag_name, attributes) end end
def process_attributes(attributes)
def process_attributes(attributes) attributes.transform_keys { |key| key.to_s.underscore.dasherize }.slice(*ATTRIBUTES) end
def sgid_attributes
def sgid_attributes @sgid_attributes ||= node_attributes.slice("sgid").presence || attachable_attributes.slice("sgid") end
def to_html
attachable = Person.create! name: "Javan"
Converts the attachment to HTML.
def to_html HtmlConversion.node_to_html(node) end
def to_plain_text
attachment = ActionText::Attachment.from_attachable(attachable)
attachable = Person.create! name: "Javan"
end
end
"[#{name}]"
def attachable_plain_text_representation
include ActionText::Attachable
class Person < ApplicationRecord
`attachable_plain_text_representation` method:
The presentation can be overridden by implementing the
attachment.to_plain_text # => "[Vroom vroom]"
attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom")
Use the `caption` when set:
attachment.to_plain_text # => "[racecar.jpg]"
attachment = ActionText::Attachment.from_attachable(attachable)
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg"
Converts the attachment to plain text.
def to_plain_text if respond_to?(:attachable_plain_text_representation) attachable_plain_text_representation(caption) else caption.to_s end end
def to_s
def to_s to_html end
def with_full_attributes
def with_full_attributes self.class.from_attributes(full_attributes, attachable) end