update ContentAttachment so that it works with "content" attributes
this makes it possible for an application to embed markup in a document that would otherwise be undesirable or expensive to process. For example, an incoming email may include a complicated bit of DOM in a quote, and while you don't want to have to process and rewrite it, you also don't want to discard it; the content attribute of ContentAttachment allows you to make that work.
This commit is contained in:
parent
408d061a80
commit
4499a3cdd0
@ -0,0 +1,3 @@
|
||||
<figure class="attachment attachment--content">
|
||||
<%= content_attachment.attachable %>
|
||||
</figure>
|
@ -6,33 +6,35 @@ class ContentAttachment
|
||||
include ActiveModel::Model
|
||||
|
||||
def self.from_node(node)
|
||||
if node["content-type"]
|
||||
if matches = node["content-type"].match(/vnd\.rubyonrails\.(.+)\.html/)
|
||||
attachment = new(name: matches[1])
|
||||
attachment if attachment.valid?
|
||||
end
|
||||
end
|
||||
attachment = new(content_type: node["content-type"], content: node["content"])
|
||||
attachment if attachment.valid?
|
||||
end
|
||||
|
||||
attr_accessor :name
|
||||
validates_inclusion_of :name, in: %w( horizontal-rule )
|
||||
attr_accessor :content_type, :content
|
||||
|
||||
validates_format_of :content_type, with: /html/
|
||||
validates_presence_of :content
|
||||
|
||||
def attachable_plain_text_representation(caption)
|
||||
case name
|
||||
when "horizontal-rule"
|
||||
" ┄ "
|
||||
else
|
||||
" "
|
||||
end
|
||||
content_instance.fragment.source
|
||||
end
|
||||
|
||||
def to_html
|
||||
@to_html ||= content_instance.render
|
||||
end
|
||||
|
||||
def to_s
|
||||
to_html
|
||||
end
|
||||
|
||||
def to_partial_path
|
||||
"action_text/attachables/content_attachment"
|
||||
end
|
||||
|
||||
def to_trix_content_attachment_partial_path
|
||||
"action_text/attachables/content_attachments/#{name.underscore}"
|
||||
end
|
||||
private
|
||||
def content_instance
|
||||
@content_instance ||= ActionText::Content.new(content)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -8,7 +8,7 @@ class Attachment
|
||||
|
||||
mattr_accessor :tag_name, default: "action-text-attachment"
|
||||
|
||||
ATTRIBUTES = %w( sgid content-type url href filename filesize width height previewable presentation caption )
|
||||
ATTRIBUTES = %w( sgid content-type url href filename filesize width height previewable presentation caption content )
|
||||
|
||||
class << self
|
||||
def fragment_by_canonicalizing_attachments(content)
|
||||
|
@ -55,6 +55,20 @@ class ActionText::AttachmentTest < ActiveSupport::TestCase
|
||||
assert_equal "[racecar.jpg]", ActionText::Attachment.from_attachable(attachable).to_plain_text
|
||||
end
|
||||
|
||||
test "converts HTML content attachment" do
|
||||
attachment = attachment_from_html(%Q(<action-text-attachment content-type="text/html" content="abc"></action-text-attachment>))
|
||||
attachable = attachment.attachable
|
||||
|
||||
assert_kind_of ActionText::Attachables::ContentAttachment, attachable
|
||||
assert_equal "text/html", attachable.content_type
|
||||
assert_equal "abc", attachable.content
|
||||
|
||||
trix_attachment = attachment.to_trix_attachment
|
||||
assert_kind_of ActionText::TrixAttachment, trix_attachment
|
||||
assert_equal "text/html", trix_attachment.attributes["contentType"]
|
||||
assert_equal "abc", trix_attachment.attributes["content"]
|
||||
end
|
||||
|
||||
test "defaults trix partial to model partial" do
|
||||
attachable = Page.create! title: "Homepage"
|
||||
assert_equal "pages/page", attachable.to_trix_content_attachment_partial_path
|
||||
|
Loading…
Reference in New Issue
Block a user