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:
commit
01d345c4d6
5 changed files with 52 additions and 19 deletions
|
@ -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.
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
<figure class="attachment attachment--content">
|
||||
<%= content_attachment.attachable %>
|
||||
</figure>
|
|
@ -2,37 +2,39 @@
|
|||
|
||||
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 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(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}"
|
||||
end
|
||||
private
|
||||
def content_instance
|
||||
@content_instance ||= ActionText::Content.new(content)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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="<p>abc</p>"></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
|
||||
|
|
Loading…
Reference in a new issue