1
+ # frozen_string_literal: true
2
+
1
3
require 'net/http'
2
4
require 'json'
3
5
require_relative "inertia_rails"
@@ -64,24 +66,34 @@ def shared_data
64
66
# Functionally, this permits using either string or symbol keys in the controller. Since the results
65
67
# is cast to json, we should treat string/symbol keys as identical.
66
68
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
68
74
end
69
75
70
76
def computed_props
71
77
_props = merge_props ( shared_data , props ) . select do |key , prop |
72
78
if rendering_partial_component?
73
- key . in? partial_keys
79
+ partial_keys . none? || key . in? ( partial_keys ) || prop . is_a? ( AlwaysProp )
74
80
else
75
- !prop . is_a? ( InertiaRails :: Lazy )
81
+ !prop . is_a? ( LazyProp )
76
82
end
77
83
end
78
84
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
83
95
end
84
- )
96
+ end
85
97
end
86
98
87
99
def page
@@ -93,18 +105,32 @@ def page
93
105
}
94
106
end
95
107
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
98
110
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
100
122
end
101
123
102
124
def partial_keys
103
125
( @request . headers [ 'X-Inertia-Partial-Data' ] || '' ) . split ( ',' ) . compact . map ( &:to_sym )
104
126
end
105
127
128
+ def partial_except_keys
129
+ ( @request . headers [ 'X-Inertia-Partial-Except' ] || '' ) . split ( ',' ) . filter_map ( &:to_sym )
130
+ end
131
+
106
132
def rendering_partial_component?
107
- @request . inertia_partial? && @request . headers [ 'X-Inertia-Partial-Component' ] == component
133
+ @request . headers [ 'X-Inertia-Partial-Component' ] == component
108
134
end
109
135
110
136
def resolve_component ( component )
0 commit comments