Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attributes hash as optional arg to replace/update model tag builders #658

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -229,17 +229,17 @@ def prepend_all(targets, content = nil, **rendering, &block)
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