Module: Turbo::Streams::ActionHelper
- Includes:
- ActionView::Helpers::TagHelper
- Included in:
- Broadcasts, TagBuilder
- Defined in:
- app/helpers/turbo/streams/action_helper.rb
Instance Method Summary collapse
-
#turbo_stream_action_tag(action, attributes = {}) ⇒ Object
Creates a ‘turbo-stream` tag according to the passed parameters.
-
#turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes) ⇒ Object
Creates a ‘turbo-stream` tag with an `action=“refresh”` attribute.
Instance Method Details
#turbo_stream_action_tag(action, attributes = {}) ⇒ Object
Creates a ‘turbo-stream` tag according to the passed parameters. Examples:
turbo_stream_action_tag "remove", target: "message_1"
# => <turbo-stream action="remove" target="message_1"></turbo-stream>
turbo_stream_action_tag "replace", target: "message_1", template: %(<div id="message_1">Hello!</div>)
# => <turbo-stream action="replace" target="message_1"><template><div id="message_1">Hello!</div></template></turbo-stream>
turbo_stream_action_tag "replace", targets: "message_1", template: %(<div id="message_1">Hello!</div>)
# => <turbo-stream action="replace" targets="message_1"><template><div id="message_1">Hello!</div></template></turbo-stream>
The ‘target:` keyword option will forward `ActionView::RecordIdentifier#dom_id`-compatible arguments to `ActionView::RecordIdentifier#dom_id`
= Message.find(1)
turbo_stream_action_tag "remove", target:
# => <turbo-stream action="remove" target="message_1"></turbo-stream>
= Message.find(1)
turbo_stream_action_tag "remove", target: [, :special]
# => <turbo-stream action="remove" target="special_message_1"></turbo-stream>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/helpers/turbo/streams/action_helper.rb', line 25 def turbo_stream_action_tag(action, attributes = {}) attributes.deep_symbolize_keys! if RUBY_VERSION < "3" target = attributes.delete(:target) targets = attributes.delete(:targets) template = attributes.delete(:template) 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 |
#turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes) ⇒ Object
Creates a ‘turbo-stream` tag with an `action=“refresh”` attribute. Example:
turbo_stream_refresh_tag
# => <turbo-stream action="refresh"></turbo-stream>
46 47 48 |
# File 'app/helpers/turbo/streams/action_helper.rb', line 46 def turbo_stream_refresh_tag(request_id: Turbo.current_request_id, **attributes) turbo_stream_action_tag(:refresh, attributes.with_defaults({ "request-id": request_id }.compact)) end |