1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #45739 from basecamp/content-attachment-update

update ContentAttachment so that it works with "content" attributes
This commit is contained in:
Jeremy Daer 2022-09-06 11:15:10 -07:00 committed by GitHub
commit 01d345c4d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 19 deletions

View file

@ -1,3 +1,7 @@
* Update ContentAttachment so that it can encapsulate arbitrary HTML content in a document.
*Jamis Buck*
* Fix an issue that caused the content layout to render multiple times when a
rich_text field was updated.

View file

@ -0,0 +1,3 @@
<figure class="attachment attachment--content">
<%= content_attachment.attachable %>
</figure>

View file

@ -2,36 +2,38 @@
module ActionText
module Attachables
class ContentAttachment
class ContentAttachment # :nodoc:
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 = new(content_type: node["content-type"], content: node["content"])
attachment if attachment.valid?
end
end
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
" "
content_instance.fragment.source
end
def to_html
@to_html ||= content_instance.render(content_instance)
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}"
private
def content_instance
@content_instance ||= ActionText::Content.new(content)
end
end
end

View file

@ -8,7 +8,7 @@ module ActionText
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)

View file

@ -55,6 +55,30 @@ 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('<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 "renders content attachment" do
html = '<action-text-attachment content-type="text/html" content="&lt;p&gt;abc&lt;/p&gt;"></action-text-attachment>'
attachment = attachment_from_html(html)
attachable = attachment.attachable
ActionText::Content.with_renderer MessagesController.renderer do
assert_equal "<p>abc</p>", attachable.to_html.strip
end
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