Class: ActionText::Attachment
- Inherits:
-
Object
- Object
- ActionText::Attachment
- Includes:
- ActionText::Attachments::Caching, ActionText::Attachments::Minification, ActionText::Attachments::TrixConversion
- Defined in:
- lib/action_text/attachment.rb
Overview
# Action Text Attachment
Attachments serialize attachables to HTML or plain text.
class Person < ApplicationRecord
include ActionText::Attachable
end
attachable = Person.create! name: "Javan"
= ActionText::Attachment.from_attachable(attachable)
.to_html # => "<action-text-attachment sgid=\"BAh7CEk..."
Constant Summary collapse
- ATTRIBUTES =
%w( sgid content-type url href filename filesize width height previewable presentation caption content )
Instance Attribute Summary collapse
-
#attachable ⇒ Object
readonly
Returns the value of attribute attachable.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Class Method Summary collapse
- .fragment_by_canonicalizing_attachments(content) ⇒ Object
- .from_attachable(attachable, attributes = {}) ⇒ Object
- .from_attachables(attachables) ⇒ Object
- .from_attributes(attributes, attachable = nil) ⇒ Object
- .from_node(node, attachable = nil) ⇒ Object
Instance Method Summary collapse
- #caption ⇒ Object
- #full_attributes ⇒ Object
-
#initialize(node, attachable) ⇒ Attachment
constructor
A new instance of Attachment.
- #inspect ⇒ Object
-
#to_html ⇒ Object
Converts the attachment to HTML.
-
#to_plain_text ⇒ Object
Converts the attachment to plain text.
- #to_s ⇒ Object
- #with_full_attributes ⇒ Object
Methods included from ActionText::Attachments::TrixConversion
Methods included from ActionText::Attachments::Caching
Constructor Details
#initialize(node, attachable) ⇒ Attachment
Returns a new instance of Attachment.
68 69 70 71 |
# File 'lib/action_text/attachment.rb', line 68 def initialize(node, attachable) @node = node @attachable = attachable end |
Instance Attribute Details
#attachable ⇒ Object (readonly)
Returns the value of attribute attachable.
63 64 65 |
# File 'lib/action_text/attachment.rb', line 63 def attachable @attachable end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
63 64 65 |
# File 'lib/action_text/attachment.rb', line 63 def node @node end |
Class Method Details
.fragment_by_canonicalizing_attachments(content) ⇒ Object
27 28 29 |
# File 'lib/action_text/attachment.rb', line 27 def (content) ((content)) end |
.from_attachable(attachable, attributes = {}) ⇒ Object
39 40 41 42 43 |
# File '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) ⇒ Object
35 36 37 |
# File '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) ⇒ Object
45 46 47 48 49 |
# File '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) ⇒ Object
31 32 33 |
# File 'lib/action_text/attachment.rb', line 31 def from_node(node, attachable = nil) new(node, attachable || ActionText::Attachable.from_node(node)) end |
Instance Method Details
#caption ⇒ Object
73 74 75 |
# File 'lib/action_text/attachment.rb', line 73 def node_attributes["caption"].presence end |
#full_attributes ⇒ Object
77 78 79 |
# File 'lib/action_text/attachment.rb', line 77 def full_attributes node_attributes.merge(attachable_attributes).merge(sgid_attributes) end |
#inspect ⇒ Object
131 132 133 |
# File 'lib/action_text/attachment.rb', line 131 def inspect "#<#{self.class.name} attachable=#{attachable.inspect}>" end |
#to_html ⇒ Object
Converts the attachment to HTML.
attachable = Person.create! name: "Javan"
= ActionText::Attachment.from_attachable(attachable)
.to_html # => "<action-text-attachment sgid=\"BAh7CEk...
123 124 125 |
# File 'lib/action_text/attachment.rb', line 123 def to_html HtmlConversion.node_to_html(node) end |
#to_plain_text ⇒ Object
Converts the attachment to plain text.
attachable = ActiveStorage::Blob.find_by filename: "racecar.jpg"
= ActionText::Attachment.from_attachable(attachable)
.to_plain_text # => "[racecar.jpg]"
Use the ‘caption` when set:
= ActionText::Attachment.from_attachable(attachable, caption: "Vroom vroom")
.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"
= ActionText::Attachment.from_attachable(attachable)
.to_plain_text # => "[Javan]"
110 111 112 113 114 115 116 |
# File 'lib/action_text/attachment.rb', line 110 def to_plain_text if respond_to?(:attachable_plain_text_representation) attachable_plain_text_representation() else .to_s end end |
#to_s ⇒ Object
127 128 129 |
# File 'lib/action_text/attachment.rb', line 127 def to_s to_html end |
#with_full_attributes ⇒ Object
81 82 83 |
# File 'lib/action_text/attachment.rb', line 81 def with_full_attributes self.class.from_attributes(full_attributes, attachable) end |