2018-10-08 17:44:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-01 05:43:25 -04:00
|
|
|
require "active_support/core_ext/object/try"
|
|
|
|
|
2018-04-13 19:23:04 -04:00
|
|
|
module ActionText
|
2018-02-07 19:26:19 -05:00
|
|
|
module Attachments
|
|
|
|
module TrixConversion
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def fragment_by_converting_trix_attachments(content)
|
|
|
|
Fragment.wrap(content).replace(TrixAttachment::SELECTOR) do |node|
|
|
|
|
from_trix_attachment(TrixAttachment.new(node))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_trix_attachment(trix_attachment)
|
|
|
|
from_attributes(trix_attachment.attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_trix_attachment(content = trix_attachment_content)
|
|
|
|
attributes = full_attributes.dup
|
|
|
|
attributes["content"] = content if content
|
|
|
|
TrixAttachment.from_attributes(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def trix_attachment_content
|
|
|
|
if partial_path = attachable.try(:to_trix_content_attachment_partial_path)
|
2021-03-19 13:35:09 -04:00
|
|
|
ActionText::Content.render(partial: partial_path, formats: :html, object: self, as: model_name.element)
|
2018-02-07 19:26:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|