diff --git a/README.md b/README.md index 37148ed..f626f53 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,19 @@ are automatically disabled for that response. Instance props are only included i 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. +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. + +```ruby +inertia_config( + component_path_resolver: ->(path:, action:) do + "Storefront/#{path.camelize}/#{action.camelize}" + end +) + +``` + + + ### Layout Inertia layouts use the rails layout convention and can be set or changed in the same way. diff --git a/lib/inertia_rails/configuration.rb b/lib/inertia_rails/configuration.rb index bc73c58..02f73f0 100644 --- a/lib/inertia_rails/configuration.rb +++ b/lib/inertia_rails/configuration.rb @@ -9,6 +9,9 @@ class Configuration # Overrides Rails default rendering behavior to render using Inertia by default. default_render: false, + # Allows the user to hook into the default rendering behavior and change it to fit their needs + component_path_resolver: ->(path:, action:) { "#{path}/#{action}" }, + # DEPRECATED: Let Rails decide which layout should be used based on the # controller configuration. layout: true, @@ -59,10 +62,14 @@ def with_defaults(config) freeze end + def component_path_resolver(path:, action:) + @options[:component_path_resolver].call(path:, action:) + end + OPTION_NAMES.each do |option| define_method(option) { evaluate_option @options[option] - } + } unless method_defined?(option) define_method("#{option}=") { |value| @options[option] = value } diff --git a/lib/inertia_rails/renderer.rb b/lib/inertia_rails/renderer.rb index cfa3a9d..ec090c2 100644 --- a/lib/inertia_rails/renderer.rb +++ b/lib/inertia_rails/renderer.rb @@ -13,9 +13,9 @@ class Renderer ) def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil) - @component = component.is_a?(TrueClass) ? "#{controller.controller_path}/#{controller.action_name}" : component @controller = controller @configuration = controller.__send__(:inertia_configuration) + @component = resolve_component(component) @request = request @response = response @render_method = render_method @@ -106,5 +106,11 @@ def partial_keys def rendering_partial_component? @request.inertia_partial? && @request.headers['X-Inertia-Partial-Component'] == component end + + def resolve_component(component) + return component unless component.is_a? TrueClass + + configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name) + end end end diff --git a/spec/dummy/app/controllers/transformed_inertia_rails_mimic_controller.rb b/spec/dummy/app/controllers/transformed_inertia_rails_mimic_controller.rb new file mode 100644 index 0000000..4861bcf --- /dev/null +++ b/spec/dummy/app/controllers/transformed_inertia_rails_mimic_controller.rb @@ -0,0 +1,11 @@ +class TransformedInertiaRailsMimicController < ApplicationController + inertia_config( + default_render: true, + component_path_resolver: ->(path:, action:) do + "#{path.camelize}/#{action.camelize}" + end + ) + + def render_test + end +end diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 9ba617c..4b236fb 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -32,6 +32,7 @@ get 'instance_props_test' => 'inertia_rails_mimic#instance_props_test' get 'default_render_test' => 'inertia_rails_mimic#default_render_test' + get 'transformed_default_render_test' => 'transformed_inertia_rails_mimic#render_test' get 'default_component_test' => 'inertia_rails_mimic#default_component_test' get 'provided_props_test' => 'inertia_rails_mimic#provided_props_test' diff --git a/spec/inertia/configuration_spec.rb b/spec/inertia/configuration_spec.rb index f7cd6a8..4213535 100644 --- a/spec/inertia/configuration_spec.rb +++ b/spec/inertia/configuration_spec.rb @@ -22,7 +22,7 @@ it 'overrides the global values' do get configuration_path - expect(response.parsed_body.symbolize_keys).to eq( + expect(response.parsed_body.symbolize_keys).to include( deep_merge_shared_data: true, default_render: false, layout: "test", diff --git a/spec/inertia/rails_mimic_spec.rb b/spec/inertia/rails_mimic_spec.rb index e4f1cb7..70bfdd0 100644 --- a/spec/inertia/rails_mimic_spec.rb +++ b/spec/inertia/rails_mimic_spec.rb @@ -33,6 +33,14 @@ expect_inertia.to render_component('inertia_rails_mimic/default_render_test') expect_inertia.to include_props({'name' => 'Brian'}) end + + context 'a rendering transformation is provided' do + it 'renders based on the transformation' do + get transformed_default_render_test_path + + expect_inertia.to render_component('TransformedInertiaRailsMimic/RenderTest') + end + end end end