Skip to content

Commit 6265817

Browse files
skryukovbknoles
authored andcommitted
Add InertiaRails.optional prop
1 parent 93eb1a3 commit 6265817

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

lib/inertia_rails/inertia_rails.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'inertia_rails/ignore_on_first_load_prop'
33
require 'inertia_rails/always_prop'
44
require 'inertia_rails/lazy_prop'
5+
require 'inertia_rails/optional_prop'
56
require 'inertia_rails/defer_prop'
67
require 'inertia_rails/merge_prop'
78
require 'inertia_rails/configuration'
@@ -22,6 +23,10 @@ def lazy(value = nil, &block)
2223
LazyProp.new(value, &block)
2324
end
2425

26+
def optional(&block)
27+
OptionalProp.new(&block)
28+
end
29+
2530
def always(&block)
2631
AlwaysProp.new(&block)
2732
end

lib/inertia_rails/optional_prop.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module InertiaRails
2+
class OptionalProp < IgnoreOnFirstLoadProp
3+
end
4+
end

spec/dummy/app/controllers/inertia_render_test_controller.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def props
1010
def except_props
1111
render inertia: 'TestComponent', props: {
1212
flat: 'flat param',
13-
lazy: InertiaRails.lazy('lazy param'),
14-
nested_lazy: InertiaRails.lazy do
13+
optional: InertiaRails.optional { 'optional param' },
14+
nested_optional: InertiaRails.optional do
1515
{
16-
first: 'first nested lazy param',
16+
first: 'first nested optional param',
1717
}
1818
end,
1919
nested: {
@@ -83,14 +83,22 @@ def lazy_props
8383
}
8484
end
8585

86+
def optional_props
87+
render inertia: 'TestComponent', props: {
88+
regular: 1,
89+
optional: InertiaRails.optional { 1 },
90+
another_optional: InertiaRails.optional { 1 },
91+
}
92+
end
93+
8694
def always_props
8795
render inertia: 'TestComponent', props: {
8896
always: InertiaRails.always { 'always prop' },
8997
regular: 'regular prop',
90-
lazy: InertiaRails.lazy do
91-
'lazy prop'
98+
optional: InertiaRails.optional do
99+
'optional prop'
92100
end,
93-
another_lazy: InertiaRails.lazy(->{ 'another lazy prop' })
101+
another_optional: InertiaRails.optional { 'another optional prop' }
94102
}
95103
end
96104

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
get 'error_500' => 'inertia_test#error_500'
3232
get 'content_type_test' => 'inertia_test#content_type_test'
3333
get 'lazy_props' => 'inertia_render_test#lazy_props'
34+
get 'optional_props' => 'inertia_render_test#optional_props'
3435
get 'always_props' => 'inertia_render_test#always_props'
3536
get 'except_props' => 'inertia_render_test#except_props'
3637
get 'merge_props' => 'inertia_render_test#merge_props'

spec/inertia/rendering_spec.rb

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257
let(:headers) do
258258
{
259259
'X-Inertia' => true,
260-
'X-Inertia-Partial-Data' => 'nested,nested_lazy',
260+
'X-Inertia-Partial-Data' => 'nested,nested_optional',
261261
'X-Inertia-Partial-Except' => 'nested',
262262
'X-Inertia-Partial-Component' => 'TestComponent',
263263
}
@@ -268,7 +268,7 @@
268268
it 'returns listed props without excepted' do
269269
expect(response.parsed_body['props']).to eq(
270270
'always' => 'always prop',
271-
'nested_lazy' => { 'first' => 'first nested lazy param' },
271+
'nested_optional' => { 'first' => 'first nested optional param' },
272272
)
273273
end
274274

@@ -282,25 +282,25 @@
282282
it 'returns all regular and partial props except excepted' do
283283
expect(response.parsed_body['props']).to eq(
284284
'flat' => 'flat param',
285-
'lazy' => 'lazy param',
285+
'optional' => 'optional param',
286286
'always' => 'always prop',
287-
'nested_lazy' => { 'first' => 'first nested lazy param' },
287+
'nested_optional' => { 'first' => 'first nested optional param' },
288288
)
289289
end
290290
end
291291

292292
context 'when except always prop' do
293293
let(:headers) {{
294294
'X-Inertia' => true,
295-
'X-Inertia-Partial-Data' => 'nested_lazy',
295+
'X-Inertia-Partial-Data' => 'nested_optional',
296296
'X-Inertia-Partial-Except' => 'always_prop',
297297
'X-Inertia-Partial-Component' => 'TestComponent',
298298
}}
299299

300300
it 'returns always prop anyway' do
301301
expect(response.parsed_body['props']).to eq(
302302
'always' => 'always prop',
303-
'nested_lazy' => { 'first' => 'first nested lazy param' },
303+
'nested_optional' => { 'first' => 'first nested optional param' },
304304
)
305305
end
306306
end
@@ -309,7 +309,7 @@
309309
let(:headers) do
310310
{
311311
'X-Inertia' => true,
312-
'X-Inertia-Partial-Data' => 'nested_lazy',
312+
'X-Inertia-Partial-Data' => 'nested_optional',
313313
'X-Inertia-Partial-Except' => 'unknown',
314314
'X-Inertia-Partial-Component' => 'TestComponent',
315315
}
@@ -318,7 +318,7 @@
318318
it 'returns props' do
319319
expect(response.parsed_body['props']).to eq(
320320
'always' => 'always prop',
321-
'nested_lazy' => { 'first' => 'first nested lazy param' },
321+
'nested_optional' => { 'first' => 'first nested optional param' },
322322
)
323323
end
324324
end
@@ -327,8 +327,8 @@
327327
let(:headers) do
328328
{
329329
'X-Inertia' => true,
330-
'X-Inertia-Partial-Data' => 'nested,nested_lazy',
331-
'X-Inertia-Partial-Except' => 'nested.first,nested_lazy.first',
330+
'X-Inertia-Partial-Data' => 'nested,nested_optional',
331+
'X-Inertia-Partial-Except' => 'nested.first,nested_optional.first',
332332
'X-Inertia-Partial-Component' => 'TestComponent',
333333
}
334334
end
@@ -337,7 +337,7 @@
337337
expect(response.parsed_body['props']).to eq(
338338
'always' => 'always prop',
339339
'nested' => { 'second' => 'second nested param' },
340-
'nested_lazy' => { 'first' => 'first nested lazy param' },
340+
'nested_optional' => { 'first' => 'first nested optional param' },
341341
)
342342
end
343343
end
@@ -372,6 +372,32 @@
372372
end
373373
end
374374

375+
context 'optional prop rendering' do
376+
context 'on first load' do
377+
let(:page) {
378+
InertiaRails::Renderer.new('TestComponent', controller, request, response, '', props: { regular: 1}).send(:page)
379+
}
380+
before { get optional_props_path }
381+
382+
it { is_expected.to include inertia_div(page) }
383+
end
384+
385+
context 'with a partial reload' do
386+
let(:page) {
387+
InertiaRails::Renderer.new('TestComponent', controller, request, response, '', props: { regular: 1, optional: 1}).send(:page)
388+
}
389+
let(:headers) {{
390+
'X-Inertia' => true,
391+
'X-Inertia-Partial-Data' => 'optional',
392+
'X-Inertia-Partial-Component' => 'TestComponent',
393+
}}
394+
395+
before { get optional_props_path, headers: headers }
396+
397+
it { is_expected.to eq page.to_json }
398+
end
399+
end
400+
375401
context 'always prop rendering' do
376402
let(:headers) { { 'X-Inertia' => true } }
377403

@@ -384,12 +410,12 @@
384410
context 'with a partial reload' do
385411
let(:headers) {{
386412
'X-Inertia' => true,
387-
'X-Inertia-Partial-Data' => 'lazy',
413+
'X-Inertia-Partial-Data' => 'optional',
388414
'X-Inertia-Partial-Component' => 'TestComponent',
389415
}}
390416

391417
it "returns listed and always props" do
392-
expect(response.parsed_body["props"]).to eq({"always" => 'always prop', "lazy" => 'lazy prop' })
418+
expect(response.parsed_body["props"]).to eq({"always" => 'always prop', "optional" => 'optional prop' })
393419
end
394420
end
395421
end

0 commit comments

Comments
 (0)