Class: ActionMosaico::TrixAttachment

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mosaico/mosaico_attachment.rb

Constant Summary collapse

TAG_NAME =
'figure'
SELECTOR =
'[data-mosaico-attachment]'
COMPOSED_ATTRIBUTES =
%w[caption presentation].freeze
ATTRIBUTES =
%w[sgid contentType url href filename filesize width height previewable
content] + COMPOSED_ATTRIBUTES
ATTRIBUTE_TYPES =
{
  'previewable' => ->(value) { value.to_s == 'true' },
  'filesize' => ->(value) { Integer(value.to_s, exception: false) || value },
  'width' => ->(value) { Integer(value.to_s, exception: false) },
  'height' => ->(value) { Integer(value.to_s, exception: false) },
  :default => ->(value) { value.to_s }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ TrixAttachment

Returns a new instance of TrixAttachment.



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

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



51
52
53
# File 'lib/action_mosaico/mosaico_attachment.rb', line 51

def node
  @node
end

Class Method Details

.from_attributes(attributes) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_mosaico/mosaico_attachment.rb', line 20

def from_attributes(attributes)
  attributes = process_attributes(attributes)

  mosaico_attachment_attributes = attributes.except(*COMPOSED_ATTRIBUTES)
  mosaico_attributes = attributes.slice(*COMPOSED_ATTRIBUTES)

  node = ActionMosaico::HtmlConversion.create_element(TAG_NAME)
  node['data-mosaico-attachment'] = JSON.generate(mosaico_attachment_attributes)
  node['data-mosaico-attributes'] = JSON.generate(mosaico_attributes) if mosaico_attributes.any?

  new(node)
end

Instance Method Details

#attributesObject



57
58
59
# File 'lib/action_mosaico/mosaico_attachment.rb', line 57

def attributes
  @attributes ||= attachment_attributes.merge(composed_attributes).slice(*ATTRIBUTES)
end

#to_htmlObject



61
62
63
# File 'lib/action_mosaico/mosaico_attachment.rb', line 61

def to_html
  ActionMosaico::HtmlConversion.node_to_html(node)
end

#to_sObject



65
66
67
# File 'lib/action_mosaico/mosaico_attachment.rb', line 65

def to_s
  to_html
end