Skip to content

Commit

Permalink
Allow Default Rendering Behavior to be Customized (#144)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BrandonShar authored Oct 26, 2024
1 parent 5afa24e commit 15d0d2d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion lib/inertia_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion lib/inertia_rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion spec/inertia/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions spec/inertia/rails_mimic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 15d0d2d

Please sign in to comment.