module Turbo::Streams::ActionHelper
# Creates a `turbo-stream` tag according to the passed parameters. Examples:
#
# turbo_stream_action_tag "remove", target: "message_1"
# # =>
#
# turbo_stream_action_tag "replace", target: "message_1", template: %(
Hello!
)
# # => Hello!
def turbo_stream_action_tag(action, target:, template: nil)
target = convert_to_turbo_stream_dom_id(target)
template = template ? "#{template}" : ""
%(#{template}).html_safe
end
private
def convert_to_turbo_stream_dom_id(element_or_dom_id)
if element_or_dom_id.respond_to?(:to_key)
element = element_or_dom_id
ActionView::RecordIdentifier.dom_id(element)
else
dom_id = element_or_dom_id
end
end
end