diff --git a/actionpack/lib/action_controller/renderer.rb b/actionpack/lib/action_controller/renderer.rb index d91c40c054..5a43e3b3f9 100644 --- a/actionpack/lib/action_controller/renderer.rb +++ b/actionpack/lib/action_controller/renderer.rb @@ -99,6 +99,7 @@ module ActionController instance.set_response! controller.make_response!(request) instance.render_to_string(*args) end + alias_method :render_to_string, :render # :nodoc: private def normalize_keys(defaults, env) diff --git a/actiontext/app/helpers/action_text/content_helper.rb b/actiontext/app/helpers/action_text/content_helper.rb index 17b50fc556..0d0bb19de2 100644 --- a/actiontext/app/helpers/action_text/content_helper.rb +++ b/actiontext/app/helpers/action_text/content_helper.rb @@ -10,6 +10,7 @@ module ActionText mattr_accessor(:scrubber) def render_action_text_content(content) + self.prefix_partial_path_with_controller_namespace = false sanitize_action_text_content(render_action_text_attachments(content)) end diff --git a/actiontext/lib/action_text.rb b/actiontext/lib/action_text.rb index 0dd6318f89..15fdf70b3b 100644 --- a/actiontext/lib/action_text.rb +++ b/actiontext/lib/action_text.rb @@ -16,6 +16,7 @@ module ActionText autoload :Fragment autoload :HtmlConversion autoload :PlainTextConversion + autoload :Rendering autoload :Serialization autoload :TrixAttachment diff --git a/actiontext/lib/action_text/attachments/trix_conversion.rb b/actiontext/lib/action_text/attachments/trix_conversion.rb index 15319f4e37..2829da1f59 100644 --- a/actiontext/lib/action_text/attachments/trix_conversion.rb +++ b/actiontext/lib/action_text/attachments/trix_conversion.rb @@ -28,7 +28,7 @@ module ActionText private def trix_attachment_content if partial_path = attachable.try(:to_trix_content_attachment_partial_path) - ActionText::Content.renderer.render(partial: partial_path, object: self, as: model_name.element) + ActionText::Content.render(partial: partial_path, object: self, as: model_name.element) end end end diff --git a/actiontext/lib/action_text/content.rb b/actiontext/lib/action_text/content.rb index 16bc6fe031..6126a4de49 100644 --- a/actiontext/lib/action_text/content.rb +++ b/actiontext/lib/action_text/content.rb @@ -1,12 +1,8 @@ # frozen_string_literal: true -require "active_support/core_ext/module/attribute_accessors_per_thread" - module ActionText class Content - include Serialization - - thread_cattr_accessor :renderer + include Rendering, Serialization attr_reader :fragment @@ -88,7 +84,7 @@ module ActionText end def to_rendered_html_with_layout - renderer.render(partial: "action_text/content/layout", locals: { content: self }) + render partial: "action_text/content/layout", locals: { content: self } end def to_s diff --git a/actiontext/lib/action_text/engine.rb b/actiontext/lib/action_text/engine.rb index 8749498158..9d5fd1dd25 100644 --- a/actiontext/lib/action_text/engine.rb +++ b/actiontext/lib/action_text/engine.rb @@ -37,21 +37,24 @@ module ActionText end initializer "action_text.helper" do - ActiveSupport.on_load(:action_controller_base) do - helper ActionText::Engine.helpers + %i[action_controller_base action_mailer].each do |abstract_controller| + ActiveSupport.on_load(abstract_controller) do + helper ActionText::Engine.helpers + end end end - initializer "action_text.renderer" do |app| - app.executor.to_run { ActionText::Content.renderer = ApplicationController.renderer } - app.executor.to_complete { ActionText::Content.renderer = ApplicationController.renderer } - + initializer "action_text.renderer" do ActiveSupport.on_load(:action_text_content) do - self.renderer = ApplicationController.renderer + self.renderer = Class.new(ActionController::Base).renderer end - ActiveSupport.on_load(:action_controller_base) do - before_action { ActionText::Content.renderer = ApplicationController.renderer.new(request.env) } + %i[action_controller_base action_mailer].each do |abstract_controller| + ActiveSupport.on_load(abstract_controller) do + around_action do |controller, action| + ActionText::Content.with_renderer(controller, &action) + end + end end end diff --git a/actiontext/lib/action_text/rendering.rb b/actiontext/lib/action_text/rendering.rb new file mode 100644 index 0000000000..caef71c6b4 --- /dev/null +++ b/actiontext/lib/action_text/rendering.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "active_support/concern" +require "active_support/core_ext/module/attribute_accessors_per_thread" + +module ActionText + module Rendering #:nodoc: + extend ActiveSupport::Concern + + included do + thread_cattr_accessor :renderer, instance_accessor: false + delegate :render, to: :class + end + + class_methods do + def with_renderer(renderer) + previous_renderer = self.renderer + self.renderer = renderer + yield + ensure + self.renderer = previous_renderer + end + + def render(*args, &block) + renderer.render_to_string(*args, &block) + end + end + end +end diff --git a/actiontext/test/dummy/app/controllers/admin/messages_controller.rb b/actiontext/test/dummy/app/controllers/admin/messages_controller.rb new file mode 100644 index 0000000000..7c3316324b --- /dev/null +++ b/actiontext/test/dummy/app/controllers/admin/messages_controller.rb @@ -0,0 +1,5 @@ +class Admin::MessagesController < ActionController::Base + def show + @message = Message.find(params[:id]) + end +end diff --git a/actiontext/test/dummy/app/controllers/application_controller.rb b/actiontext/test/dummy/app/controllers/application_controller.rb deleted file mode 100644 index 09705d12ab..0000000000 --- a/actiontext/test/dummy/app/controllers/application_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ApplicationController < ActionController::Base -end diff --git a/actiontext/test/dummy/app/controllers/messages_controller.rb b/actiontext/test/dummy/app/controllers/messages_controller.rb index ec85acb10d..b323d50a27 100644 --- a/actiontext/test/dummy/app/controllers/messages_controller.rb +++ b/actiontext/test/dummy/app/controllers/messages_controller.rb @@ -1,4 +1,4 @@ -class MessagesController < ApplicationController +class MessagesController < ActionController::Base before_action :set_message, only: [:show, :edit, :update, :destroy] # GET /messages diff --git a/actiontext/test/dummy/app/mailers/messages_mailer.rb b/actiontext/test/dummy/app/mailers/messages_mailer.rb new file mode 100644 index 0000000000..ce0af3b3ca --- /dev/null +++ b/actiontext/test/dummy/app/mailers/messages_mailer.rb @@ -0,0 +1,6 @@ +class MessagesMailer < ApplicationMailer + def notification + @message = params[:message] + mail to: params[:recipient], subject: "NEW MESSAGE: #{@message.subject}" + end +end diff --git a/actiontext/test/dummy/app/views/admin/messages/show.html.erb b/actiontext/test/dummy/app/views/admin/messages/show.html.erb new file mode 100644 index 0000000000..ba9067f201 --- /dev/null +++ b/actiontext/test/dummy/app/views/admin/messages/show.html.erb @@ -0,0 +1,16 @@ +
<%= @message.content.to_plain_text %>+
<%= notice %>
-- Subject: - <%= @message.subject %> -
+", content_from_html(html).to_html end + test "renders with layout when ApplicationController is not defined" do + html = "#{attachment_html}