class ActionMosaico::Attachment

Constants

ATTRIBUTES

Attributes

attachable[R]
node[R]

Public Class Methods

fragment_by_canonicalizing_attachments(content) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/action_mosaico/attachment.rb, line 20
def from_node(node, attachable = nil)
  new(node, attachable || ActionMosaico::Attachable.from_node(node))
end
new(node, attachable) click to toggle source
# File lib/action_mosaico/attachment.rb, line 58
def initialize(node, attachable)
  @node = node
  @attachable = attachable
end

Private Class Methods

node_from_attributes(attributes) click to toggle source
# File lib/action_mosaico/attachment.rb, line 42
def node_from_attributes(attributes)
  if attributes = process_attributes(attributes).presence
    ActionMosaico::HtmlConversion.create_element(tag_name, attributes)
  end
end
process_attributes(attributes) click to toggle source
# File lib/action_mosaico/attachment.rb, line 48
def process_attributes(attributes)
  attributes.transform_keys { |key| key.to_s.underscore.dasherize }.slice(*ATTRIBUTES)
end

Public Instance Methods

caption() click to toggle source
# File lib/action_mosaico/attachment.rb, line 63
def caption
  node_attributes['caption'].presence
end
full_attributes() click to toggle source
# File lib/action_mosaico/attachment.rb, line 67
def full_attributes
  node_attributes.merge(attachable_attributes).merge(sgid_attributes)
end
inspect() click to toggle source
# File lib/action_mosaico/attachment.rb, line 91
def inspect
  "#<#{self.class.name} attachable=#{attachable.inspect}>"
end
to_html() click to toggle source
# File lib/action_mosaico/attachment.rb, line 83
def to_html
  HtmlConversion.node_to_html(node)
end
to_plain_text() click to toggle source
# 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_s() click to toggle source
# File lib/action_mosaico/attachment.rb, line 87
def to_s
  to_html
end
with_full_attributes() click to toggle source
# File lib/action_mosaico/attachment.rb, line 71
def with_full_attributes
  self.class.from_attributes(full_attributes, attachable)
end

Private Instance Methods

attachable_attributes() click to toggle source
# File lib/action_mosaico/attachment.rb, line 101
def attachable_attributes
  @attachable_attributes ||= (attachable.try(:to_rich_text_attributes) || {}).stringify_keys
end
node_attributes() click to toggle source
# File lib/action_mosaico/attachment.rb, line 97
def node_attributes
  @node_attributes ||= ATTRIBUTES.map { |name| [name.underscore, node[name]] }.to_h.compact
end
sgid_attributes() click to toggle source
# File lib/action_mosaico/attachment.rb, line 105
def sgid_attributes
  @sgid_attributes ||= node_attributes.slice('sgid').presence || attachable_attributes.slice('sgid')
end