diff --git a/app/helpers/turbo/streams_helper.rb b/app/helpers/turbo/streams_helper.rb index f4df525e..e92d3cfb 100644 --- a/app/helpers/turbo/streams_helper.rb +++ b/app/helpers/turbo/streams_helper.rb @@ -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) diff --git a/test/streams/streams_helper_test.rb b/test/streams/streams_helper_test.rb index e8511dfb..d1312b85 100644 --- a/test/streams/streams_helper_test.rb +++ b/test/streams/streams_helper_test.rb @@ -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_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 \ %(),