diff --git a/app/models/concerns/turbo/broadcastable.rb b/app/models/concerns/turbo/broadcastable.rb index 462d647d..d05e6b40 100644 --- a/app/models/concerns/turbo/broadcastable.rb +++ b/app/models/concerns/turbo/broadcastable.rb @@ -518,7 +518,7 @@ def broadcast_rendering_with_defaults(options) options.tap do |o| # Add the current instance into the locals with the element name (which is the un-namespaced name) # as the key. This parallels how the ActionView::ObjectRenderer would create a local variable. - o[:locals] = (o[:locals] || {}).reverse_merge!(model_name.element.to_sym => self).compact + o[:locals] = (o[:locals] || {}).reverse_merge(model_name.element.to_sym => self).compact if o[:html] || o[:partial] return o diff --git a/test/dummy/app/models/comment.rb b/test/dummy/app/models/comment.rb index 990ef722..537dc4c9 100644 --- a/test/dummy/app/models/comment.rb +++ b/test/dummy/app/models/comment.rb @@ -5,5 +5,6 @@ class Comment < ApplicationRecord broadcasts_to ->(comment) { [comment.article, :comments] }, target: ->(comment) { "article_#{comment.article_id}_comments" }, - partial: "comments/different_comment" + partial: "comments/different_comment", + locals: { highlight: true } end diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index 3a5be551..b0d1d2af 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -441,6 +441,23 @@ class Turbo::BroadcastableCommentTest < ActionCable::Channel::TestCase end end + test "creating a second comment while using locals broadcasts the second comment" do + stream = "#{@article.to_gid_param}:comments" + target = "article_#{@article.id}_comments" + + assert_broadcast_on stream, turbo_stream_action_tag("append", target: target, template: %(

comment

\n)) do + perform_enqueued_jobs do + @article.comments.create!(body: "comment") + end + end + + assert_broadcast_on stream, turbo_stream_action_tag("append", target: target, template: %(

another comment

\n)) do + perform_enqueued_jobs do + @article.comments.create!(body: "another comment") + end + end + end + test "updating a comment broadcasts" do comment = @article.comments.create!(body: "random") stream = "#{@article.to_gid_param}:comments"