Class: Turbo::Streams::TagBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionHelper
Defined in:
app/models/turbo/streams/tag_builder.rb

Overview

This tag builder is used both for inline controller turbo actions (see Turbo::Streams::TurboStreamsTagBuilder) and for turbo stream templates. This object plays together with any normal Ruby you’d run in an ERB template, so you can iterate, like:

<% # app/views/postings/destroy.turbo_stream.erb %>
<% @postings.each do |posting| %>
  <%= turbo_stream.remove posting %>
<% end %>

Or string several separate updates together:

<% # app/views/entries/_entry.turbo_stream.erb %>
<%= turbo_stream.remove entry %>

<%= turbo_stream.append "entries" do %>
  <% # format is automatically switched, such that _entry.html.erb partial is rendered, not _entry.turbo_stream.erb %>
  <%= render partial: "entries/entry", locals: { entry: entry } %>
<%= end %>

Or you can render the HTML that should be part of the update inline:

<% # app/views/topics/merges/_merge.turbo_stream.erb %>
<%= turbo_stream.append dom_id(topic_merge) do %>
  <%= link_to topic_merge.topic.name, topic_path(topic_merge.topic) %>
<% end %>

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ TagBuilder

Returns a new instance of TagBuilder.



28
29
30
# File 'app/models/turbo/streams/tag_builder.rb', line 28

def initialize(view_context)
  @view_context = view_context
end

Instance Method Details

#action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block) ⇒ Object

Send an action of the type name. Options described in the concrete methods.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/turbo/streams/tag_builder.rb', line 97

def action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block)
  target_name = extract_target_name_from(target)

  case
  when content
    turbo_stream_action_tag name, target: target_name, template: (render_record(content) if allow_inferred_rendering) || content
  when block_given?
    turbo_stream_action_tag name, target: target_name, template: @view_context.capture(&block)
  when rendering.any?
    turbo_stream_action_tag name, target: target_name, template: @view_context.render(formats: [ :html ], **rendering)
  else
    turbo_stream_action_tag name, target: target_name, template: (render_record(target) if allow_inferred_rendering)
  end
end

#append(target, content = nil, **rendering, &block) ⇒ Object

Append to the target in the dom identified with target either the content passed in or a rendering result determined by the rendering keyword arguments, the content in the block, or the rendering of the content as a record. Examples:

<%= turbo_stream.append "clearances", "<div id='clearance_5'>Append this to .clearances</div>" %>
<%= turbo_stream.append "clearances", clearance %>
<%= turbo_stream.append "clearances", partial: "clearances/unique_clearance", locals: { clearance: clearance } %>
<%= turbo_stream.append "clearances" do %>
  <div id='clearance_5'>Append this to .clearances</div>
<% end %>


78
79
80
# File 'app/models/turbo/streams/tag_builder.rb', line 78

def append(target, content = nil, **rendering, &block)
  action :append, target, content, **rendering, &block
end

#prepend(target, content = nil, **rendering, &block) ⇒ Object

Prepend to the target in the dom identified with target either the content passed in or a rendering result determined by the rendering keyword arguments or the content in the block, or the rendering of the content as a record. Examples:

<%= turbo_stream.prepend "clearances", "<div id='clearance_5'>Prepend this to .clearances</div>" %>
<%= turbo_stream.prepend "clearances", clearance %>
<%= turbo_stream.prepend "clearances", partial: "clearances/unique_clearance", locals: { clearance: clearance } %>
<%= turbo_stream.prepend "clearances" do %>
  <div id='clearance_5'>Prepend this to .clearances</div>
<% end %>


92
93
94
# File 'app/models/turbo/streams/tag_builder.rb', line 92

def prepend(target, content = nil, **rendering, &block)
  action :prepend, target, content, **rendering, &block
end

#remove(target) ⇒ Object

Removes the target from the dom. The target can either be a dom id string or an object that responds to to_key, which is then called and passed through ActionView::RecordIdentifier.dom_id (all Active Records do). Examples:

<%= turbo_stream.remove "clearance_5" %>
<%= turbo_stream.remove clearance %>


38
39
40
# File 'app/models/turbo/streams/tag_builder.rb', line 38

def remove(target)
  action :remove, target, allow_inferred_rendering: false
end

#replace(target, content = nil, **rendering, &block) ⇒ Object

Replace the target in the dom with the either the content passed in, a rendering result determined by the rendering keyword arguments, the content in the block, or the rendering of the target as a record. Examples:

<%= turbo_stream.replace "clearance_5", "<div id='clearance_5'>Replace the dom target identified by clearance_5</div>" %>
<%= turbo_stream.replace clearance %>
<%= turbo_stream.replace clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
<%= turbo_stream.replace "clearance_5" do %>
  <div id='clearance_5'>Replace the dom target identified by clearance_5</div>
<% end %>


51
52
53
# File 'app/models/turbo/streams/tag_builder.rb', line 51

def replace(target, content = nil, **rendering, &block)
  action :replace, target, content, **rendering, &block
end

#update(target, content = nil, **rendering, &block) ⇒ Object

Update the target in the dom with the either the content passed in or a rendering result determined by the rendering keyword arguments, the content in the block, or the rendering of the target as a record. Examples:

<%= turbo_stream.update "clearance_5", "Update the content of the dom target identified by clearance_5" %>
<%= turbo_stream.update clearance %>
<%= turbo_stream.update clearance, partial: "clearances/unique_clearance", locals: { title: "Hello" } %>
<%= turbo_stream.update "clearance_5" do %>
  Update the content of the dom target identified by clearance_5
<% end %>


64
65
66
# File 'app/models/turbo/streams/tag_builder.rb', line 64

def update(target, content = nil, **rendering, &block)
  action :update, target, content, **rendering, &block
end