Skip to content

Commit 3cf3b27

Browse files
committed
Move component resolving to renderer
1 parent 8701c6c commit 3cf3b27

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

lib/inertia_rails.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
require 'patches/mapper'
88

99
ActionController::Renderers.add :inertia do |component, options|
10-
if component.is_a?(Hash) && options[:props].nil?
11-
options[:props] = component
12-
component = true
13-
end
14-
1510
InertiaRails::Renderer.new(
1611
component,
1712
self,

lib/inertia_rails/renderer.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ class Renderer
2020
)
2121

2222
def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil, encrypt_history: nil, clear_history: nil)
23+
raise ArgumentError, 'Parameter `props` is not allowed when passing a Hash as the first argument' if component.is_a?(Hash) && !props.nil?
24+
2325
@controller = controller
2426
@configuration = controller.__send__(:inertia_configuration)
2527
@component = resolve_component(component)
2628
@request = request
2729
@response = response
2830
@render_method = render_method
29-
@props = props || controller.__send__(:inertia_view_assigns)
31+
@props = props || (component.is_a?(Hash) ? component : controller.__send__(:inertia_view_assigns))
3032
@view_data = view_data || {}
3133
@deep_merge = !deep_merge.nil? ? deep_merge : configuration.deep_merge_shared_data
3234
@encrypt_history = !encrypt_history.nil? ? encrypt_history : configuration.encrypt_history
@@ -165,9 +167,11 @@ def rendering_partial_component?
165167
end
166168

167169
def resolve_component(component)
168-
return component unless component.is_a? TrueClass
169-
170-
configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name)
170+
if component == true || component.is_a?(Hash)
171+
configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name)
172+
else
173+
component
174+
end
171175
end
172176

173177
def keep_prop?(prop, path)

spec/dummy/app/controllers/inertia_rails_mimic_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ def default_component_test
2727
render inertia: true
2828
end
2929

30-
def default_component_with_params_test
30+
def default_component_with_props_test
3131
render inertia: { my: 'props' }
3232
end
33+
34+
def default_component_with_duplicated_props_test
35+
# should raise an error
36+
render inertia: { my: 'props' }, props: { another: 'prop' }
37+
end
3338
end

spec/dummy/config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
get 'default_render_test' => 'inertia_rails_mimic#default_render_test'
4444
get 'transformed_default_render_test' => 'transformed_inertia_rails_mimic#render_test'
4545
get 'default_component_test' => 'inertia_rails_mimic#default_component_test'
46-
get 'default_component_with_params_test' => 'inertia_rails_mimic#default_component_with_params_test'
46+
get 'default_component_with_props_test' => 'inertia_rails_mimic#default_component_with_props_test'
47+
get 'default_component_with_duplicated_props_test' => 'inertia_rails_mimic#default_component_with_duplicated_props_test'
4748
get 'provided_props_test' => 'inertia_rails_mimic#provided_props_test'
4849

4950
post 'redirect_to_share_test' => 'inertia_test#redirect_to_share_test'

spec/inertia/rails_mimic_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@
2626
end
2727

2828
it 'has the correct derived component with props' do
29-
get default_component_with_params_test_path
29+
get default_component_with_props_test_path
3030

31-
expect_inertia.to render_component('inertia_rails_mimic/default_component_with_params_test')
31+
expect_inertia.to render_component('inertia_rails_mimic/default_component_with_props_test')
3232
.and have_exact_props({my: 'props'})
3333
end
34+
35+
it 'raises an error when props as properties are provided' do
36+
expect {
37+
get default_component_with_duplicated_props_test_path
38+
}.to raise_error(ArgumentError, 'Parameter `props` is not allowed when passing a Hash as the first argument')
39+
end
3440
end
3541

3642
context 'no render is done at all and default_render is enabled' do

0 commit comments

Comments
 (0)