Sha256: 4d043135b45219523cee37a3da8c3201011eb6218d17e08dfe07a4af1e6eb715

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 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 = template ? "<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

2 entries across 2 versions & 1 rubygems

Version Path
turbo-rails-0.5.2 app/helpers/turbo/streams/action_helper.rb
turbo-rails-0.5.1 app/helpers/turbo/streams/action_helper.rb