Visitar URL original
Support locals when rendering ActionText · rails/rails@bc0372a · GitHub
Skip to content

Commit bc0372a

Browse files
committed
Support locals when rendering ActionText
1 parent 3c81c37 commit bc0372a

9 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎actiontext/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Support rendering ActionText with locals
2+
3+
<%= render @post.body, locals: {turbo_frame: :_top} %>
4+
5+
Attachments receive local variables when their partials are rendered.
6+
7+
*Chris Oliver*
8+
19
* Dispatch all Active Storage `direct-upload:`-prefixed events
210

311
Add support for `direct-upload:before-blob-request` and `direct-upload:before-storage-request`.

‎actiontext/app/helpers/action_text/content_helper.rb‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ module ContentHelper
1111
mattr_accessor(:allowed_attributes)
1212
mattr_accessor(:scrubber)
1313

14-
def render_rich_text(rich_text, locals: {})
15-
render_action_text_content(rich_text.body, locals: locals)
16-
end
17-
1814
def render_action_text_content(content, locals: {})
1915
self.prefix_partial_path_with_controller_namespace = false
2016
sanitize_action_text_content(render_action_text_attachments(content, locals: locals))

‎actiontext/app/models/action_text/rich_text.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RichText < Record
4040
cattr_accessor :editor, instance_accessor: false
4141

4242
serialize :body, coder: ActionText::Content
43-
delegate :to_s, :nil?, to: :body
43+
delegate :render_in, :to_s, :nil?, to: :body
4444

4545
##
4646
# :method: record
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%= render_action_text_content(content) %>
1+
<%= render_action_text_content(content, locals: local_assigns.excluding(:content)) %>

‎actiontext/lib/action_text/attachables/content_attachment.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def attachable_markdown_representation(caption, attachment_links: false)
2626
end
2727

2828
def to_html
29-
@to_html ||= content_instance.render(content_instance)
29+
@to_html ||= content_instance.render(partial: content_instance.to_partial_path, object: content_instance)
3030
end
3131

3232
def to_s

‎actiontext/lib/action_text/attachments/conversion.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def editor_attachment_content
3434
attachable.try(:to_editor_content_attachment_partial_path) ||
3535
ActionText.deprecator.silence { attachable.try(:to_trix_content_attachment_partial_path) }
3636
)
37-
ActionText::Content.render(partial: partial_path, formats: :html, object: self, as: model_name.element)
37+
ActionText::Content.render(partial: partial_path, formats: :html, object: self, as: model_name.element, locals: { in_editor: true })
3838
end
3939
end
4040
end

‎actiontext/lib/action_text/content.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def to_html
170170
fragment.to_html
171171
end
172172

173-
def to_rendered_html_with_layout
174-
render layout: "action_text/contents/content", partial: to_partial_path, formats: :html, locals: { content: self }
173+
def to_rendered_html_with_layout(locals: {})
174+
render layout: "action_text/contents/content", partial: to_partial_path, formats: :html, locals: locals.merge(content: self)
175175
end
176176

177177
def to_partial_path
@@ -189,6 +189,13 @@ def to_s
189189
to_rendered_html_with_layout
190190
end
191191

192+
# Safely transforms Content into an HTML String with optional locals.
193+
#
194+
# render @post.body, turbo_frame: "_top"
195+
def render_in(view_context, locals: {}, **options)
196+
to_rendered_html_with_layout(locals: locals)
197+
end
198+
192199
def as_json(*)
193200
to_html
194201
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<span class="mentioned-person"><%= person.name %></span>
1+
<span class="mentioned-person"><%= person.name %><%= " (#{local_assigns[:role]})" if local_assigns[:role] %></span>

‎actiontext/test/unit/content_test.rb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ class ActionText::ContentTest < ActiveSupport::TestCase
151151
assert_not defined?(::ApplicationController)
152152
end
153153

154+
test "renders via render_in" do
155+
html = "<h1>Hello world</h1>"
156+
rendered = content_from_html(html).render_in(ActionController::Base.new.view_context)
157+
158+
assert_includes rendered, html
159+
assert_match %r/\A#{Regexp.escape '<div class="trix-content">'}/, rendered
160+
end
161+
162+
test "render_in passes locals through to attachment partials" do
163+
alice = people(:alice)
164+
html = %Q(<action-text-attachment sgid="#{alice.attachable_sgid}"></action-text-attachment>)
165+
166+
ActionText::Content.with_renderer MessagesController.renderer do
167+
rendered = content_from_html(html).render_in(ActionController::Base.new.view_context, locals: { role: "admin" })
168+
assert_includes rendered, "Alice (admin)"
169+
end
170+
end
171+
154172
test "does basic sanitization" do
155173
html = "<div onclick='action()'>safe<script>unsafe</script></div>"
156174
rendered = content_from_html(html).to_rendered_html_with_layout

0 commit comments

Comments
 (0)