Class: ActionMosaico::Attachment

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

Constant Summary collapse

ATTRIBUTES =
%w[sgid content-type url href filename filesize width height previewable presentation caption].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionMosaico::Attachments::TrixConversion

#to_mosaico_attachment

Methods included from ActionMosaico::Attachments::Caching

#cache_key

Constructor Details

#initialize(node, attachable) ⇒ Attachment

Returns a new instance of Attachment.



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

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

Instance Attribute Details

#attachableObject (readonly)

Returns the value of attribute attachable.



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

def attachable
  @attachable
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

Class Method Details

.fragment_by_canonicalizing_attachments(content) ⇒ Object



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

def fragment_by_canonicalizing_attachments(content)
  fragment_by_minifying_attachments(fragment_by_converting_mosaico_attachments(content))
end

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



28
29
30
31
32
# File 'lib/action_mosaico/attachment.rb', line 28

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



24
25
26
# File 'lib/action_mosaico/attachment.rb', line 24

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

.from_attributes(attributes, attachable = nil) ⇒ Object



34
35
36
37
38
# File 'lib/action_mosaico/attachment.rb', line 34

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

.from_node(node, attachable = nil) ⇒ Object



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

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

Instance Method Details

#captionObject



63
64
65
# File 'lib/action_mosaico/attachment.rb', line 63

def caption
  node_attributes['caption'].presence
end

#full_attributesObject



67
68
69
# File 'lib/action_mosaico/attachment.rb', line 67

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

#inspectObject



91
92
93
# File 'lib/action_mosaico/attachment.rb', line 91

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

#to_htmlObject



83
84
85
# File 'lib/action_mosaico/attachment.rb', line 83

def to_html
  HtmlConversion.node_to_html(node)
end

#to_plain_textObject



75
76
77
78
79
80
81
# File 'lib/action_mosaico/attachment.rb', line 75

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

#to_sObject



87
88
89
# File 'lib/action_mosaico/attachment.rb', line 87

def to_s
  to_html
end

#with_full_attributesObject



71
72
73
# File 'lib/action_mosaico/attachment.rb', line 71

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