module Turbo::Streams::ActionHelper
def convert_to_turbo_stream_dom_id(target, include_selector: false)
def convert_to_turbo_stream_dom_id(target, include_selector: false) target_array = Array.wrap(target) if target_array.any? { |value| value.respond_to?(:to_key) } "#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target_array)}" else target end end
def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes)
turbo_stream_action_tag "remove", target: [message, :special]
message = Message.find(1)
# =>
turbo_stream_action_tag "remove", target: message
message = Message.find(1)
`ActionView::RecordIdentifier#dom_id`
The `target:` keyword option will forward `ActionView::RecordIdentifier#dom_id`-compatible arguments to
# =>
Hello!
turbo_stream_action_tag "replace", targets: "message_1", template: %(
Hello!
)# =>
Hello!
turbo_stream_action_tag "replace", target: "message_1", template: %(
Hello!
)# =>
turbo_stream_action_tag "remove", target: "message_1"
Creates a `turbo-stream` tag according to the passed parameters. Examples:
def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes) template = action.to_sym.in?(%i[ remove refresh ]) ? "" : tag.template(template.to_s.html_safe) if target = convert_to_turbo_stream_dom_id(target) tag.turbo_stream(template, **attributes, action: action, target: target) elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true) tag.turbo_stream(template, **attributes, action: action, targets: targets) else tag.turbo_stream(template, **attributes, action: action) end end
def turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes)
turbo_stream_refresh_tag
Creates a `turbo-stream` tag with an `action="refresh"` attribute. Example:
def turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes) turbo_stream_action_tag(:refresh, "request-id": request_id.presence, **attributes) end