Sha256: 509a498ab4811baab286551dda35d63a6bea0f756ac56e14c379ea80295b7da3

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

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

    %(<turbo-stream action="#{action}" target="#{target}">#{template}</turbo-stream>).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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
turbo-rails-0.5.8 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.7 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.6 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.5 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.4 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.3 app/helpers/turbo/streams/action_helper.rb