Action Text Attachment
Attachments
將可附加物件序列化為 HTML 或純文字。
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..."
方法
- C
- F
- I
- N
- T
- W
常數
ATTRIBUTES(屬性) | = | %w( sgid content-type url href filename filesize width height previewable presentation caption content ) |
屬性
[R] | attachable(可附加物件) | |
[R] | node(節點) |
類別公開方法
fragment_by_canonicalizing_attachments(content) 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 27 def fragment_by_canonicalizing_attachments(content) fragment_by_minifying_attachments(fragment_by_converting_trix_attachments(content)) end
from_attachable(attachable, attributes = {}) 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 39 def from_attachable(attachable, attributes = {}) if node = node_from_attributes(attachable.to_rich_text_attributes(attributes)) new(node, attachable) end end
from_attachables(attachables) 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 35 def from_attachables(attachables) Array(attachables).filter_map { |attachable| from_attachable(attachable) } end
from_attributes(attributes, attachable = nil) 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 45 def from_attributes(attributes, attachable = nil) if node = node_from_attributes(attributes) from_node(node, attachable) end end
from_node(node, attachable = nil) 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 31 def from_node(node, attachable = nil) new(node, attachable || ActionText::Attachable.from_node(node)) end
new(node, attachable) 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 68 def initialize(node, attachable) @node = node @attachable = attachable end
實例公開方法
caption() 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 73 def caption node_attributes["caption"].presence end
full_attributes() 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 77 def full_attributes node_attributes.merge(attachable_attributes).merge(sgid_attributes) end
inspect() 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 131 def inspect "#<#{self.class.name} attachable=#{attachable.inspect}>" end
to_html() 連結
將附件轉換為 HTML。
attachable = Person.create! name: "Javan"
attachment = ActionText::Attachment.from_attachable(attachable)
attachment.to_html # => "<action-text-attachment sgid=\"BAh7CEk...
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 123 def to_html HtmlConversion.node_to_html(node) end
to_plain_text() 連結
將附件轉換為純文字。
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg"
attachment = ActionText::Attachment.from_attachable(attachable)
attachment.to_plain_text # => "[racecar.jpg]"
設定時使用 caption
(標題)
attachment = ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom")
attachment.to_plain_text # => "[Vroom vroom]"
可以透過實作 attachable_plain_text_representation
方法來覆蓋呈現方式
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]"
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 110 def to_plain_text if respond_to?(:attachable_plain_text_representation) attachable_plain_text_representation(caption) else caption.to_s end end
to_s() 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 127 def to_s to_html end
with_full_attributes() 連結
原始碼:顯示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 81 def with_full_attributes self.class.from_attributes(full_attributes, attachable) end