Skip to content

Commit

Permalink
Raise ArgumentError when turbo_stream_from helper gets passed blank s…
Browse files Browse the repository at this point in the history
…treamables (#661)

* Handle blank streamable in turbo_stream_for helper

* turbo_stream_from requires at least one non-blank streamable
  • Loading branch information
mwallba committed Aug 31, 2024
1 parent 01ff576 commit e9ed0a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/helpers/turbo/streams_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ def turbo_stream
# It is also possible to pass additional parameters to the channel by passing them through `data` attributes:
#
# <%= turbo_stream_from "room", channel: RoomChannel, data: {room_name: "room #1"} %>
#
# Raises an +ArgumentError+ if all streamables are blank
#
# <%= turbo_stream_from("") %> # => ArgumentError: streamables can't be blank
# <%= turbo_stream_from("", nil) %> # => ArgumentError: streamables can't be blank
def turbo_stream_from(*streamables, **attributes)
raise ArgumentError, "streamables can't be blank" unless streamables.any?(&:present?)
attributes[:channel] = attributes[:channel]&.to_s || "Turbo::StreamsChannel"
attributes[:"signed-stream-name"] = Turbo::StreamsChannel.signed_stream_name(streamables)

Expand Down
20 changes: 20 additions & 0 deletions test/streams/streams_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ class Turbo::StreamsHelperTest < ActionView::TestCase
turbo_stream_from("messages")
end

test "with multiple streamables, some blank" do
assert_dom_equal \
%(<turbo-cable-stream-source channel="Turbo::StreamsChannel" signed-stream-name="#{Turbo::StreamsChannel.signed_stream_name(["channel", nil, "", "messages"])}"></turbo-cable-stream-source>),
turbo_stream_from("channel", nil, "", "messages")
end

test "with invalid streamables" do
assert_raises ArgumentError, "streamables can't be blank" do
turbo_stream_from("")
end

assert_raises ArgumentError, "streamables can't be blank" do
turbo_stream_from(nil)
end

assert_raises ArgumentError, "streamables can't be blank" do
turbo_stream_from("", nil)
end
end

test "with streamable and html attributes" do
assert_dom_equal \
%(<turbo-cable-stream-source channel="Turbo::StreamsChannel" signed-stream-name="#{Turbo::StreamsChannel.signed_stream_name("messages")}" data-stream-target="source"></turbo-cable-stream-source>),
Expand Down

0 comments on commit e9ed0a2

Please sign in to comment.