-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support dot notation for :only keys in partial reloads #163
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a42ea77
Support dot notation for :only keys in partial reloads
bknoles c5653d2
Remove no-longer-necessary code
bknoles af8fb91
Squeeze out a few more unncessary iterations
bknoles 776da3a
Don't filter out intentionally empty hashes from transformed props
bknoles a62b21a
Add a couple more specs to satisfy curiosity
bknoles c6df93d
Add specs to document what happens if you try to only/except evaluate…
bknoles 0c566ee
Document dot notation in partial reloads
bknoles c4606ef
Fix closing tabs tag
bknoles 2027984
Use constants for the enum-like transformation actions
bknoles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ | |
|
||
module InertiaRails | ||
class Renderer | ||
KEEP_PROP = :keep | ||
DONT_KEEP_PROP = :dont_keep | ||
|
||
attr_reader( | ||
:component, | ||
:configuration, | ||
|
@@ -74,25 +77,21 @@ def merge_props(shared_data, props) | |
end | ||
|
||
def computed_props | ||
_props = merge_props(shared_data, props).select do |key, prop| | ||
if rendering_partial_component? | ||
partial_keys.none? || key.in?(partial_keys) || prop.is_a?(AlwaysProp) | ||
else | ||
!prop.is_a?(LazyProp) | ||
end | ||
end | ||
_props = merge_props(shared_data, props) | ||
|
||
drop_partial_except_keys(_props) if rendering_partial_component? | ||
deep_transform_props _props do |prop, path| | ||
next [DONT_KEEP_PROP] unless keep_prop?(prop, path) | ||
|
||
deep_transform_values _props do |prop| | ||
case prop | ||
transformed_prop = case prop | ||
when BaseProp | ||
prop.call(controller) | ||
when Proc | ||
controller.instance_exec(&prop) | ||
else | ||
prop | ||
end | ||
|
||
[KEEP_PROP, transformed_prop] | ||
end | ||
end | ||
|
||
|
@@ -105,28 +104,28 @@ def page | |
} | ||
end | ||
|
||
def deep_transform_values(hash, &block) | ||
return block.call(hash) unless hash.is_a? Hash | ||
def deep_transform_props(props, parent_path = [], &block) | ||
props.reduce({}) do |transformed_props, (key, prop)| | ||
current_path = parent_path + [key] | ||
|
||
hash.transform_values {|value| deep_transform_values(value, &block)} | ||
end | ||
|
||
def drop_partial_except_keys(hash) | ||
partial_except_keys.each do |key| | ||
parts = key.to_s.split('.').map(&:to_sym) | ||
*initial_keys, last_key = parts | ||
current = initial_keys.any? ? hash.dig(*initial_keys) : hash | ||
if prop.is_a?(Hash) && prop.any? | ||
nested = deep_transform_props(prop, current_path, &block) | ||
transformed_props.merge!(key => nested) unless nested.empty? | ||
else | ||
action, transformed_prop = block.call(prop, current_path) | ||
transformed_props.merge!(key => transformed_prop) if action == KEEP_PROP | ||
end | ||
|
||
current.delete(last_key) if current.is_a?(Hash) && !current[last_key].is_a?(AlwaysProp) | ||
transformed_props | ||
end | ||
end | ||
|
||
def partial_keys | ||
(@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym) | ||
(@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact | ||
end | ||
|
||
def partial_except_keys | ||
(@request.headers['X-Inertia-Partial-Except'] || '').split(',').filter_map(&:to_sym) | ||
(@request.headers['X-Inertia-Partial-Except'] || '').split(',').compact | ||
end | ||
|
||
def rendering_partial_component? | ||
|
@@ -138,5 +137,34 @@ def resolve_component(component) | |
|
||
configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name) | ||
end | ||
|
||
def keep_prop?(prop, path) | ||
return true if prop.is_a?(AlwaysProp) | ||
|
||
if rendering_partial_component? | ||
path_with_prefixes = path_prefixes(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worth nothing that if we're in a partial reload, we'll end up calculating this array for every single prop key. That should still be way faster than fully evaluating all of those props, though. |
||
return false if excluded_by_only_partial_keys?(path_with_prefixes) | ||
return false if excluded_by_except_partial_keys?(path_with_prefixes) | ||
end | ||
|
||
# Precedence: Evaluate LazyProp only after partial keys have been checked | ||
return false if prop.is_a?(LazyProp) && !rendering_partial_component? | ||
|
||
true | ||
end | ||
|
||
def path_prefixes(parts) | ||
(0...parts.length).map do |i| | ||
parts[0..i].join('.') | ||
end | ||
end | ||
|
||
def excluded_by_only_partial_keys?(path_with_prefixes) | ||
partial_keys.present? && (path_with_prefixes & partial_keys).empty? | ||
end | ||
|
||
def excluded_by_except_partial_keys?(path_with_prefixes) | ||
partial_except_keys.present? && (path_with_prefixes & partial_except_keys).any? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the prop filtering logic now lives in one concise place. i debated pulling this out into a class, but I don't think the cleanliness is worth the extra indirection.