Skip to content

Commit

Permalink
Just require target attribute for default Stream actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Sep 13, 2022
1 parent 50fb92f commit d9127ef
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/helpers/turbo/streams/action_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Turbo::Streams::ActionHelper
include ActionView::Helpers::TagHelper

TARGET_REQUIRED_ACTIONS = [:remove, :replace, :before, :after, :update, :append, :prepend]

# Creates a `turbo-stream` tag according to the passed parameters. Examples:
#
# turbo_stream_action_tag "remove", target: "message_1"
Expand All @@ -19,11 +21,18 @@ def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **
elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
tag.turbo_stream(template, **attributes.merge(action: action, targets: targets))
else
raise ArgumentError, "target or targets must be supplied"
raise ArgumentError, "target or targets must be supplied" if needs_target_attribute?(action)

tag.turbo_stream(template, **attributes.merge(action: action)).html_safe
end
end

private

def needs_target_attribute?(action)
TARGET_REQUIRED_ACTIONS.include?(action.to_sym)
end

def convert_to_turbo_stream_dom_id(target, include_selector: false)
if target.respond_to?(:to_key)
[ ("#" if include_selector), ActionView::RecordIdentifier.dom_id(target) ].compact.join
Expand Down
48 changes: 48 additions & 0 deletions test/streams/action_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,52 @@ class Turbo::ActionHelperTest < ActionCable::Channel::TestCase

assert_equal stream, turbo_stream_action_tag("append", target: "message_1", template: "Template", class: { "stream": true, "another-stream": true, "no-stream": false })
end

test "raises ArgumentError if no target attribute is passed for remove" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("remove") }

assert_equal "target or targets must be supplied", exp.message
end

test "raises ArgumentError if no target attribute is passed for replace" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("replace") }

assert_equal "target or targets must be supplied", exp.message
end

test "raises ArgumentError if no target attribute is passed for before" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("before") }

assert_equal "target or targets must be supplied", exp.message
end

test "raises ArgumentError if no target attribute is passed for after" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("after") }

assert_equal "target or targets must be supplied", exp.message
end

test "raises ArgumentError if no target attribute is passed for update" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("update") }

assert_equal "target or targets must be supplied", exp.message
end

test "raises ArgumentError if no target attribute is passed for append" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("append") }

assert_equal "target or targets must be supplied", exp.message
end

test "raises ArgumentError if no target attribute is passed for prepend" do
exp = assert_raises(ArgumentError) { turbo_stream_action_tag("prepend") }

assert_equal "target or targets must be supplied", exp.message
end

test "doesn't raise ArgumentError if no target attribute is passed for custom action" do
stream = assert_nothing_raised { turbo_stream_action_tag("my_custom_action") }

assert_equal "<turbo-stream action=\"my_custom_action\"><template></template></turbo-stream>", stream
end
end

0 comments on commit d9127ef

Please sign in to comment.