class ActionView::PartialRenderer

Monkey patch required to make ‘t` work as expected. Is this evil? TODO Do we need to monkey patch other types of renderers as well?

Public Instance Methods

original_render(partial, context, block)
Alias for: render
render(partial, context, block) click to toggle source

See ‘content_for` in `lib/nice_partials/partial.rb` for something similar.

# File lib/nice_partials/monkey_patch.rb, line 7
def render(partial, context, block)
  if block
    partial_prefix = nice_partials_locale_prefix_from_view_context_and_block(context, block)
    context.nice_partials_push_t_prefix partial_prefix
  else
    # Render partial calls with no block should disable any prefix magic.
    context.nice_partials_push_t_prefix ''
  end

  begin
    result = original_render(partial, context, block)
  rescue Exception => exception
    # If there was some sort of exception thrown, we also need to pop the `t` prefix.
    # This provides compatibility with other libraries that depend on catching exceptions from the view renderer.
    context.nice_partials_pop_t_prefix
    raise exception
  end

  # Whether there was a block or not, pop off whatever we put on the stack.
  context.nice_partials_pop_t_prefix

  return result
end
Also aliased as: original_render
render_partial_template(view, locals, template, layout, block) click to toggle source

This and ActionView::Template#render below are for compatibility with Ruby 3, as opposed to altering the original functionality.

# File lib/nice_partials/monkey_patch.rb, line 33
def render_partial_template(view, locals, template, layout, block)
  ActiveSupport::Notifications.instrument(
    "render_partial.action_view",
    identifier: template.identifier,
    layout: layout && layout.virtual_path
  ) do |payload|
    content = template.render(view, locals, ActionView::OutputBuffer.new, {add_to_stack: !block}) do |*name|
      view._layout_for(*name, &block)
    end

    content = layout.render(view, locals) { content } if layout
    payload[:cache_hit] = view.view_renderer.cache_hits[template.virtual_path]
    build_rendered_template(content, template)
  end
end