Sha256: ad9c7119cfc1e4dbd7d6b7a095b62925977684d57210b4677e7d0156ee6d3179

Contents?: true

Size: 1.55 KB

Versions: 13

Compression:

Stored size: 1.55 KB

Contents

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="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>
  def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil)
    template = action.to_sym == :remove ? "" : "<template>#{template}</template>"

    if target = convert_to_turbo_stream_dom_id(target)
      %(<turbo-stream action="#{action}" target="#{target}">#{template}</turbo-stream>).html_safe
    elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
      %(<turbo-stream action="#{action}" targets="#{targets}">#{template}</turbo-stream>).html_safe
    else
      raise ArgumentError, "target or targets must be supplied"
    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) ].compact.join
      else
        target
      end
    end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
turbo-rails-1.1.1 app/helpers/turbo/streams/action_helper.rb
turbo-rails-1.1.0 app/helpers/turbo/streams/action_helper.rb
turbo-rails-1.0.1 app/helpers/turbo/streams/action_helper.rb
turbo-rails-1.0.0 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.9.1 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.9.0 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.8.3 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.8.2 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.8.1 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.8.0 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.7.15 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.7.14 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.7.13 app/helpers/turbo/streams/action_helper.rb