Class: PortBay::ErbStamper::Handler

Inherits:
ActionView::Template::Handlers::ERB
  • Object
show all
Defined in:
lib/portbay/erb_stamper/handler.rb

Overview

The compile hook.

ActionView::Template::Handlers::ERB#call(template, source) is handed the template's raw source text and returns the Ruby that renders it. That is the seam: stamp the source, hand the stamped text to the stock handler, and every downstream stage — Erubi, the compiled method, the render cache — carries the coordinate without knowing it is there.

Subclassing rather than reimplementing is the whole point. This class adds one if to call and inherits translate_location, erb_trim_mode, escape_ignore_list, strip_trailing_newlines, handles_encoding? and supports_streaming? untouched, so a template that compiled before compiles identically after. It also means a Rails upgrade that changes how ERB compiles changes this handler with it.

Registration happens in Railtie, not here, and it does NOT require the application to own its own boot: ActionView::Template.register_template_handler replaces the :erb entry in ActionView's own handler table, which is the documented way to supply one.

Instance Method Summary collapse

Instance Method Details

#call(template, source) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/portbay/erb_stamper/handler.rb', line 27

def call(template, source)
  rel = ErbStamper.relative_path(template)
  if rel && ErbStamper.enabled?
    begin
      stamped = Stamp.call(source, rel)
      source = stamped if stamped
    rescue StandardError
      # A stamping bug must never take the page down with it. The editor
      # degrades to text search, which is exactly what an un-instrumented
      # Rails project already gets.
    end
  end
  super(template, source)
end