Skip to content

Commit

Permalink
Add attributes hash as optional arg to replace/update model tag build…
Browse files Browse the repository at this point in the history
…er helper (#658)
  • Loading branch information
omarluq authored Sep 16, 2024
1 parent 9cd81b4 commit 64e699a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
24 changes: 12 additions & 12 deletions app/models/turbo/streams/tag_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def remove_all(targets)
# <%= turbo_stream.replace "clearance_5" do %>
# <div id='clearance_5'>Replace the dom target identified by clearance_5</div>
# <% end %>
def replace(target, content = nil, **rendering, &block)
action :replace, target, content, **rendering, &block
def replace(target, content = nil, attributes: {}, **rendering, &block)
action :replace, target, content, attributes: attributes, **rendering, &block
end

# Replace the <tt>targets</tt> in the dom with either the <tt>content</tt> passed in, a rendering result determined
Expand All @@ -90,8 +90,8 @@ def replace(target, content = nil, **rendering, &block)
# <%= turbo_stream.replace_all ".clearance_item" do %>
# <div class='.clearance_item'>Replace the dom target identified by the class clearance_item</div>
# <% end %>
def replace_all(targets, content = nil, **rendering, &block)
action_all :replace, targets, content, **rendering, &block
def replace_all(targets, content = nil, attributes: {}, **rendering, &block)
action_all :replace, targets, content, attributes: attributes, **rendering, &block
end

# Insert the <tt>content</tt> passed in, a rendering result determined by the <tt>rendering</tt> keyword arguments,
Expand Down Expand Up @@ -155,8 +155,8 @@ def after_all(targets, content = nil, **rendering, &block)
# <%= turbo_stream.update "clearance_5" do %>
# Update the content of the dom target identified by clearance_5
# <% end %>
def update(target, content = nil, **rendering, &block)
action :update, target, content, **rendering, &block
def update(target, content = nil, attributes: {}, **rendering, &block)
action :update, target, content, attributes: attributes, **rendering, &block
end

# Update the <tt>targets</tt> in the dom with either the <tt>content</tt> passed in or a rendering result determined
Expand All @@ -168,8 +168,8 @@ def update(target, content = nil, **rendering, &block)
# <%= turbo_stream.update_all "clearance_item" do %>
# Update the content of the dom target identified by the class clearance_item
# <% end %>
def update_all(targets, content = nil, **rendering, &block)
action_all :update, targets, content, **rendering, &block
def update_all(targets, content = nil, attributes: {}, **rendering, &block)
action_all :update, targets, content, attributes: attributes, **rendering, &block
end

# Append to the target in the dom identified with <tt>target</tt> either the <tt>content</tt> passed in or a
Expand Down Expand Up @@ -241,17 +241,17 @@ def refresh(...)
end

# Send an action of the type <tt>name</tt> to <tt>target</tt>. Options described in the concrete methods.
def action(name, target, content = nil, allow_inferred_rendering: true, **rendering, &block)
def action(name, target, content = nil, attributes: {}, 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
turbo_stream_action_tag name, target: target, template: template, **attributes
end

# Send an action of the type <tt>name</tt> to <tt>targets</tt>. Options described in the concrete methods.
def action_all(name, targets, content = nil, allow_inferred_rendering: true, **rendering, &block)
def action_all(name, targets, content = nil, attributes: {}, 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
turbo_stream_action_tag name, targets: targets, template: template, **attributes
end

private
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/messages/show.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<%= turbo_stream.replace @message %>
<%= turbo_stream.replace @message, "Something else" %>
<%= turbo_stream.replace "message_5", "Something fifth" %>
<%= turbo_stream.replace "message_5", "Something fifth", attributes: { method: :morph } %>
<%= turbo_stream.replace "message_5", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %>
<%= turbo_stream.append "messages", @message %>
<%= turbo_stream.append "messages", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %>
Expand Down
1 change: 1 addition & 0 deletions test/streams/streams_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest
<turbo-stream action="replace" target="message_1"><template>#{render(message_1)}</template></turbo-stream>
<turbo-stream action="replace" target="message_1"><template>Something else</template></turbo-stream>
<turbo-stream action="replace" target="message_5"><template>Something fifth</template></turbo-stream>
<turbo-stream method="morph" action="replace" target="message_5"><template>Something fifth</template></turbo-stream>
<turbo-stream action="replace" target="message_5"><template>#{render(message_5)}</template></turbo-stream>
<turbo-stream action="append" target="messages"><template>#{render(message_1)}</template></turbo-stream>
<turbo-stream action="append" target="messages"><template>#{render(message_5)}</template></turbo-stream>
Expand Down

0 comments on commit 64e699a

Please sign in to comment.