Skip to content

Commit

Permalink
Turbo::StreamsChannel can set targets attribute on stream tag (#210)
Browse files Browse the repository at this point in the history
* Turbo::StreamsChannel can set targets attribute

Background:
- hotwired/turbo#113 allowed for multiple targets in turbo frame streams.
- hotwired/turbo-rails#194 implemented it for `Turbo::Streams::TagBuilder`

This attempts to expose the feature on `Turbo::StreamsChannel` methods.

* Add assertions for `targets` argument
  • Loading branch information
atosbucket committed Aug 26, 2021
1 parent faa68d5 commit 01fe682
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 33 deletions.
62 changes: 30 additions & 32 deletions app/channels/turbo/streams/broadcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,69 @@
module Turbo::Streams::Broadcasts
include Turbo::Streams::ActionHelper

def broadcast_remove_to(*streamables, target:)
broadcast_action_to *streamables, action: :remove, target: target
def broadcast_remove_to(*streamables, **opts)
broadcast_action_to *streamables, action: :remove, **opts
end

def broadcast_replace_to(*streamables, target:, **rendering)
broadcast_action_to *streamables, action: :replace, target: target, **rendering
def broadcast_replace_to(*streamables, **opts)
broadcast_action_to *streamables, action: :replace, **opts
end

def broadcast_update_to(*streamables, target:, **rendering)
broadcast_action_to *streamables, action: :update, target: target, **rendering
def broadcast_update_to(*streamables, **opts)
broadcast_action_to *streamables, action: :update, **opts
end

def broadcast_before_to(*streamables, target:, **rendering)
broadcast_action_to *streamables, action: :before, target: target, **rendering
def broadcast_before_to(*streamables, **opts)
broadcast_action_to *streamables, action: :before, **opts
end

def broadcast_after_to(*streamables, target:, **rendering)
broadcast_action_to *streamables, action: :after, target: target, **rendering
def broadcast_after_to(*streamables, **opts)
broadcast_action_to *streamables, action: :after, **opts
end

def broadcast_append_to(*streamables, target:, **rendering)
broadcast_action_to *streamables, action: :append, target: target, **rendering
def broadcast_append_to(*streamables, **opts)
broadcast_action_to *streamables, action: :append, **opts
end

def broadcast_prepend_to(*streamables, target:, **rendering)
broadcast_action_to *streamables, action: :prepend, target: target, **rendering
def broadcast_prepend_to(*streamables, **opts)
broadcast_action_to *streamables, action: :prepend, **opts
end

def broadcast_action_to(*streamables, action:, target:, **rendering)
broadcast_stream_to *streamables, content: turbo_stream_action_tag(action, target: target, template:
def broadcast_action_to(*streamables, action:, target: nil, targets: nil, **rendering)
broadcast_stream_to *streamables, content: turbo_stream_action_tag(action, target: target, targets: targets, template:
rendering.delete(:content) || (rendering.any? ? render_format(:html, **rendering) : nil)
)
end


def broadcast_replace_later_to(*streamables, target:, **rendering)
broadcast_action_later_to *streamables, action: :replace, target: target, **rendering
def broadcast_replace_later_to(*streamables, **opts)
broadcast_action_later_to *streamables, action: :replace, **opts
end

def broadcast_update_later_to(*streamables, target:, **rendering)
broadcast_action_later_to *streamables, action: :update, target: target, **rendering
def broadcast_update_later_to(*streamables, **opts)
broadcast_action_later_to *streamables, action: :update, **opts
end

def broadcast_before_later_to(*streamables, target:, **rendering)
broadcast_action_later_to *streamables, action: :before, target: target, **rendering
def broadcast_before_later_to(*streamables, **opts)
broadcast_action_later_to *streamables, action: :before, **opts
end

def broadcast_after_later_to(*streamables, target:, **rendering)
broadcast_action_later_to *streamables, action: :after, target: target, **rendering
def broadcast_after_later_to(*streamables, **opts)
broadcast_action_later_to *streamables, action: :after, **opts
end

def broadcast_append_later_to(*streamables, target:, **rendering)
broadcast_action_later_to *streamables, action: :append, target: target, **rendering
def broadcast_append_later_to(*streamables, **opts)
broadcast_action_later_to *streamables, action: :append, **opts
end

def broadcast_prepend_later_to(*streamables, target:, **rendering)
broadcast_action_later_to *streamables, action: :prepend, target: target, **rendering
def broadcast_prepend_later_to(*streamables, **opts)
broadcast_action_later_to *streamables, action: :prepend, **opts
end

def broadcast_action_later_to(*streamables, action:, target:, **rendering)
def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, **rendering)
Turbo::Streams::ActionBroadcastJob.perform_later \
stream_name_from(streamables), action: action, target: target, **rendering
stream_name_from(streamables), action: action, target: target, targets: targets, **rendering
end


def broadcast_render_to(*streamables, **rendering)
broadcast_stream_to *streamables, content: render_format(:turbo_stream, **rendering)
end
Expand Down
61 changes: 60 additions & 1 deletion test/streams/streams_channel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
assert_broadcast_on "stream", turbo_stream_action_tag("remove", target: "message_1") do
Turbo::StreamsChannel.broadcast_remove_to "stream", target: "message_1"
end

assert_broadcast_on "stream", turbo_stream_action_tag("remove", targets: ".message") do
Turbo::StreamsChannel.broadcast_remove_to "stream", targets: ".message"
end
end

test "broadcasting remove now with record" do
Expand All @@ -25,18 +29,30 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_replace_to "stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
end

assert_broadcast_on "stream", turbo_stream_action_tag("replace", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_replace_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end

test "broadcasting update now" do
assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_update_to "stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
end

assert_broadcast_on "stream", turbo_stream_action_tag("update", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_update_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end

test "broadcasting append now" do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_append_to "stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
end

assert_broadcast_on "stream", turbo_stream_action_tag("append", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_append_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end

test "broadcasting append now with empty template" do
Expand All @@ -49,14 +65,21 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_prepend_to "stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_prepend_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end

test "broadcasting action now" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", target: "messages", partial: "messages/message", locals: { message: "hello!" }
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end

test "broadcasting replace later" do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "<p>hello!</p>") do
Expand All @@ -65,6 +88,13 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
"stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("replace", targets: ".message", template: "<p>hello!</p>") do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_replace_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end
end

test "broadcasting update later" do
Expand All @@ -74,6 +104,13 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
"stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("update", targets: ".message", template: "<p>hello!</p>") do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_update_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end
end

test "broadcasting append later" do
Expand All @@ -83,6 +120,13 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
"stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("append", targets: ".message", template: "<p>hello!</p>") do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_append_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end
end

test "broadcasting prepend later" do
Expand All @@ -92,6 +136,14 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
"stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_prepend_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end

end

test "broadcasting action later" do
Expand All @@ -101,6 +153,13 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
"stream", action: "prepend", target: "messages", partial: "messages/message", locals: { message: "hello!" }
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_action_later_to \
"stream", action: "prepend", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
end
end
end


Expand Down

0 comments on commit 01fe682

Please sign in to comment.