Class: ActionText::Attachment

Inherits:
Object
  • Object
show all
Includes:
ActionText::Attachments::Caching, ActionText::Attachments::Minification, ActionText::Attachments::TrixConversion
Defined in:
lib/action_text/attachment.rb

Constant Summary collapse

TAG_NAME =
"action-text-attachment"
SELECTOR =
TAG_NAME
ATTRIBUTES =
%w( sgid content-type url href filename filesize width height previewable presentation caption )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionText::Attachments::TrixConversion

#to_trix_attachment

Methods included from ActionText::Attachments::Caching

#cache_key

Constructor Details

#initialize(node, attachable) ⇒ Attachment

Returns a new instance of Attachment.



53
54
55
56
# File 'lib/action_text/attachment.rb', line 53

def initialize(node, attachable)
  @node = node
  @attachable = attachable
end

Instance Attribute Details

#attachableObject (readonly)

Returns the value of attribute attachable.



48
49
50
# File 'lib/action_text/attachment.rb', line 48

def attachable
  @attachable
end

#nodeObject (readonly)

Returns the value of attribute node.



48
49
50
# File 'lib/action_text/attachment.rb', line 48

def node
  @node
end

Class Method Details

.fragment_by_canonicalizing_attachments(content) ⇒ Object



12
13
14
# File 'lib/action_text/attachment.rb', line 12

def fragment_by_canonicalizing_attachments(content)
  fragment_by_minifying_attachments(fragment_by_converting_trix_attachments(content))
end

.from_attachable(attachable, attributes = {}) ⇒ Object



24
25
26
27
28
# File 'lib/action_text/attachment.rb', line 24

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



20
21
22
# File 'lib/action_text/attachment.rb', line 20

def from_attachables(attachables)
  Array(attachables).map { |attachable| from_attachable(attachable) }.compact
end

.from_attributes(attributes, attachable = nil) ⇒ Object



30
31
32
33
34
# File 'lib/action_text/attachment.rb', line 30

def from_attributes(attributes, attachable = nil)
  if node = node_from_attributes(attributes)
    from_node(node, attachable)
  end
end

.from_node(node, attachable = nil) ⇒ Object



16
17
18
# File 'lib/action_text/attachment.rb', line 16

def from_node(node, attachable = nil)
  new(node, attachable || ActionText::Attachable.from_node(node))
end

Instance Method Details

#captionObject



58
59
60
# File 'lib/action_text/attachment.rb', line 58

def caption
  node_attributes["caption"].presence
end

#full_attributesObject



62
63
64
# File 'lib/action_text/attachment.rb', line 62

def full_attributes
  node_attributes.merge(attachable_attributes).merge(sgid_attributes)
end

#inspectObject



86
87
88
# File 'lib/action_text/attachment.rb', line 86

def inspect
  "#<#{self.class.name} attachable=#{attachable.inspect}>"
end

#to_htmlObject



78
79
80
# File 'lib/action_text/attachment.rb', line 78

def to_html
  HtmlConversion.node_to_html(node)
end

#to_plain_textObject



70
71
72
73
74
75
76
# File 'lib/action_text/attachment.rb', line 70

def to_plain_text
  if respond_to?(:attachable_plain_text_representation)
    attachable_plain_text_representation(caption)
  else
    caption.to_s
  end
end

#to_sObject



82
83
84
# File 'lib/action_text/attachment.rb', line 82

def to_s
  to_html
end

#with_full_attributesObject



66
67
68
# File 'lib/action_text/attachment.rb', line 66

def with_full_attributes
  self.class.from_attributes(full_attributes, attachable)
end