Skip to content

Commit f58b725

Browse files
authored
Merge pull request rails#40308 from seanpdoyle/action-text-rendering-guides
Improve ActionText extensiblibility
2 parents cc2e097 + 3500571 commit f58b725

File tree

16 files changed

+179
-21
lines changed

16 files changed

+179
-21
lines changed

actiontext/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Expose how we render the HTML _surrounding_ rich text content as an
2+
extensible `layouts/action_view/contents/_content.html.erb` template to
3+
encourage user-land customizations, while retaining private API control over how
4+
the rich text itself is rendered by `action_text/contents/_content.html.erb`
5+
partial.
6+
7+
*Sean Doyle*
8+
19
* Add `with_all_rich_text` method to eager load all rich text associations on a model at once.
210

311
*Matt Swanson*, *DHH*

actiontext/app/helpers/action_text/content_helper.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,31 @@ def render_action_text_attachments(content)
2222
content.render_attachments do |attachment|
2323
unless attachment.in?(content.gallery_attachments)
2424
attachment.node.tap do |node|
25-
node.inner_html = render(attachment, in_gallery: false).chomp
25+
node.inner_html = render_action_text_attachment attachment, locals: { in_gallery: false }
2626
end
2727
end
2828
end.render_attachment_galleries do |attachment_gallery|
2929
render(layout: attachment_gallery, object: attachment_gallery) do
3030
attachment_gallery.attachments.map do |attachment|
31-
attachment.node.inner_html = render(attachment, in_gallery: true).chomp
31+
attachment.node.inner_html = render_action_text_attachment attachment, locals: { in_gallery: true }
3232
attachment.to_html
3333
end.join.html_safe
3434
end.chomp
3535
end
3636
end
37+
38+
def render_action_text_attachment(attachment, locals: {}) #:nodoc:
39+
options = { locals: locals, object: attachment, partial: attachment }
40+
41+
if attachment.respond_to?(:to_attachable_partial_path)
42+
options[:partial] = attachment.to_attachable_partial_path
43+
end
44+
45+
if attachment.respond_to?(:model_name)
46+
options[:as] = attachment.model_name.element
47+
end
48+
49+
render(**options).chomp
50+
end
3751
end
3852
end

actiontext/app/views/action_text/content/_layout.html.erb

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render_action_text_content(content) %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="trix-content">
2+
<%= yield -%>
3+
</div>

actiontext/lib/action_text/attachable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def to_trix_content_attachment_partial_path
7171
to_partial_path
7272
end
7373

74+
def to_attachable_partial_path
75+
to_partial_path
76+
end
77+
7478
def to_rich_text_attributes(attributes = {})
7579
attributes.dup.tap do |attrs|
7680
attrs[:sgid] = attachable_sgid

actiontext/lib/action_text/content.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ def to_html
8484
end
8585

8686
def to_rendered_html_with_layout
87-
render partial: "action_text/content/layout", formats: :html, locals: { content: self }
87+
render layout: "action_text/contents/content", partial: to_partial_path, formats: :html, locals: { content: self }
88+
end
89+
90+
def to_partial_path
91+
"action_text/contents/content"
8892
end
8993

9094
def to_s

actiontext/lib/generators/action_text/install/install_generator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def create_actiontext_files
4747

4848
copy_file "#{GEM_ROOT}/app/views/active_storage/blobs/_blob.html.erb",
4949
"app/views/active_storage/blobs/_blob.html.erb"
50+
51+
copy_file "#{GEM_ROOT}/app/views/layouts/action_text/contents/_content.html.erb",
52+
"app/views/layouts/action_text/contents/_content.html.erb"
5053
end
5154

5255
def create_migrations

actiontext/test/dummy/app/models/person.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class Person < ApplicationRecord
44
def to_trix_content_attachment_partial_path
55
"people/trix_content_attachment"
66
end
7+
8+
def to_attachable_partial_path
9+
"people/attachable"
10+
end
711
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span class="mentioned-person"><%= person.name %></span>

0 commit comments

Comments
 (0)