1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actiontext/lib/action_text/rendering.rb
Jonathan Hefner ef61c9c8a3
Fix rendering Action Text HTML in new threads
Because `ActionText::Content.renderer` is implemented as a
`thread_cattr_accessor`, any default value set in the main thread will
be inaccessible from other threads.  Therefore, use a `cattr_accessor`
to store the default renderer, and fall back to it when `renderer` has
not been set by e.g. `with_renderer`.

Fixes #40757.
2020-12-07 16:41:44 -05:00

30 lines
748 B
Ruby

# 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
cattr_accessor :default_renderer, instance_accessor: false
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 || default_renderer).render_to_string(*args, &block)
end
end
end
end