Action Text 附件
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..."
常量
属性 | = | %w( sgid content-type url href filename filesize width height previewable presentation caption content ) |
属性
[R] | 可附加项 | |
[R] | 节点 |
类公共方法
通过规范化附件生成片段(内容) 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 25 def fragment_by_canonicalizing_attachments(content) fragment_by_minifying_attachments(fragment_by_converting_trix_attachments(content)) end
来自可附加项(可附加项,属性 = {}) 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 37 def from_attachable(attachable, attributes = {}) if node = node_from_attributes(attachable.to_rich_text_attributes(attributes)) new(node, attachable) end end
来自可附加项(可附加项) 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 33 def from_attachables(attachables) Array(attachables).filter_map { |attachable| from_attachable(attachable) } end
来自属性(属性,可附加项 = nil) 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 43 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 29 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 66 def initialize(node, attachable) @node = node @attachable = attachable end
实例公共方法
caption() 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 71 def caption node_attributes["caption"].presence end
full_attributes() 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 75 def full_attributes node_attributes.merge(attachable_attributes).merge(sgid_attributes) end
inspect() 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 129 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 121 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 108 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 125 def to_s to_html end
with_full_attributes() 链接
来源:显示 | 在 GitHub 上
# File actiontext/lib/action_text/attachment.rb, line 79 def with_full_attributes self.class.from_attributes(full_attributes, attachable) end