Class: ActionText::Attachment
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | actiontext/lib/action_text/attachment.rb |
Overview
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
-
ATTRIBUTES =
# File 'actiontext/lib/action_text/attachment.rb', line 24%w( sgid content-type url href filename filesize width height previewable presentation caption content )
Class Attribute Summary
- .tag_name (also: #tag_name) rw
Class Method Summary
- .fragment_by_canonicalizing_attachments(content)
- .from_attachable(attachable, attributes = {})
- .from_attachables(attachables)
- .from_attributes(attributes, attachable = nil)
- .from_node(node, attachable = nil)
- .new(node, attachable) ⇒ Attachment constructor
- .node_from_attributes(attributes) private
- .process_attributes(attributes) private
Instance Attribute Summary
- #attachable readonly
- #node readonly
- #tag_name rw
Instance Method Summary
- #caption
- #full_attributes
- #inspect
-
#to_html
Converts the attachment to HTML.
- #to_param
-
#to_plain_text
Converts the attachment to plain text.
- #to_s
- #with_full_attributes
- #attachable_attributes private
- #node_attributes private
- #sgid_attributes private
Attachments::TrixConversion
- Included
Attachments::Caching
- Included
Constructor Details
.new(node, attachable) ⇒ Attachment
# File 'actiontext/lib/action_text/attachment.rb', line 68
def initialize(node, attachable) @node = node @attachable = attachable end
Class Attribute Details
.tag_name (rw) Also known as: #tag_name
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 22
mattr_accessor :tag_name, default: "action-text-attachment"
Class Method Details
.fragment_by_canonicalizing_attachments(content)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 27
def (content) ( (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
.node_from_attributes(attributes) (private)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 52
def node_from_attributes(attributes) if attributes = process_attributes(attributes).presence ActionText::HtmlConversion.create_element(tag_name, attributes) end end
.process_attributes(attributes) (private)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 58
def process_attributes(attributes) attributes.transform_keys { |key| key.to_s.underscore.dasherize }.slice(*ATTRIBUTES) end
Instance Attribute Details
#attachable (readonly)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 63
attr_reader :node, :attachable
#node (readonly)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 63
attr_reader :node, :attachable
#tag_name (rw)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 22
mattr_accessor :tag_name, default: "action-text-attachment"
Instance Method Details
#attachable_attributes (private)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 140
def attachable_attributes @attachable_attributes ||= (attachable.try(:to_rich_text_attributes) || {}).stringify_keys 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
#node_attributes (private)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 136
def node_attributes @node_attributes ||= ATTRIBUTES.to_h { |name| [ name.underscore, node[name] ] }.compact end
#sgid_attributes (private)
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 144
def sgid_attributes @sgid_attributes ||= node_attributes.slice("sgid").presence || attachable_attributes.slice("sgid") end
#to_html
Converts the attachment to HTML.
attachable = Person.create! name: "Javan"
= ActionText::Attachment.from_attachable(attachable)
.to_html # => "<action-text-attachment sgid=\"BAh7CEk...
# File 'actiontext/lib/action_text/attachment.rb', line 123
def to_html HtmlConversion.node_to_html(node) end
#to_param
[ GitHub ]# File 'actiontext/lib/action_text/attachment.rb', line 65
delegate :to_param, to: :attachable
#to_plain_text
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]"
# File 'actiontext/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
[ 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