Skip to content

Commit

Permalink
update ContentAttachment so that it works with "content" attributes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jamis committed Aug 2, 2022
1 parent 408d061 commit 4499a3c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<figure class="attachment attachment--content">
<%= content_attachment.attachable %>
</figure>
36 changes: 19 additions & 17 deletions actiontext/lib/action_text/attachables/content_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion actiontext/lib/action_text/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions actiontext/test/unit/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4499a3c

Please sign in to comment.