Sha256: aa0fd860d881aad22e214c30b7deff4440456f2a99f9b0f3999ef1bb4e30ccd3

Contents?: true

Size: 957 Bytes

Versions: 4

Compression:

Stored size: 957 Bytes

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(target)
      target.respond_to?(:to_key) ? ActionView::RecordIdentifier.dom_id(target) : target
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
turbo-rails-0.5.12 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.11 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.10 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.9 app/helpers/turbo/streams/action_helper.rb