Skip to content

Commit

Permalink
Move renderer call out of railtie into base file. Fix syntax issues i…
Browse files Browse the repository at this point in the history
…n renderer.
  • Loading branch information
bknoles committed Sep 11, 2019
1 parent 87e1868 commit 1947200
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
16 changes: 16 additions & 0 deletions lib/inertia.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'inertia/renderer'
require 'inertia/railtie'

ActionController::Renderers.add :inertia do |component, options|
Inertia::Renderer.new(
component,
request,
response,
method(:render),
props: options[:props],
view_data: options[:view_data],
).render
end

module Inertia
end
8 changes: 0 additions & 8 deletions lib/inertia/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,5 @@ class Railtie < Rails::Railtie
app.config.autoload_paths << Inertia::Configuration.path
end

config.after_initialize do
require 'inertia/renderer'
require 'action_controller/metal/renderers'

ActionController::Renderers.add :inertia do |component, options|
Inertia::Renderer.new(component, props: options[:props], view_data: options[:view_data]).render
end
end
end
end
33 changes: 19 additions & 14 deletions lib/inertia/renderer.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
require_relative "inertia"

module Inertia
class Renderer
attr_reader :component, :view_data

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

def render
if request.headers['X-Inertia']
response.set_header('Vary', 'Accept')
response.set_header('X-Inertia', 'true')
render json: page, status: 200
if @request.headers['X-Inertia']
@response.set_header('Vary', 'Accept')
@response.set_header('X-Inertia', 'true')
@render_method.call json: page, status: 200
else
render template: 'inertia', layout: Inertia.layout, locals: (view_data).merge({page: page})
@render_method.call template: 'inertia', layout: Inertia.layout, locals: (view_data).merge({page: page})
end
end

private

def props
only = (request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)
only = (@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)

_props = Inertia.instance.shared_data.merge(@props)

_props = (only.any? && request.headers['X-Inertia-Partial-Component'] == component) ?
_props = (only.any? && @request.headers['X-Inertia-Partial-Component'] == component) ?
_props.select {|key| key.in? only} :
_props

Expand All @@ -36,15 +41,15 @@ def page
{
component: component,
props: props,
url: request.original_url,
url: @request.original_url,
version: Inertia.version,
}
end
end

def deep_transform_values(hash, proc)
return proc.call(hash) unless hash.is_a? Hash

hash.transform_values {|value| deep_transform_values(value, proc)}
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
end
end

0 comments on commit 1947200

Please sign in to comment.