Skip to content

Commit 8caa288

Browse files
committed
Added a prop transformer method to the configuration
1 parent 3f4c09d commit 8caa288

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/inertia_rails/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class Configuration
1212
# Allows the user to hook into the default rendering behavior and change it to fit their needs
1313
component_path_resolver: ->(path:, action:) { "#{path}/#{action}" },
1414

15+
# A function that transforms the props before they are sent to the client.
16+
prop_transformer: ->(props:) { props },
17+
1518
# DEPRECATED: Let Rails decide which layout should be used based on the
1619
# controller configuration.
1720
layout: true,
@@ -89,6 +92,10 @@ def component_path_resolver(path:, action:)
8992
@options[:component_path_resolver].call(path: path, action: action)
9093
end
9194

95+
def prop_transformer(props:)
96+
@options[:prop_transformer].call(props: props)
97+
end
98+
9299
OPTION_NAMES.each do |option|
93100
unless method_defined?(option)
94101
define_method(option) do

lib/inertia_rails/renderer.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ def merge_props(shared_props, props)
9090
end
9191

9292
def computed_props
93-
merged_props = merge_props(shared_data, props)
94-
deep_transform_props(merged_props).tap do |transformed_props|
95-
transformed_props[:_inertia_meta] = meta_tags if meta_tags.present?
96-
end
93+
merge_props(shared_data, props)
94+
# This performs the internal work of hydrating/filtering props
95+
.then { |props| deep_transform_props(props) }
96+
# Then we apply the user-defined prop transformer
97+
.then { |props| configuration.prop_transformer(props: props) }
98+
# Then we add meta tags after everything since they must not be transformed
99+
.tap { |props| props[:_inertia_meta] = meta_tags if meta_tags.present? }
97100
end
98101

99102
def page

0 commit comments

Comments
 (0)