Class: Turbo::Streams::TagBuilder
- Inherits:
-
Object
- Object
- Turbo::Streams::TagBuilder
- 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 %>
To integrate with custom actions, extend this class in response to the :turbo_streams_tag_builder load hook:
ActiveSupport.on_load :turbo_streams_tag_builder do
def highlight(target)
action :highlight, target
end
def highlight_all(targets)
action_all :highlight, targets
end
end
turbo_stream.highlight "my-element"
# => <turbo-stream action="highlight" target="my-element"><template></template></turbo-stream>
turbo_stream.highlight_all ".my-selector"
# => <turbo-stream action="highlight" targets=".my-selector"><template></template></turbo-stream>
Instance Method Summary collapse
-
#action(name, target, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) ⇒ Object
Send an action of the type
name
totarget
. -
#action_all(name, targets, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) ⇒ Object
Send an action of the type
name
totargets
. -
#after(target, content = nil, **rendering, &block) ⇒ Object
Insert the
content
passed in, a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record after thetarget
in the dom. -
#after_all(targets, content = nil, **rendering, &block) ⇒ Object
Insert the
content
passed in, a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record after thetargets
in the dom. -
#append(target, content = nil, **rendering, &block) ⇒ Object
Append to the target in the dom identified with
target
either thecontent
passed in or a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the content as a record. -
#append_all(targets, content = nil, **rendering, &block) ⇒ Object
Append to the targets in the dom identified with
targets
either thecontent
passed in or a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the content as a record. -
#before(target, content = nil, **rendering, &block) ⇒ Object
Insert the
content
passed in, a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record before thetarget
in the dom. -
#before_all(targets, content = nil, **rendering, &block) ⇒ Object
Insert the
content
passed in, a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record before thetargets
in the dom. -
#initialize(view_context) ⇒ TagBuilder
constructor
A new instance of TagBuilder.
-
#prepend(target, content = nil, **rendering, &block) ⇒ Object
Prepend to the target in the dom identified with
target
either thecontent
passed in or a rendering result determined by therendering
keyword arguments or the content in the block, or the rendering of the content as a record. -
#prepend_all(targets, content = nil, **rendering, &block) ⇒ Object
Prepend to the targets in the dom identified with
targets
either thecontent
passed in or a rendering result determined by therendering
keyword arguments or the content in the block, or the rendering of the content as a record. -
#refresh(**options) ⇒ Object
Creates a ‘turbo-stream` tag with an `[action=“refresh”`] attribute and a `[request-id]` attribute that defaults to `Turbo.current_request_id`:.
-
#remove(target) ⇒ Object
Removes the
target
from the dom. -
#remove_all(targets) ⇒ Object
Removes the
targets
from the dom. -
#replace(target, content = nil, method: nil, **rendering, &block) ⇒ Object
Replace the
target
in the dom with either thecontent
passed in, a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record. -
#replace_all(targets, content = nil, method: nil, **rendering, &block) ⇒ Object
Replace the
targets
in the dom with either thecontent
passed in, a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record. -
#update(target, content = nil, method: nil, **rendering, &block) ⇒ Object
Update the
target
in the dom with either thecontent
passed in or a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the target as a record. -
#update_all(targets, content = nil, method: nil, **rendering, &block) ⇒ Object
Update the
targets
in the dom with either thecontent
passed in or a rendering result determined by therendering
keyword arguments, the content in the block, or the rendering of the targets as a record.
Constructor Details
#initialize(view_context) ⇒ TagBuilder
Returns a new instance of TagBuilder.
46 47 48 49 |
# File 'app/models/turbo/streams/tag_builder.rb', line 46 def initialize(view_context) @view_context = view_context @view_context.formats |= [:html] end |
Instance Method Details
#action(name, target, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) ⇒ Object
Send an action of the type name
to target
. Options described in the concrete methods.
248 249 250 251 252 |
# File 'app/models/turbo/streams/tag_builder.rb', line 248 def action(name, target, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block) turbo_stream_action_tag name, target: target, template: template, method: method end |
#action_all(name, targets, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) ⇒ Object
Send an action of the type name
to targets
. Options described in the concrete methods.
255 256 257 258 259 |
# File 'app/models/turbo/streams/tag_builder.rb', line 255 def action_all(name, targets, content = nil, method: nil, allow_inferred_rendering: true, **rendering, &block) template = render_template(targets, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block) turbo_stream_action_tag name, targets: targets, template: template, method: method end |
#after(target, content = nil, **rendering, &block) ⇒ Object
Insert 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 after the target
in the dom. Examples:
<%= turbo_stream.after "clearance_5", "<div id='clearance_6'>Insert after the dom target identified by clearance_5</div>" %>
<%= turbo_stream.after clearance %>
<%= turbo_stream.after clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
<%= turbo_stream.after "clearance_5" do %>
<div id='clearance_6'>Insert after the dom target identified by clearance_5</div>
<% end %>
134 135 136 |
# File 'app/models/turbo/streams/tag_builder.rb', line 134 def after(target, content = nil, **rendering, &block) action :after, target, content, **rendering, &block end |
#after_all(targets, content = nil, **rendering, &block) ⇒ Object
Insert 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 after the targets
in the dom. Examples:
<%= turbo_stream.after_all ".clearance_item", "<div class='clearance_item'>Insert after the dom target identified by the class clearance_item</div>" %>
<%= turbo_stream.after_all clearance %>
<%= turbo_stream.after_all clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
<%= turbo_stream.after_all "clearance_item" do %>
<div class='clearance_item'>Insert after the dom target identified by the class clearance_item</div>
<% end %>
147 148 149 |
# File 'app/models/turbo/streams/tag_builder.rb', line 147 def after_all(targets, content = nil, **rendering, &block) action_all :after, targets, content, **rendering, &block 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 %>
189 190 191 |
# File 'app/models/turbo/streams/tag_builder.rb', line 189 def append(target, content = nil, **rendering, &block) action :append, target, content, **rendering, &block end |
#append_all(targets, content = nil, **rendering, &block) ⇒ Object
Append to the targets in the dom identified with targets
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_all ".clearances", "<div class='clearance_item'>Append this to .clearance_group</div>" %>
<%= turbo_stream.append_all ".clearances", clearance %>
<%= turbo_stream.append_all ".clearances", partial: "clearances/new_clearance", locals: { clearance: clearance } %>
<%= turbo_stream.append_all ".clearances" do %>
<div id='clearance_item'>Append this to .clearances</div>
<% end %>
203 204 205 |
# File 'app/models/turbo/streams/tag_builder.rb', line 203 def append_all(targets, content = nil, **rendering, &block) action_all :append, targets, content, **rendering, &block end |
#before(target, content = nil, **rendering, &block) ⇒ Object
Insert 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 before the target
in the dom. Examples:
<%= turbo_stream.before "clearance_5", "<div id='clearance_4'>Insert before the dom target identified by clearance_5</div>" %>
<%= turbo_stream.before clearance %>
<%= turbo_stream.before clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
<%= turbo_stream.before "clearance_5" do %>
<div id='clearance_4'>Insert before the dom target identified by clearance_5</div>
<% end %>
108 109 110 |
# File 'app/models/turbo/streams/tag_builder.rb', line 108 def before(target, content = nil, **rendering, &block) action :before, target, content, **rendering, &block end |
#before_all(targets, content = nil, **rendering, &block) ⇒ Object
Insert 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 before the targets
in the dom. Examples:
<%= turbo_stream.before_all ".clearance_item", "<div class='clearance_item'>Insert before the dom target identified by the class clearance_item</div>" %>
<%= turbo_stream.before_all clearance %>
<%= turbo_stream.before_all clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
<%= turbo_stream.before_all ".clearance_item" do %>
<div class='clearance_item'>Insert before the dom target identified by clearance_item</div>
<% end %>
121 122 123 |
# File 'app/models/turbo/streams/tag_builder.rb', line 121 def before_all(targets, content = nil, **rendering, &block) action_all :before, targets, 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 %>
217 218 219 |
# File 'app/models/turbo/streams/tag_builder.rb', line 217 def prepend(target, content = nil, **rendering, &block) action :prepend, target, content, **rendering, &block end |
#prepend_all(targets, content = nil, **rendering, &block) ⇒ Object
Prepend to the targets in the dom identified with targets
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_all ".clearances", "<div class='clearance_item'>Prepend this to .clearances</div>" %>
<%= turbo_stream.prepend_all ".clearances", clearance %>
<%= turbo_stream.prepend_all ".clearances", partial: "clearances/new_clearance", locals: { clearance: clearance } %>
<%= turbo_stream.prepend_all ".clearances" do %>
<div class='clearance_item'>Prepend this to .clearances</div>
<% end %>
231 232 233 |
# File 'app/models/turbo/streams/tag_builder.rb', line 231 def prepend_all(targets, content = nil, **rendering, &block) action_all :prepend, targets, content, **rendering, &block end |
#refresh(**options) ⇒ Object
Creates a ‘turbo-stream` tag with an `[action=“refresh”`] attribute and a `[request-id]` attribute that defaults to `Turbo.current_request_id`:
turbo_stream.refresh
# => <turbo-stream action="refresh" request-id="ef083d55-7516-41b1-ad28-16f553399c6a"></turbo-stream>
turbo_stream.refresh request_id: "abc123"
# => <turbo-stream action="refresh" request-id="abc123"></turbo-stream>
243 244 245 |
# File 'app/models/turbo/streams/tag_builder.rb', line 243 def refresh(**) turbo_stream_refresh_tag(**) 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 %>
57 58 59 |
# File 'app/models/turbo/streams/tag_builder.rb', line 57 def remove(target) action :remove, target, allow_inferred_rendering: false end |
#remove_all(targets) ⇒ Object
Removes the targets
from the dom. The targets can either be a CSS selector 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_all ".clearance_item" %>
<%= turbo_stream.remove_all clearance %>
67 68 69 |
# File 'app/models/turbo/streams/tag_builder.rb', line 67 def remove_all(targets) action_all :remove, targets, allow_inferred_rendering: false end |
#replace(target, content = nil, method: nil, **rendering, &block) ⇒ Object
Replace the target
in the dom with 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 %>
<%= turbo_stream.replace clearance, "<div>Morph the dom target</div>", method: :morph %>
81 82 83 |
# File 'app/models/turbo/streams/tag_builder.rb', line 81 def replace(target, content = nil, method: nil, **rendering, &block) action :replace, target, content, method: method, **rendering, &block end |
#replace_all(targets, content = nil, method: nil, **rendering, &block) ⇒ Object
Replace the targets
in the dom with 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_all ".clearance_item", "<div class='clearance_item'>Replace the dom target identified by the class clearance_item</div>" %>
<%= turbo_stream.replace_all clearance %>
<%= turbo_stream.replace_all clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
<%= turbo_stream.replace_all ".clearance_item" do %>
<div class='.clearance_item'>Replace the dom target identified by the class clearance_item</div>
<% end %>
<%= turbo_stream.replace_all clearance, "<div>Morph the dom target</div>", method: :morph %>
95 96 97 |
# File 'app/models/turbo/streams/tag_builder.rb', line 95 def replace_all(targets, content = nil, method: nil, **rendering, &block) action_all :replace, targets, content, method: method, **rendering, &block end |
#update(target, content = nil, method: nil, **rendering, &block) ⇒ Object
Update the target
in the dom with 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 %>
<%= turbo_stream.update clearance, "<div>Morph the dom target</div>", method: :morph %>
161 162 163 |
# File 'app/models/turbo/streams/tag_builder.rb', line 161 def update(target, content = nil, method: nil, **rendering, &block) action :update, target, content, method: method, **rendering, &block end |
#update_all(targets, content = nil, method: nil, **rendering, &block) ⇒ Object
Update the targets
in the dom with 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 targets as a record. Examples:
<%= turbo_stream.update_all "clearance_item", "Update the content of the dom target identified by the class clearance_item" %>
<%= turbo_stream.update_all clearance %>
<%= turbo_stream.update_all clearance, partial: "clearances/new_clearance", locals: { title: "Hello" } %>
<%= turbo_stream.update_all "clearance_item" do %>
Update the content of the dom target identified by the class clearance_item
<% end %>
<%= turbo_stream.update_all clearance, "<div>Morph the dom target</div>", method: :morph %>
175 176 177 |
# File 'app/models/turbo/streams/tag_builder.rb', line 175 def update_all(targets, content = nil, method: nil, **rendering, &block) action_all :update, targets, content, method: method, **rendering, &block end |