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: nil, view_data: nil, deep_merge: nil) ⇒ Renderer

Returns a new instance of Renderer.



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

def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil)
  @component = component.is_a?(TrueClass) ? "#{controller.controller_path}/#{controller.action_name}" : component
  @controller = controller
  @request = request
  @response = response
  @render_method = render_method
  @props = props ? props : controller.inertia_view_assigns
  @view_data = view_data || {}
  @deep_merge = !deep_merge.nil? ? deep_merge : InertiaRails.deep_merge_shared_data?
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



5
6
7
# File 'lib/inertia_rails/renderer.rb', line 5

def component
  @component
end

#view_dataObject (readonly)

Returns the value of attribute view_data.



5
6
7
# File 'lib/inertia_rails/renderer.rb', line 5

def view_data
  @view_data
end

Instance Method Details

#renderObject



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

def render
  if @request.headers['X-Inertia']
    @response.set_header('Vary', 'X-Inertia')
    @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: layout, locals: (view_data).merge({page: page})
  end
end