From dfabcf787b44a67642fc97f4e76c0cf73d78a17c Mon Sep 17 00:00:00 2001 From: AlexKovynev Date: Mon, 16 Sep 2024 10:13:22 +0300 Subject: [PATCH] Add method to TagBuilder (#665) Co-authored-by: AlexKovynev --- app/models/turbo/streams/tag_builder.rb | 28 +++++++++++-------- .../app/views/messages/show.turbo_stream.erb | 2 ++ .../views/messages/update.turbo_stream.erb | 2 ++ test/streams/streams_controller_test.rb | 6 +++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/models/turbo/streams/tag_builder.rb b/app/models/turbo/streams/tag_builder.rb index b6032ab5..5b05f57c 100644 --- a/app/models/turbo/streams/tag_builder.rb +++ b/app/models/turbo/streams/tag_builder.rb @@ -77,8 +77,9 @@ def remove_all(targets) # <%= turbo_stream.replace "clearance_5" do %> #
Replace the dom target identified by clearance_5
# <% end %> - def replace(target, content = nil, **rendering, &block) - action :replace, target, content, **rendering, &block + # <%= turbo_stream.replace clearance, "
Morph the dom target
", method: :morph %> + def replace(target, content = nil, method: nil, **rendering, &block) + action :replace, target, content, method: method, **rendering, &block end # Replace the targets in the dom with either the content passed in, a rendering result determined @@ -90,8 +91,9 @@ def replace(target, content = nil, **rendering, &block) # <%= turbo_stream.replace_all ".clearance_item" do %> #
Replace the dom target identified by the class clearance_item
# <% end %> - def replace_all(targets, content = nil, **rendering, &block) - action_all :replace, targets, content, **rendering, &block + # <%= turbo_stream.replace_all clearance, "
Morph the dom target
", method: :morph %> + def replace_all(targets, content = nil, method: nil, **rendering, &block) + action_all :replace, targets, content, method: method, **rendering, &block end # Insert the content passed in, a rendering result determined by the rendering keyword arguments, @@ -155,8 +157,9 @@ def after_all(targets, content = nil, **rendering, &block) # <%= turbo_stream.update "clearance_5" do %> # Update the content of the dom target identified by clearance_5 # <% end %> - def update(target, content = nil, **rendering, &block) - action :update, target, content, **rendering, &block + # <%= turbo_stream.update clearance, "
Morph the dom target
", method: :morph %> + def update(target, content = nil, method: nil, **rendering, &block) + action :update, target, content, method: method, **rendering, &block end # Update the targets in the dom with either the content passed in or a rendering result determined @@ -168,8 +171,9 @@ def update(target, content = nil, **rendering, &block) # <%= turbo_stream.update_all "clearance_item" do %> # Update the content of the dom target identified by the class clearance_item # <% end %> - def update_all(targets, content = nil, **rendering, &block) - action_all :update, targets, content, **rendering, &block + # <%= turbo_stream.update_all clearance, "
Morph the dom target
", method: :morph %> + def update_all(targets, content = nil, method: nil, **rendering, &block) + action_all :update, targets, content, method: method, **rendering, &block end # Append to the target in the dom identified with target either the content passed in or a @@ -241,17 +245,17 @@ def refresh(...) end # Send an action of the type name to target. Options described in the concrete methods. - def action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block) + def action(name, target, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block) - turbo_stream_action_tag name, target: target, template: template + turbo_stream_action_tag name, target: target, template: template, method: method end # Send an action of the type name to targets. Options described in the concrete methods. - def action_all(name, targets, content = nil, allow_inferred_rendering: true, **rendering, &block) + def action_all(name, targets, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) template = render_template(targets, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block) - turbo_stream_action_tag name, targets: targets, template: template + turbo_stream_action_tag name, targets: targets, template: template, method: method end private diff --git a/test/dummy/app/views/messages/show.turbo_stream.erb b/test/dummy/app/views/messages/show.turbo_stream.erb index 38b8eebb..c705d2f5 100644 --- a/test/dummy/app/views/messages/show.turbo_stream.erb +++ b/test/dummy/app/views/messages/show.turbo_stream.erb @@ -2,6 +2,8 @@ <%= turbo_stream.replace @message %> <%= turbo_stream.replace @message, "Something else" %> <%= turbo_stream.replace "message_5", "Something fifth" %> +<%= turbo_stream.replace "message_5", "Something fifth", method: :morph %> +<%= turbo_stream.update "message_5", "Something fifth", method: :morph %> <%= turbo_stream.replace "message_5", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %> <%= turbo_stream.append "messages", @message %> <%= turbo_stream.append "messages", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %> diff --git a/test/dummy/app/views/messages/update.turbo_stream.erb b/test/dummy/app/views/messages/update.turbo_stream.erb index b7211b1c..02b9e16e 100644 --- a/test/dummy/app/views/messages/update.turbo_stream.erb +++ b/test/dummy/app/views/messages/update.turbo_stream.erb @@ -2,6 +2,8 @@ <%= turbo_stream.replace_all @message %> <%= turbo_stream.replace_all @message, "Something else" %> <%= turbo_stream.replace_all "#message_4", "Something fourth" %> +<%= turbo_stream.replace_all "#message_5", "Something fifth", method: :morph %> +<%= turbo_stream.update_all "#message_5", "Something fifth", method: :morph %> <%= turbo_stream.replace_all "#message_5", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %> <%= turbo_stream.append_all "#messages", @message %> <%= turbo_stream.append_all "#messages", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %> diff --git a/test/streams/streams_controller_test.rb b/test/streams/streams_controller_test.rb index 173626db..401fef44 100644 --- a/test/streams/streams_controller_test.rb +++ b/test/streams/streams_controller_test.rb @@ -23,6 +23,8 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest + + @@ -37,7 +39,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest patch message_path(id: 1), as: :turbo_stream - assert_turbo_stream action: :replace, count: 4 + assert_turbo_stream action: :replace, count: 5 assert_turbo_stream action: :replace, targets: "#message_4" do assert_select 'template', 'Something fourth' end @@ -46,6 +48,8 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest + +