class ActionText::TrixAttachment
Constants
- ATTRIBUTES
- ATTRIBUTE_TYPES
- COMPOSED_ATTRIBUTES
- SELECTOR
- TAG_NAME
Attributes
node[R]
Public Class Methods
from_attributes(attributes)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 19 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
new(node)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 51 def initialize(node) @node = node end
Private Class Methods
process_attributes(attributes)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 33 def process_attributes(attributes) typecast_attribute_values(transform_attribute_keys(attributes)) end
transform_attribute_keys(attributes)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 37 def transform_attribute_keys(attributes) attributes.transform_keys { |key| key.to_s.underscore.camelize(:lower) } end
typecast_attribute_values(attributes)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 41 def typecast_attribute_values(attributes) attributes.map do |key, value| typecast = ATTRIBUTE_TYPES[key] || ATTRIBUTE_TYPES[:default] [key, typecast.call(value)] end.to_h end
Public Instance Methods
attributes()
click to toggle source
# File lib/action_text/trix_attachment.rb, line 55 def attributes @attributes ||= attachment_attributes.merge(composed_attributes).slice(*ATTRIBUTES) end
to_html()
click to toggle source
# File lib/action_text/trix_attachment.rb, line 59 def to_html ActionText::HtmlConversion.node_to_html(node) end
to_s()
click to toggle source
# File lib/action_text/trix_attachment.rb, line 63 def to_s to_html end
Private Instance Methods
attachment_attributes()
click to toggle source
# File lib/action_text/trix_attachment.rb, line 68 def attachment_attributes read_json_object_attribute("data-trix-attachment") end
composed_attributes()
click to toggle source
# File lib/action_text/trix_attachment.rb, line 72 def composed_attributes read_json_object_attribute("data-trix-attributes") end
read_json_attribute(name)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 80 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)
click to toggle source
# File lib/action_text/trix_attachment.rb, line 76 def read_json_object_attribute(name) read_json_attribute(name) || {} end