Skip to content

Commit

Permalink
Add method to TagBuilder (#665)
Browse files Browse the repository at this point in the history
Co-authored-by: AlexKovynev <alexkovynev@gmail.com>
  • Loading branch information
AlexKovynev and AlexKovynev authored Sep 16, 2024
1 parent c0c3021 commit dfabcf7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
28 changes: 16 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,9 @@ 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
# <%= turbo_stream.replace clearance, "<div>Morph the dom target</div>", method: :morph %>
def replace(target, content = nil, method: nil, **rendering, &block)
action :replace, target, content, method: method, **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 +91,9 @@ 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
# <%= turbo_stream.replace_all clearance, "<div>Morph the dom target</div>", method: :morph %>
def replace_all(targets, content = nil, method: nil, **rendering, &block)
action_all :replace, targets, content, method: method, **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 +157,9 @@ 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
# <%= turbo_stream.update clearance, "<div>Morph the dom target</div>", method: :morph %>
def update(target, content = nil, method: nil, **rendering, &block)
action :update, target, content, method: method, **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 +171,9 @@ 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
# <%= turbo_stream.update_all clearance, "<div>Morph the dom target</div>", method: :morph %>
def update_all(targets, content = nil, method: nil, **rendering, &block)
action_all :update, targets, content, method: method, **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 +245,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, 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
turbo_stream_action_tag name, target: target, template: template, method: method
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, 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
turbo_stream_action_tag name, targets: targets, template: template, method: method
end

private
Expand Down
2 changes: 2 additions & 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,8 @@
<%= turbo_stream.replace @message %>
<%= turbo_stream.replace @message, "Something else" %>
<%= turbo_stream.replace "message_5", "Something fifth" %>
<%= turbo_stream.replace "message_5", "Something fifth", method: :morph %>
<%= turbo_stream.update "message_5", "Something fifth", 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
2 changes: 2 additions & 0 deletions test/dummy/app/views/messages/update.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<%= turbo_stream.replace_all @message %>
<%= turbo_stream.replace_all @message, "Something else" %>
<%= turbo_stream.replace_all "#message_4", "Something fourth" %>
<%= turbo_stream.replace_all "#message_5", "Something fifth", method: :morph %>
<%= turbo_stream.update_all "#message_5", "Something fifth", method: :morph %>
<%= turbo_stream.replace_all "#message_5", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %>
<%= turbo_stream.append_all "#messages", @message %>
<%= turbo_stream.append_all "#messages", partial: "messages/message", locals: { message: Message.new(id: 5, content: "OLLA!") } %>
Expand Down
6 changes: 5 additions & 1 deletion test/streams/streams_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 method="morph" action="update" 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 All @@ -37,7 +39,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest

patch message_path(id: 1), as: :turbo_stream

assert_turbo_stream action: :replace, count: 4
assert_turbo_stream action: :replace, count: 5
assert_turbo_stream action: :replace, targets: "#message_4" do
assert_select 'template', 'Something fourth'
end
Expand All @@ -46,6 +48,8 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest
<turbo-stream action="replace" targets="#message_1"><template>#{render(message_1)}</template></turbo-stream>
<turbo-stream action="replace" targets="#message_1"><template>Something else</template></turbo-stream>
<turbo-stream action="replace" targets="#message_4"><template>Something fourth</template></turbo-stream>
<turbo-stream method="morph" action="replace" targets="#message_5"><template>Something fifth</template></turbo-stream>
<turbo-stream method="morph" action="update" targets="#message_5"><template>Something fifth</template></turbo-stream>
<turbo-stream action="replace" targets="#message_5"><template>#{render(message_5)}</template></turbo-stream>
<turbo-stream action="append" targets="#messages"><template>#{render(message_1)}</template></turbo-stream>
<turbo-stream action="append" targets="#messages"><template>#{render(message_5)}</template></turbo-stream>
Expand Down

0 comments on commit dfabcf7

Please sign in to comment.