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- )
9+ %i[ component configuration controller props view_data encrypt_history
10+ clear_history ] . each do | method_name |
11+ define_method ( method_name ) do
12+ InertiaRails . deprecator . warn (
13+ "[DEPRECATION] Accessing `InertiaRails::Renderer# #{ method_name } ` is deprecated and will be removed in v4.0"
14+ )
15+ instance_variable_get ( "@ #{ method_name } " )
16+ end
17+ end
1818
1919 def initialize ( component , controller , request , response , render_method , **options )
2020 if component . is_a? ( Hash ) && options . key? ( :props )
@@ -24,15 +24,20 @@ def initialize(component, controller, request, response, render_method, **option
2424
2525 @controller = controller
2626 @configuration = controller . __send__ ( :inertia_configuration )
27- @component = resolve_component ( component )
2827 @request = request
2928 @response = response
3029 @render_method = render_method
31- @props = options . fetch ( :props , component . is_a? ( Hash ) ? component : controller . __send__ ( :inertia_view_assigns ) )
3230 @view_data = options . fetch ( :view_data , { } )
33- @deep_merge = options . fetch ( :deep_merge , configuration . deep_merge_shared_data )
34- @encrypt_history = options . fetch ( :encrypt_history , configuration . encrypt_history )
31+ @encrypt_history = options . fetch ( :encrypt_history , @configuration . encrypt_history )
3532 @clear_history = options . fetch ( :clear_history , controller . session [ :inertia_clear_history ] || false )
33+
34+ deep_merge = options . fetch ( :deep_merge , @configuration . deep_merge_shared_data )
35+ passed_props = options . fetch ( :props ,
36+ component . is_a? ( Hash ) ? component : @controller . __send__ ( :inertia_view_assigns ) )
37+ @props = merge_props ( shared_data , passed_props , deep_merge )
38+
39+ @component = resolve_component ( component )
40+
3641 @controller . instance_variable_set ( '@_inertia_rendering' , true )
3742 controller . inertia_meta . add ( options [ :meta ] ) if options [ :meta ]
3843 end
@@ -48,41 +53,41 @@ def render
4853 @render_method . call json : page . to_json , status : @response . status , content_type : Mime [ :json ]
4954 else
5055 begin
51- return render_ssr if configuration . ssr_enabled
56+ return render_ssr if @ configuration. ssr_enabled
5257 rescue StandardError
5358 nil
5459 end
55- controller . instance_variable_set ( '@_inertia_page' , page )
56- @render_method . call template : 'inertia' , layout : layout , locals : view_data . merge ( page : page )
60+ @ controller. instance_variable_set ( '@_inertia_page' , page )
61+ @render_method . call template : 'inertia' , layout : layout , locals : @ view_data. merge ( page : page )
5762 end
5863 end
5964
6065 private
6166
6267 def render_ssr
63- uri = URI ( "#{ configuration . ssr_url } /render" )
68+ uri = URI ( "#{ @ configuration. ssr_url } /render" )
6469 res = JSON . parse ( Net ::HTTP . post ( uri , page . to_json , 'Content-Type' => 'application/json' ) . body )
6570
66- controller . instance_variable_set ( '@_inertia_ssr_head' , res [ 'head' ] . join . html_safe )
67- @render_method . call html : res [ 'body' ] . html_safe , layout : layout , locals : view_data . merge ( page : page )
71+ @ controller. instance_variable_set ( '@_inertia_ssr_head' , res [ 'head' ] . join . html_safe )
72+ @render_method . call html : res [ 'body' ] . html_safe , layout : layout , locals : @ view_data. merge ( page : page )
6873 end
6974
7075 def layout
71- layout = configuration . layout
76+ layout = @ configuration. layout
7277 layout . nil? || layout
7378 end
7479
7580 def shared_data
76- controller . __send__ ( :inertia_shared_data )
81+ @ controller. __send__ ( :inertia_shared_data )
7782 end
7883
7984 # Cast props to symbol keyed hash before merging so that we have a consistent data structure and
8085 # avoid duplicate keys after merging.
8186 #
8287 # Functionally, this permits using either string or symbol keys in the controller. Since the results
8388 # is cast to json, we should treat string/symbol keys as identical.
84- def merge_props ( shared_props , props )
85- if @ deep_merge
89+ def merge_props ( shared_props , props , deep_merge )
90+ if deep_merge
8691 shared_props . deep_symbolize_keys . deep_merge! ( props . deep_symbolize_keys )
8792 else
8893 shared_props . symbolize_keys . merge ( props . symbolize_keys )
@@ -91,16 +96,15 @@ def merge_props(shared_props, props)
9196
9297 def computed_props
9398 # rubocop:disable Style/MultilineBlockChain
94- merge_props ( shared_data , props )
95- . then do |merged_props | # Always keep errors in the props
99+ @ props
100+ . tap do |merged_props | # Always keep errors in the props
96101 if merged_props . key? ( :errors ) && !merged_props [ :errors ] . is_a? ( BaseProp )
97102 errors = merged_props [ :errors ]
98103 merged_props [ :errors ] = InertiaRails . always { errors }
99104 end
100- merged_props
101105 end
102106 . then { |props | deep_transform_props ( props ) } # Internal hydration/filtering
103- . then { |props | configuration . prop_transformer ( props : props ) } # Apply user-defined prop transformer
107+ . then { |props | @ configuration. prop_transformer ( props : props ) } # Apply user-defined prop transformer
104108 . tap do |props | # Add meta tags last (never transformed)
105109 props [ :_inertia_meta ] = meta_tags if meta_tags . present?
106110 end
@@ -111,12 +115,12 @@ def page
111115 return @page if defined? ( @page )
112116
113117 @page = {
114- component : component ,
118+ component : @ component,
115119 props : computed_props ,
116120 url : @request . original_fullpath ,
117- version : configuration . version ,
118- encryptHistory : encrypt_history ,
119- clearHistory : clear_history ,
121+ version : @ configuration. version ,
122+ encryptHistory : @ encrypt_history,
123+ clearHistory : @ clear_history,
120124 }
121125
122126 deferred_props = deferred_props_keys
@@ -138,9 +142,9 @@ def deep_transform_props(props, parent_path = [])
138142 transformed_props [ key ] =
139143 case prop
140144 when BaseProp
141- prop . call ( controller )
145+ prop . call ( @ controller)
142146 when Proc
143- controller . instance_exec ( &prop )
147+ @ controller. instance_exec ( &prop )
144148 else
145149 prop
146150 end
@@ -248,12 +252,12 @@ def partial_except_keys
248252 end
249253
250254 def rendering_partial_component?
251- @request . headers [ 'X-Inertia-Partial-Component' ] == component
255+ @request . headers [ 'X-Inertia-Partial-Component' ] == @ component
252256 end
253257
254258 def resolve_component ( component )
255259 if component == true || component . is_a? ( Hash )
256- configuration . component_path_resolver ( path : controller . controller_path , action : controller . action_name )
260+ @ configuration. component_path_resolver ( path : @ controller. controller_path , action : @ controller. action_name )
257261 else
258262 component
259263 end
@@ -289,7 +293,7 @@ def excluded_by_except_partial_keys?(path_with_prefixes)
289293 end
290294
291295 def meta_tags
292- controller . inertia_meta . meta_tags
296+ @ controller. inertia_meta . meta_tags
293297 end
294298 end
295299end
0 commit comments