Class: ActionText::Attachment

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

Constant Summary collapse

ATTRIBUTES =
%w( sgid content-type url href filename filesize width height previewable presentation caption content )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionText::Attachments::TrixConversion

#to_trix_attachment

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Methods included from ActionText::Attachments::Caching

#cache_key

Constructor Details

#initialize(node, attachable) ⇒ Attachment

Returns a new instance of Attachment.



55
56
57
58
# File 'actiontext/lib/action_text/attachment.rb', line 55

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

Instance Attribute Details

#attachableObject (readonly)

Returns the value of attribute attachable



50
51
52
# File 'actiontext/lib/action_text/attachment.rb', line 50

def attachable
  @attachable
end

#nodeObject (readonly)

Returns the value of attribute node



50
51
52
# File 'actiontext/lib/action_text/attachment.rb', line 50

def node
  @node
end

Class Method Details

.fragment_by_canonicalizing_attachments(content) ⇒ Object



14
15
16
# File 'actiontext/lib/action_text/attachment.rb', line 14

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

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



26
27
28
29
30
# File 'actiontext/lib/action_text/attachment.rb', line 26

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



22
23
24
# File 'actiontext/lib/action_text/attachment.rb', line 22

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

.from_attributes(attributes, attachable = nil) ⇒ Object



32
33
34
35
36
# File 'actiontext/lib/action_text/attachment.rb', line 32

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

.from_node(node, attachable = nil) ⇒ Object



18
19
20
# File 'actiontext/lib/action_text/attachment.rb', line 18

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

Instance Method Details

#captionObject



60
61
62
# File 'actiontext/lib/action_text/attachment.rb', line 60

def caption
  node_attributes["caption"].presence
end

#full_attributesObject



64
65
66
# File 'actiontext/lib/action_text/attachment.rb', line 64

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

#inspectObject



88
89
90
# File 'actiontext/lib/action_text/attachment.rb', line 88

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

#to_htmlObject



80
81
82
# File 'actiontext/lib/action_text/attachment.rb', line 80

def to_html
  HtmlConversion.node_to_html(node)
end

#to_plain_textObject



72
73
74
75
76
77
78
# File 'actiontext/lib/action_text/attachment.rb', line 72

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

#to_sObject



84
85
86
# File 'actiontext/lib/action_text/attachment.rb', line 84

def to_s
  to_html
end

#with_full_attributesObject



68
69
70
# File 'actiontext/lib/action_text/attachment.rb', line 68

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