Skip to content

Commit 15d0d2d

Browse files
authored
Allow Default Rendering Behavior to be Customized (#144)
* allow customizeable rendering * this feels cleaner * add readme example * Revert "this feels cleaner" This reverts commit e1f650b. * change back to config and better naming * switch to named params
1 parent 5afa24e commit 15d0d2d

File tree

7 files changed

+49
-3
lines changed

7 files changed

+49
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ are automatically disabled for that response. Instance props are only included i
7777

7878
Automatic component name is also opt in, you must set the [`default_render`](#default_render) config value to `true`. Otherwise, you can simply `render inertia: true` for the same behavior explicitly.
7979

80+
If the default component path doesn't match your convention, you can define a method to resolve it however you like via the `component_path_resolver` config value. The value of this should be callable and will receive the path and action and should return a string component path.
81+
82+
```ruby
83+
inertia_config(
84+
component_path_resolver: ->(path:, action:) do
85+
"Storefront/#{path.camelize}/#{action.camelize}"
86+
end
87+
)
88+
89+
```
90+
91+
92+
8093
### Layout
8194

8295
Inertia layouts use the rails layout convention and can be set or changed in the same way.

lib/inertia_rails/configuration.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Configuration
99
# Overrides Rails default rendering behavior to render using Inertia by default.
1010
default_render: false,
1111

12+
# Allows the user to hook into the default rendering behavior and change it to fit their needs
13+
component_path_resolver: ->(path:, action:) { "#{path}/#{action}" },
14+
1215
# DEPRECATED: Let Rails decide which layout should be used based on the
1316
# controller configuration.
1417
layout: true,
@@ -59,10 +62,14 @@ def with_defaults(config)
5962
freeze
6063
end
6164

65+
def component_path_resolver(path:, action:)
66+
@options[:component_path_resolver].call(path:, action:)
67+
end
68+
6269
OPTION_NAMES.each do |option|
6370
define_method(option) {
6471
evaluate_option @options[option]
65-
}
72+
} unless method_defined?(option)
6673
define_method("#{option}=") { |value|
6774
@options[option] = value
6875
}

lib/inertia_rails/renderer.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class Renderer
1313
)
1414

1515
def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil)
16-
@component = component.is_a?(TrueClass) ? "#{controller.controller_path}/#{controller.action_name}" : component
1716
@controller = controller
1817
@configuration = controller.__send__(:inertia_configuration)
18+
@component = resolve_component(component)
1919
@request = request
2020
@response = response
2121
@render_method = render_method
@@ -106,5 +106,11 @@ def partial_keys
106106
def rendering_partial_component?
107107
@request.inertia_partial? && @request.headers['X-Inertia-Partial-Component'] == component
108108
end
109+
110+
def resolve_component(component)
111+
return component unless component.is_a? TrueClass
112+
113+
configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name)
114+
end
109115
end
110116
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class TransformedInertiaRailsMimicController < ApplicationController
2+
inertia_config(
3+
default_render: true,
4+
component_path_resolver: ->(path:, action:) do
5+
"#{path.camelize}/#{action.camelize}"
6+
end
7+
)
8+
9+
def render_test
10+
end
11+
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
get 'instance_props_test' => 'inertia_rails_mimic#instance_props_test'
3434
get 'default_render_test' => 'inertia_rails_mimic#default_render_test'
35+
get 'transformed_default_render_test' => 'transformed_inertia_rails_mimic#render_test'
3536
get 'default_component_test' => 'inertia_rails_mimic#default_component_test'
3637
get 'provided_props_test' => 'inertia_rails_mimic#provided_props_test'
3738

spec/inertia/configuration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
it 'overrides the global values' do
2323
get configuration_path
2424

25-
expect(response.parsed_body.symbolize_keys).to eq(
25+
expect(response.parsed_body.symbolize_keys).to include(
2626
deep_merge_shared_data: true,
2727
default_render: false,
2828
layout: "test",

spec/inertia/rails_mimic_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
expect_inertia.to render_component('inertia_rails_mimic/default_render_test')
3434
expect_inertia.to include_props({'name' => 'Brian'})
3535
end
36+
37+
context 'a rendering transformation is provided' do
38+
it 'renders based on the transformation' do
39+
get transformed_default_render_test_path
40+
41+
expect_inertia.to render_component('TransformedInertiaRailsMimic/RenderTest')
42+
end
43+
end
3644
end
3745
end
3846

0 commit comments

Comments
 (0)