Skip to content

Commit 6ea5879

Browse files
committed
Introduce except props support
1 parent 3302b25 commit 6ea5879

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lib/inertia_rails/renderer.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def computed_props
7878
end
7979
end
8080

81+
drop_partial_except_keys(_props) if rendering_partial_component?
82+
8183
deep_transform_values _props do |prop|
8284
case prop
8385
when BaseProp
@@ -105,10 +107,24 @@ def deep_transform_values(hash, &block)
105107
hash.transform_values {|value| deep_transform_values(value, &block)}
106108
end
107109

110+
def drop_partial_except_keys(hash)
111+
partial_except_keys.each do |key|
112+
parts = key.to_s.split('.').map(&:to_sym)
113+
*initial_keys, last_key = parts
114+
current = initial_keys.any? ? hash.dig(*initial_keys) : hash
115+
116+
current.delete(last_key) if current.is_a?(Hash) && !current[last_key].is_a?(AlwaysProp)
117+
end
118+
end
119+
108120
def partial_keys
109121
(@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)
110122
end
111123

124+
def partial_except_keys
125+
(@request.headers['X-Inertia-Partial-Except'] || '').split(',').filter_map(&:to_sym)
126+
end
127+
112128
def rendering_partial_component?
113129
@request.inertia_partial? && @request.headers['X-Inertia-Partial-Component'] == component
114130
end

spec/inertia/rendering_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,79 @@
113113
end
114114
end
115115

116+
context 'partial except rendering' do
117+
let(:headers) do
118+
{
119+
'X-Inertia' => true,
120+
'X-Inertia-Partial-Data' => 'nested,nested_lazy',
121+
'X-Inertia-Partial-Except' => 'nested',
122+
'X-Inertia-Partial-Component' => 'TestComponent',
123+
}
124+
end
125+
126+
before { get except_props_path, headers: headers }
127+
128+
it 'returns listed props without excepted' do
129+
expect(response.parsed_body['props']).to eq(
130+
'always' => 'always prop',
131+
'nested_lazy' => { 'first' => 'first nested lazy param' },
132+
)
133+
end
134+
135+
context 'when except always prop' do
136+
let(:headers) {{
137+
'X-Inertia' => true,
138+
'X-Inertia-Partial-Data' => 'nested_lazy',
139+
'X-Inertia-Partial-Except' => 'always_prop',
140+
'X-Inertia-Partial-Component' => 'TestComponent',
141+
}}
142+
143+
it 'returns always prop anyway' do
144+
expect(response.parsed_body['props']).to eq(
145+
'always' => 'always prop',
146+
'nested_lazy' => { 'first' => 'first nested lazy param' },
147+
)
148+
end
149+
end
150+
151+
context 'when except unknown prop' do
152+
let(:headers) do
153+
{
154+
'X-Inertia' => true,
155+
'X-Inertia-Partial-Data' => 'nested_lazy',
156+
'X-Inertia-Partial-Except' => 'unknown',
157+
'X-Inertia-Partial-Component' => 'TestComponent',
158+
}
159+
end
160+
161+
it 'returns props' do
162+
expect(response.parsed_body['props']).to eq(
163+
'always' => 'always prop',
164+
'nested_lazy' => { 'first' => 'first nested lazy param' },
165+
)
166+
end
167+
end
168+
169+
context 'when excludes with dot notation' do
170+
let(:headers) do
171+
{
172+
'X-Inertia' => true,
173+
'X-Inertia-Partial-Data' => 'nested,nested_lazy',
174+
'X-Inertia-Partial-Except' => 'nested.first,nested_lazy.first',
175+
'X-Inertia-Partial-Component' => 'TestComponent',
176+
}
177+
end
178+
179+
it 'works with dot notation only with simple props' do
180+
expect(response.parsed_body['props']).to eq(
181+
'always' => 'always prop',
182+
'nested' => { 'second' => 'second nested param' },
183+
'nested_lazy' => { 'first' => 'first nested lazy param' },
184+
)
185+
end
186+
end
187+
end
188+
116189
context 'lazy prop rendering' do
117190
context 'on first load' do
118191
let(:page) {

0 commit comments

Comments
 (0)