Class: AsyncRender::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/async_render/executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(partial:, locals:, state:, formats: [ :html ]) ⇒ Executor

Returns a new instance of Executor.



3
4
5
6
7
8
# File 'lib/async_render/executor.rb', line 3

def initialize(partial:, locals:, state:, formats: [ :html ])
  @partial = partial
  @locals = locals
  @state = state
  @formats = formats
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/async_render/executor.rb', line 10

def call
  # Wrap in Rails executor for proper request-local state
  Rails.application.executor.wrap do
    # Ensure we have a database connection for this thread
    begin
      AsyncRender.restore_state_proc&.call(state)
      ApplicationController.renderer.render(partial:, locals:, formats:)
    rescue => e
      Rails.logger.error { "Error rendering #{partial}: #{e.message}" }
      e.backtrace.each { |line| Rails.logger.error { "  #{line}" } }

      if Rails.env.local?
        <<~HTML
          <p style='background-color:red;color:white'>
            Error rendering #{ERB::Util.html_escape(partial)}: #{ERB::Util.html_escape(e.message)}<br>
            #{ERB::Util.html_escape(e.backtrace.join("\n")).gsub("\n", "<br>")}
          </p>
        HTML
      else
        ""
      end
    end
  end
end