123456789_123456789_123456789_123456789_123456789_

Class: ActionText::TrixAttachment

Relationships & Source Files
Inherits: Object
Defined in: actiontext/lib/action_text/trix_attachment.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(node) ⇒ TrixAttachment

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 53

def initialize(node)
  @node = node
end

Class Method Details

.from_attributes(attributes)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 21

def from_attributes(attributes)
  attributes = process_attributes(attributes)

  trix_attachment_attributes = attributes.except(*COMPOSED_ATTRIBUTES)
  trix_attributes = attributes.slice(*COMPOSED_ATTRIBUTES)

  node = ActionText::HtmlConversion.create_element(TAG_NAME)
  node["data-trix-attachment"] = JSON.generate(trix_attachment_attributes)
  node["data-trix-attributes"] = JSON.generate(trix_attributes) if trix_attributes.any?

  new(node)
end

.process_attributes(attributes) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 35

def process_attributes(attributes)
  typecast_attribute_values(transform_attribute_keys(attributes))
end

.transform_attribute_keys(attributes) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 39

def transform_attribute_keys(attributes)
  attributes.transform_keys { |key| key.to_s.underscore.camelize(:lower) }
end

.typecast_attribute_values(attributes) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 43

def typecast_attribute_values(attributes)
  attributes.to_h do |key, value|
    typecast = ATTRIBUTE_TYPES[key] || ATTRIBUTE_TYPES[:default]
    [key, typecast.call(value)]
  end
end

Instance Attribute Details

#node (readonly)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 51

attr_reader :node

Instance Method Details

#attachment_attributes (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 70

def attachment_attributes
  read_json_object_attribute("data-trix-attachment")
end

#attributes

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 57

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

#composed_attributes (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 74

def composed_attributes
  read_json_object_attribute("data-trix-attributes")
end

#read_json_attribute(name) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 82

def read_json_attribute(name)
  if value = node[name]
    begin
      JSON.parse(value)
    rescue => e
      Rails.logger.error "[#{self.class.name}] Couldn't parse JSON #{value} from NODE #{node.inspect}"
      Rails.logger.error "[#{self.class.name}] Failed with #{e.class}: #{e.backtrace}"
      nil
    end
  end
end

#read_json_object_attribute(name) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 78

def read_json_object_attribute(name)
  read_json_attribute(name) || {}
end

#to_html

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 61

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

#to_s

[ GitHub ]

  
# File 'actiontext/lib/action_text/trix_attachment.rb', line 65

def to_s
  to_html
end