Class: InertiaRails::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia_rails/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component, controller, request, response, render_method, props:, view_data:) ⇒ Renderer

Returns a new instance of Renderer.



9
10
11
12
13
14
15
16
17
# File 'lib/inertia_rails/renderer.rb', line 9

def initialize(component, controller, request, response, render_method, props:, view_data:)
  @component = component
  @controller = controller
  @request = request
  @response = response
  @render_method = render_method
  @props = props || {}
  @view_data = view_data || {}
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



7
8
9
# File 'lib/inertia_rails/renderer.rb', line 7

def component
  @component
end

#view_dataObject (readonly)

Returns the value of attribute view_data.



7
8
9
# File 'lib/inertia_rails/renderer.rb', line 7

def view_data
  @view_data
end

Instance Method Details

#renderObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/inertia_rails/renderer.rb', line 19

def render
  if @request.headers['X-Inertia']
    @response.set_header('Vary', 'Accept')
    @response.set_header('X-Inertia', 'true')
    @render_method.call json: page, status: @response.status, content_type: Mime[:json]
  else
    return render_ssr if ::InertiaRails.ssr_enabled? rescue nil
    @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
  end
end