From 5e4d48ff0e5febece6addce116a74d9c76d2e5c2 Mon Sep 17 00:00:00 2001 From: omarluq Date: Sun, 4 Aug 2024 11:15:38 -0500 Subject: [PATCH] Add attributes hash as optional arg to replace/update model tag builder helper --- app/models/turbo/streams/tag_builder.rb | 24 +++++++++---------- .../app/views/messages/show.turbo_stream.erb | 1 + test/streams/streams_controller_test.rb | 1 + 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/models/turbo/streams/tag_builder.rb b/app/models/turbo/streams/tag_builder.rb index 99a2aa47..f54d4063 100644 --- a/app/models/turbo/streams/tag_builder.rb +++ b/app/models/turbo/streams/tag_builder.rb @@ -77,8 +77,8 @@ 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 + def replace(target, content = nil, attributes: {}, **rendering, &block) + action :replace, target, content, attributes: attributes, **rendering, &block end # Replace the targets in the dom with either the content passed in, a rendering result determined @@ -90,8 +90,8 @@ 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 + def replace_all(targets, content = nil, attributes: {}, **rendering, &block) + action_all :replace, targets, content, attributes: attributes, **rendering, &block end # Insert the content passed in, a rendering result determined by the rendering keyword arguments, @@ -155,8 +155,8 @@ 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 + def update(target, content = nil, attributes: {}, **rendering, &block) + action :update, target, content, attributes: attributes, **rendering, &block end # Update the targets in the dom with either the content passed in or a rendering result determined @@ -168,8 +168,8 @@ 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 + def update_all(targets, content = nil, attributes: {}, **rendering, &block) + action_all :update, targets, content, attributes: attributes, **rendering, &block end # Append to the target in the dom identified with target either the content passed in or a @@ -229,17 +229,17 @@ def prepend_all(targets, content = nil, **rendering, &block) 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, attributes: {}, 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, **attributes 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, attributes: {}, 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, **attributes 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..1b35b598 100644 --- a/test/dummy/app/views/messages/show.turbo_stream.erb +++ b/test/dummy/app/views/messages/show.turbo_stream.erb @@ -2,6 +2,7 @@ <%= turbo_stream.replace @message %> <%= turbo_stream.replace @message, "Something else" %> <%= turbo_stream.replace "message_5", "Something fifth" %> +<%= turbo_stream.replace "message_5", "Something fifth", attributes: { 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/streams/streams_controller_test.rb b/test/streams/streams_controller_test.rb index 173626db..186bc451 100644 --- a/test/streams/streams_controller_test.rb +++ b/test/streams/streams_controller_test.rb @@ -23,6 +23,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest +