rails--rails/actiontext/lib/action_text/attachment.rb

106 lines
2.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "active_support/core_ext/object/try"
module ActionText
2018-02-08 00:26:19 +00:00
class Attachment
include Attachments::TrixConversion, Attachments::Minification, Attachments::Caching
mattr_accessor :tag_name, default: "action-text-attachment"
ATTRIBUTES = %w( sgid content-type url href filename filesize width height previewable presentation caption )
2018-02-08 00:26:19 +00:00
class << self
def fragment_by_canonicalizing_attachments(content)
fragment_by_minifying_attachments(fragment_by_converting_trix_attachments(content))
end
def from_node(node, attachable = nil)
new(node, attachable || ActionText::Attachable.from_node(node))
2018-02-08 00:26:19 +00:00
end
def from_attachables(attachables)
2021-04-23 01:08:34 +00:00
Array(attachables).filter_map { |attachable| from_attachable(attachable) }
2018-02-08 00:26:19 +00:00
end
def from_attachable(attachable, attributes = {})
if node = node_from_attributes(attachable.to_rich_text_attributes(attributes))
2018-02-08 00:26:19 +00:00
new(node, attachable)
end
end
def from_attributes(attributes, attachable = nil)
if node = node_from_attributes(attributes)
from_node(node, attachable)
end
end
private
def node_from_attributes(attributes)
if attributes = process_attributes(attributes).presence
ActionText::HtmlConversion.create_element(tag_name, attributes)
2018-02-08 00:26:19 +00:00
end
end
def process_attributes(attributes)
attributes.transform_keys { |key| key.to_s.underscore.dasherize }.slice(*ATTRIBUTES)
end
end
attr_reader :node, :attachable
delegate :to_param, to: :attachable
delegate_missing_to :attachable
def initialize(node, attachable)
@node = node
@attachable = attachable
end
def caption
node_attributes["caption"].presence
end
def full_attributes
node_attributes.merge(attachable_attributes).merge(sgid_attributes)
end
def with_full_attributes
self.class.from_attributes(full_attributes, attachable)
end
def to_plain_text
if respond_to?(:attachable_plain_text_representation)
attachable_plain_text_representation(caption)
else
caption.to_s
end
end
def to_html
HtmlConversion.node_to_html(node)
2018-02-08 00:26:19 +00:00
end
def to_s
to_html
end
def inspect
"#<#{self.class.name} attachable=#{attachable.inspect}>"
end
private
def node_attributes
Enable `Style/MapToHash` cop Ruby 2.6 added block argument processing to `Enumerable#to_h`. https://bugs.ruby-lang.org/issues/15143 Rails 7 requires Ruby 2.7.0 or higher, so the new feature can use it. `Style/MapToHash` cop will detect it. And this cop in the `Style` department, but this seems to improve performance as follows: ```ruby # map_to_hash.rb require 'benchmark/ips' ARRAY = (1..100).to_a HASH = {foo: 1, bar: 2} Benchmark.ips do |x| x.report('array.map.to_h') { ARRAY.map { |v| [v, v * 2] }.to_h } x.report('array.to_h') { ARRAY.to_h { |v| [v, v * 2] } } x.compare! end Benchmark.ips do |x| x.report('hash.map.to_h') { HASH.map { |k, v| [k.to_s, v * 2] }.to_h } x.report('hash.to_h') { HASH.to_h { |k, v| [k.to_s, v * 2] } } x.compare! end ``` ```console % ruby map_to_hash.rb Warming up -------------------------------------- array.map.to_h 9.063k i/100ms array.to_h 9.609k i/100ms Calculating ------------------------------------- array.map.to_h 89.063k (± 3.9%) i/s - 453.150k in 5.096572s array.to_h 96.449k (± 1.7%) i/s - 490.059k in 5.082529s Comparison: array.to_h: 96448.7 i/s array.map.to_h: 89063.4 i/s - 1.08x (± 0.00) slower Warming up -------------------------------------- hash.map.to_h 106.284k i/100ms hash.to_h 149.354k i/100ms Calculating ------------------------------------- hash.map.to_h 1.102M (± 2.2%) i/s - 5.527M in 5.019657s hash.to_h 1.490M (± 0.9%) i/s - 7.468M in 5.013264s Comparison: hash.to_h: 1489707.0 i/s hash.map.to_h: 1101561.5 i/s - 1.35x (± 0.00) slower ``` `Style/MapToHash` cop ... https://docs.rubocop.org/rubocop/1.25/cops_style.html#stylemaptohash
2022-02-25 17:45:24 +00:00
@node_attributes ||= ATTRIBUTES.to_h { |name| [ name.underscore, node[name] ] }.compact
2018-02-08 00:26:19 +00:00
end
def attachable_attributes
@attachable_attributes ||= (attachable.try(:to_rich_text_attributes) || {}).stringify_keys
2018-02-08 00:26:19 +00:00
end
def sgid_attributes
@sgid_attributes ||= node_attributes.slice("sgid").presence || attachable_attributes.slice("sgid")
end
end
end