Allow Turbo Stream actions to be provided as keyword arguments #374
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.
This pull request allows methods within the
Turbo::Streams::TagBuilder
class to be used with keywords arguments. This also allows users to build helpers for custom action helpers when thetarget
is not required, as seen in #373.It also adds tests cases for all seven default actions and the
action
andaction_all
helper methods to make sure we support all possible variants.The amount of times I've seen people struggle with the
target
andtargets
attribute is super high. I'm also running into it from time to time. I always found it confusing to have a regular action helper fortarget
and a*_all
method fortargets
.What this pull request proposes is to use keywords argument to make it more readable and clear which attribute is being used.
This change will not break backwards compatibility, as it accounts for the arguments to be passed as positional or keyword arguments. So this is also still valid:
The other problem the keywords argument solve is that you don't need to remember which argument is which and in which order you need to pass them. With keywords arguments you can pass them in any order. This might not be a big issue for the default actions, but is really beneficial for custom actions.
This also helps when building your own helpers for custom action without having to reinvent the wheel. Previously you had to do something like:
With this pull request you can now make use of the
action
method and also get thetarget
vstargets
for free, without having to declare two methods in your helper for the custom action. The other benefit you get from this is that you can now pass in a block for the rendering or use the rendering mechanism which are built-into the default actions. So one can now implement a helper for a custom action like this:Which allows this action to be used in all variations:
To achieve this previously it required quite a sophisticated helper method. This pull request really simplifies this a lot!
Related: #375