app/helpers/turbo/streams/action_helper.rb in turbo-rails-1.4.0 vs app/helpers/turbo/streams/action_helper.rb in turbo-rails-1.5.0

- old
+ new

@@ -9,10 +9,22 @@ # 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 = Message.find(1) + # turbo_stream_action_tag "remove", target: message + # # => <turbo-stream action="remove" target="message_1"></turbo-stream> + # + # message = Message.find(1) + # turbo_stream_action_tag "remove", target: [message, :special] + # # => <turbo-stream action="remove" target="special_message_1"></turbo-stream> + # def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes) template = action.to_sym == :remove ? "" : 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) @@ -23,11 +35,11 @@ end end private 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)}" + if Array(target).any? { |value| value.respond_to?(:to_key) } + "#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(*target)}" else target end end end