Class: InertiaJb::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia_jb/handler.rb

Overview

ActionView template handler for *.inertia page templates.

An .inertia template is jb-style Ruby: its last expression is a Hash of Inertia props. The handler compiles it into a Props object — the template's return value, produced by an otherwise completely ordinary Rails render. Controller#render_to_body recognises that value and hands it to inertia-rails' :inertia renderer.

The default_format declaration is what makes :inertia a real Rails template format: a file named show.inertia carries no format extension, so TemplateDetails#format_or_default falls back to this value and the built ActionView::Template reports format == :inertia. Two things follow for free — the template still resolves for an ordinary html request (a format-less path matches any requested format), and no layout is ever found for it, because layout lookup runs against formats.first (:inertia) and layouts/*.html.erb simply does not match.

Class Method Summary collapse

Class Method Details

.call(template, source = nil) ⇒ Object



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

def self.call(template, source = nil)
  source ||= template.source

  unless template.format == :inertia
    raise ArgumentError, <<~MESSAGE
      InertiaJb: #{template.identifier} has format #{template.format.inspect}, not :inertia.
      Page templates must be named `<action>.inertia` (drop the `.html.`) so that
      Rails resolves them as the :inertia format.
    MESSAGE
  end

  # `begin;#{source}` keeps the template's own line numbers aligned in
  # backtraces. The block evaluates to the template's last expression (the
  # props Hash). `@virtual_path` is assigned by Rails in the compiled
  # method's preamble, so it is already set here.
  "::InertiaJb::Props.new(@virtual_path, begin;#{source}\nend)"
end

.handles_encoding?Boolean

Hand the source to Ruby as-is, like Jb::Handler does.

Returns:

  • (Boolean)


43
44
45
# File 'lib/inertia_jb/handler.rb', line 43

def self.handles_encoding?
  true
end