1+ # frozen_string_literal: true
2+
13require 'net/http'
24require 'json'
35require_relative "inertia_rails"
@@ -64,24 +66,34 @@ def shared_data
6466 # Functionally, this permits using either string or symbol keys in the controller. Since the results
6567 # is cast to json, we should treat string/symbol keys as identical.
6668 def merge_props ( shared_data , props )
67- shared_data . deep_symbolize_keys . send ( @deep_merge ? :deep_merge : :merge , props . deep_symbolize_keys )
69+ if @deep_merge
70+ shared_data . deep_symbolize_keys . deep_merge! ( props . deep_symbolize_keys )
71+ else
72+ shared_data . symbolize_keys . merge ( props . symbolize_keys )
73+ end
6874 end
6975
7076 def computed_props
7177 _props = merge_props ( shared_data , props ) . select do |key , prop |
7278 if rendering_partial_component?
73- key . in? partial_keys
79+ partial_keys . none? || key . in? ( partial_keys ) || prop . is_a? ( AlwaysProp )
7480 else
75- !prop . is_a? ( InertiaRails :: Lazy )
81+ !prop . is_a? ( LazyProp )
7682 end
7783 end
7884
79- deep_transform_values (
80- _props ,
81- lambda do |prop |
82- prop . respond_to? ( :call ) ? controller . instance_exec ( &prop ) : prop
85+ drop_partial_except_keys ( _props ) if rendering_partial_component?
86+
87+ deep_transform_values _props do |prop |
88+ case prop
89+ when BaseProp
90+ prop . call ( controller )
91+ when Proc
92+ controller . instance_exec ( &prop )
93+ else
94+ prop
8395 end
84- )
96+ end
8597 end
8698
8799 def page
@@ -93,18 +105,32 @@ def page
93105 }
94106 end
95107
96- def deep_transform_values ( hash , proc )
97- return proc . call ( hash ) unless hash . is_a? Hash
108+ def deep_transform_values ( hash , & block )
109+ return block . call ( hash ) unless hash . is_a? Hash
98110
99- hash . transform_values { |value | deep_transform_values ( value , proc ) }
111+ hash . transform_values { |value | deep_transform_values ( value , &block ) }
112+ end
113+
114+ def drop_partial_except_keys ( hash )
115+ partial_except_keys . each do |key |
116+ parts = key . to_s . split ( '.' ) . map ( &:to_sym )
117+ *initial_keys , last_key = parts
118+ current = initial_keys . any? ? hash . dig ( *initial_keys ) : hash
119+
120+ current . delete ( last_key ) if current . is_a? ( Hash ) && !current [ last_key ] . is_a? ( AlwaysProp )
121+ end
100122 end
101123
102124 def partial_keys
103125 ( @request . headers [ 'X-Inertia-Partial-Data' ] || '' ) . split ( ',' ) . compact . map ( &:to_sym )
104126 end
105127
128+ def partial_except_keys
129+ ( @request . headers [ 'X-Inertia-Partial-Except' ] || '' ) . split ( ',' ) . filter_map ( &:to_sym )
130+ end
131+
106132 def rendering_partial_component?
107- @request . inertia_partial? && @request . headers [ 'X-Inertia-Partial-Component' ] == component
133+ @request . headers [ 'X-Inertia-Partial-Component' ] == component
108134 end
109135
110136 def resolve_component ( component )
0 commit comments