66
77module InertiaRails
88 class Renderer
9- attr_reader (
10- :component ,
11- :configuration ,
12- :controller ,
13- :props ,
14- :view_data ,
15- :encrypt_history ,
16- :clear_history
17- )
18-
199 def initialize ( component , controller , request , response , render_method , **options )
2010 if component . is_a? ( Hash ) && options . key? ( :props )
2111 raise ArgumentError ,
@@ -28,10 +18,10 @@ def initialize(component, controller, request, response, render_method, **option
2818 @response = response
2919 @render_method = render_method
3020 @view_data = options . fetch ( :view_data , { } )
31- @encrypt_history = options . fetch ( :encrypt_history , configuration . encrypt_history )
21+ @encrypt_history = options . fetch ( :encrypt_history , @ configuration. encrypt_history )
3222 @clear_history = options . fetch ( :clear_history , controller . session [ :inertia_clear_history ] || false )
3323
34- deep_merge = options . fetch ( :deep_merge , configuration . deep_merge_shared_data )
24+ deep_merge = options . fetch ( :deep_merge , @ configuration. deep_merge_shared_data )
3525 passed_props = options . fetch ( :props , component . is_a? ( Hash ) ? component : @controller . __send__ ( :inertia_view_assigns ) )
3626 @props = merge_props ( shared_data , passed_props , deep_merge )
3727
@@ -52,32 +42,32 @@ def render
5242 @render_method . call json : page . to_json , status : @response . status , content_type : Mime [ :json ]
5343 else
5444 begin
55- return render_ssr if configuration . ssr_enabled
45+ return render_ssr if @ configuration. ssr_enabled
5646 rescue StandardError
5747 nil
5848 end
59- controller . instance_variable_set ( '@_inertia_page' , page )
60- @render_method . call template : 'inertia' , layout : layout , locals : view_data . merge ( page : page )
49+ @ controller. instance_variable_set ( '@_inertia_page' , page )
50+ @render_method . call template : 'inertia' , layout : layout , locals : @ view_data. merge ( page : page )
6151 end
6252 end
6353
6454 private
6555
6656 def render_ssr
67- uri = URI ( "#{ configuration . ssr_url } /render" )
57+ uri = URI ( "#{ @ configuration. ssr_url } /render" )
6858 res = JSON . parse ( Net ::HTTP . post ( uri , page . to_json , 'Content-Type' => 'application/json' ) . body )
6959
70- controller . instance_variable_set ( '@_inertia_ssr_head' , res [ 'head' ] . join . html_safe )
71- @render_method . call html : res [ 'body' ] . html_safe , layout : layout , locals : view_data . merge ( page : page )
60+ @ controller. instance_variable_set ( '@_inertia_ssr_head' , res [ 'head' ] . join . html_safe )
61+ @render_method . call html : res [ 'body' ] . html_safe , layout : layout , locals : @ view_data. merge ( page : page )
7262 end
7363
7464 def layout
75- layout = configuration . layout
65+ layout = @ configuration. layout
7666 layout . nil? || layout
7767 end
7868
7969 def shared_data
80- controller . __send__ ( :inertia_shared_data )
70+ @ controller. __send__ ( :inertia_shared_data )
8171 end
8272
8373 # Cast props to symbol keyed hash before merging so that we have a consistent data structure and
@@ -103,7 +93,7 @@ def computed_props
10393 end
10494 end
10595 . then { |props | deep_transform_props ( props ) } # Internal hydration/filtering
106- . then { |props | configuration . prop_transformer ( props : props ) } # Apply user-defined prop transformer
96+ . then { |props | @ configuration. prop_transformer ( props : props ) } # Apply user-defined prop transformer
10797 . tap do |props | # Add meta tags last (never transformed)
10898 props [ :_inertia_meta ] = meta_tags if meta_tags . present?
10999 end
@@ -114,12 +104,12 @@ def page
114104 return @page if defined? ( @page )
115105
116106 @page = {
117- component : component ,
107+ component : @ component,
118108 props : computed_props ,
119109 url : @request . original_fullpath ,
120- version : configuration . version ,
121- encryptHistory : encrypt_history ,
122- clearHistory : clear_history ,
110+ version : @ configuration. version ,
111+ encryptHistory : @ encrypt_history,
112+ clearHistory : @ clear_history,
123113 }
124114
125115 deferred_props = deferred_props_keys
@@ -141,9 +131,9 @@ def deep_transform_props(props, parent_path = [])
141131 transformed_props [ key ] =
142132 case prop
143133 when BaseProp
144- prop . call ( controller )
134+ prop . call ( @ controller)
145135 when Proc
146- controller . instance_exec ( &prop )
136+ @ controller. instance_exec ( &prop )
147137 else
148138 prop
149139 end
@@ -251,12 +241,12 @@ def partial_except_keys
251241 end
252242
253243 def rendering_partial_component?
254- @request . headers [ 'X-Inertia-Partial-Component' ] == component
244+ @request . headers [ 'X-Inertia-Partial-Component' ] == @ component
255245 end
256246
257247 def resolve_component ( component )
258248 if component == true || component . is_a? ( Hash )
259- configuration . component_path_resolver ( path : controller . controller_path , action : controller . action_name )
249+ @ configuration. component_path_resolver ( path : @ controller. controller_path , action : @ controller. action_name )
260250 else
261251 component
262252 end
@@ -292,7 +282,7 @@ def excluded_by_except_partial_keys?(path_with_prefixes)
292282 end
293283
294284 def meta_tags
295- controller . inertia_meta . meta_tags
285+ @ controller. inertia_meta . meta_tags
296286 end
297287 end
298288end
0 commit comments