Skip to content

Commit 3302b25

Browse files
committed
Introduce InertiaRails.always
1 parent d98e817 commit 3302b25

File tree

13 files changed

+117
-35
lines changed

13 files changed

+117
-35
lines changed

lib/inertia_rails/always_prop.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module InertiaRails
4+
class AlwaysProp < BaseProp
5+
end
6+
end

lib/inertia_rails/lazy.rb renamed to lib/inertia_rails/base_prop.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# frozen_string_literal: true
22

33
module InertiaRails
4-
class Lazy
4+
# Base class for all props.
5+
class BaseProp
56
def initialize(value = nil, &block)
67
raise ArgumentError, 'You must provide either a value or a block, not both' if value && block
78

lib/inertia_rails/inertia_rails.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
require 'inertia_rails/lazy'
1+
require 'inertia_rails/base_prop'
2+
require 'inertia_rails/always_prop'
3+
require 'inertia_rails/lazy_prop'
24
require 'inertia_rails/configuration'
35

46
module InertiaRails
5-
CONFIGURATION = Configuration.default
7+
class << self
8+
CONFIGURATION = Configuration.default
69

7-
def self.configure
8-
yield(CONFIGURATION)
9-
end
10+
def configure
11+
yield(CONFIGURATION)
12+
end
1013

11-
def self.configuration
12-
CONFIGURATION
13-
end
14+
def configuration
15+
CONFIGURATION
16+
end
17+
18+
def lazy(value = nil, &block)
19+
LazyProp.new(value, &block)
20+
end
1421

15-
def self.lazy(value = nil, &block)
16-
InertiaRails::Lazy.new(value, &block)
22+
def always(value = nil, &block)
23+
AlwaysProp.new(value, &block)
24+
end
1725
end
1826
end

lib/inertia_rails/lazy_prop.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module InertiaRails
4+
class LazyProp < BaseProp
5+
end
6+
end

lib/inertia_rails/renderer.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'net/http'
24
require 'json'
35
require_relative "inertia_rails"
@@ -70,15 +72,15 @@ def merge_props(shared_data, props)
7072
def computed_props
7173
_props = merge_props(shared_data, props).select do |key, prop|
7274
if rendering_partial_component?
73-
key.in? partial_keys
75+
key.in?(partial_keys) || prop.is_a?(AlwaysProp)
7476
else
75-
!prop.is_a?(InertiaRails::Lazy)
77+
!prop.is_a?(LazyProp)
7678
end
7779
end
7880

7981
deep_transform_values _props do |prop|
8082
case prop
81-
when Lazy
83+
when BaseProp
8284
prop.call(controller)
8385
when Proc
8486
controller.instance_exec(&prop)

spec/dummy/app/controllers/inertia_render_test_controller.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ def props
77
}
88
end
99

10+
def except_props
11+
render inertia: 'TestComponent', props: {
12+
flat: 'flat param',
13+
lazy: InertiaRails.lazy('lazy param'),
14+
nested_lazy: InertiaRails.lazy do
15+
{
16+
first: 'first nested lazy param',
17+
}
18+
end,
19+
nested: {
20+
first: 'first nested param',
21+
second: 'second nested param'
22+
},
23+
always: InertiaRails.always('always prop')
24+
}
25+
end
26+
1027
def view_data
1128
render inertia: 'TestComponent', view_data: {
1229
name: 'Brian',
@@ -34,4 +51,15 @@ def lazy_props
3451
grit: InertiaRails.lazy(->{ 'intense' })
3552
}
3653
end
54+
55+
def always_props
56+
render inertia: 'TestComponent', props: {
57+
always: InertiaRails.always('always prop'),
58+
regular: 'regular prop',
59+
lazy: InertiaRails.lazy do
60+
'lazy prop'
61+
end,
62+
another_lazy: InertiaRails.lazy(->{ 'another lazy prop' })
63+
}
64+
end
3765
end

spec/dummy/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
get 'error_500' => 'inertia_test#error_500'
3131
get 'content_type_test' => 'inertia_test#content_type_test'
3232
get 'lazy_props' => 'inertia_render_test#lazy_props'
33+
get 'always_props' => 'inertia_render_test#always_props'
34+
get 'except_props' => 'inertia_render_test#except_props'
3335
get 'non_inertiafied' => 'inertia_test#non_inertiafied'
3436

3537
get 'instance_props_test' => 'inertia_rails_mimic#instance_props_test'

spec/inertia/always_prop_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.describe InertiaRails::AlwaysProp do
2+
it_behaves_like 'base prop'
3+
end

spec/inertia/base_prop_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.describe InertiaRails::BaseProp do
2+
it_behaves_like 'base prop'
3+
end

spec/inertia/lazy_prop_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.describe InertiaRails::LazyProp do
2+
it_behaves_like 'base prop'
3+
end

0 commit comments

Comments
 (0)