Derive stream actions arguments from context#4
Derive stream actions arguments from context#4kaspth wants to merge 6 commits intomarcoroth:mainfrom
Conversation
First, we add a `turbo` helper in Rails views, so we can do:
```ruby
Turbo::Elements::TurboStream.new(action: "morph", target: "post_1", view_context: self, partial: "posts/post", locals: { post: @post })
turbo.stream action: "morph", target: "post_1", partial: "posts/post", locals: { post: @post }
```
Next, it's now possible to register custom stream actions, which are just shorthands:
```ruby
Turbo::Ruby.stream_actions do
register :morph # Shorthand to define a method `morph` that calls `stream` behind the scenes.
end
turbo.morph target: "post_1", partial: "posts/post", locals: { post: @post }
```
There's also specific target contexts to help build stream actions.
```ruby
turbo.for(@posts).morph partial: "posts/post" # Will generate a `morph` action for each post.
turbo.target(@post) do |target|
target.frame do
target.stream action: "yolo"
end
end
```
Note: we'll pass along `object:` when `**rendering`, so users can spare passing `locals:`:
```ruby
turbo.morph @post, partial: "posts/post"
```
There's also specific `frame` methods:
```ruby
turbo.frame @post
turbo.target(@post).frame
turbo.(@posts).frame do |post|
# Generate a frame for each post, yielding it into the block.
end
```
|
@marcoroth @joeldrapper this is just me playing around with stuff, I have no idea if it's useful 😄 But this should show the thinking so far. |
|
Ok, so I've tried to move the whole actions context stuff so it's also available on And then the view context version is a lot slimmer and really just forwards on the |
|
Thank you @kaspth! I really love where this is heading and I can definitely see how some of these ideas are going to improve the DX for TurboPower (which is why I started to build this gem). I'm going to rebuild the Ruby/Rails helpers for TurboPower based on your ideas and see how that feels/works out. I'll keep you posted! |
|
@marcoroth awesome, I'll look forward to it! For me the main thing is to try to have a consistent structure to how the context gets to interpret positional arguments (whether they mean e.g. So that both |
First, we add a
turbohelper in Rails views, so we can do:Next, it's now possible to register custom stream actions, which are just shorthands:
There's also specific target contexts to help build stream actions.
Note: we'll pass along
object:when**rendering, so users can spare passinglocals::There's also specific
framemethods: