class InertiaRails::Renderer

Attributes

component[R]
view_data[R]

Public Class Methods

new(component, controller, request, response, render_method, props:, view_data:) click to toggle source
# File lib/inertia_rails/renderer.rb, line 7
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

Public Instance Methods

render() click to toggle source
# File lib/inertia_rails/renderer.rb, line 17
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
    @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
  end
end

Private Instance Methods

deep_transform_values(hash, proc) click to toggle source
# File lib/inertia_rails/renderer.rb, line 50
def deep_transform_values(hash, proc)
  return proc.call(hash) unless hash.is_a? Hash

  hash.transform_values {|value| deep_transform_values(value, proc)}
end
page() click to toggle source
# File lib/inertia_rails/renderer.rb, line 41
def page
  {
    component: component,
    props: props,
    url: @request.original_fullpath,
    version: ::InertiaRails.version,
  }
end
partial_keys() click to toggle source
# File lib/inertia_rails/renderer.rb, line 56
def partial_keys
  (@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)
end
props() click to toggle source
# File lib/inertia_rails/renderer.rb, line 29
def props
  _props = ::InertiaRails.shared_data(@controller).merge(@props).select do |key, prop|
    if rendering_partial_component?
      key.in? partial_keys
    else
      !prop.is_a?(InertiaRails::Lazy)
    end
  end

  deep_transform_values(_props, lambda {|prop| prop.respond_to?(:call) ? @controller.instance_exec(&prop) : prop })
end
rendering_partial_component?() click to toggle source
# File lib/inertia_rails/renderer.rb, line 60
def rendering_partial_component?
  @request.inertia_partial? && @request.headers['X-Inertia-Partial-Component'] == component
end