diff --git a/app/models/turbo/streams/tag_builder.rb b/app/models/turbo/streams/tag_builder.rb index 99a2aa47..a766c1e0 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, method = nil, **rendering, &block) + action :replace, target, content, method, **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, method = nil, **rendering, &block) + action_all :replace, targets, content, method, **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, method = nil, **rendering, &block) + action :update, target, content, method, **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, method = nil, **rendering, &block) + action_all :update, targets, content, method, **rendering, &block end # Append to the target in the dom identified with target either the content passed in or a @@ -229,20 +229,24 @@ 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, 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, **attributes_for_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, **attributes_for_method(method) end private + def attributes_for_method(method) + { method: method }.compact + end + def render_template(target, content = nil, allow_inferred_rendering: true, **rendering, &block) case when content.respond_to?(:render_in) diff --git a/test/dummy/app/views/messages/show.turbo_stream.erb b/test/dummy/app/views/messages/show.turbo_stream.erb index 38b8eebb..7e23f450 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", :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..032cfd4b 100644 --- a/test/dummy/app/views/messages/update.turbo_stream.erb +++ b/test/dummy/app/views/messages/update.turbo_stream.erb @@ -2,6 +2,7 @@ <%= 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", :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..da25bd7f 100644 --- a/test/streams/streams_controller_test.rb +++ b/test/streams/streams_controller_test.rb @@ -23,6 +23,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest + @@ -37,7 +38,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 +47,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest +