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 optional method to TagBuilder for morph #665

Merged
merged 1 commit into from
Sep 16, 2024
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
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)
AlexKovynev marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -229,17 +233,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, 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>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also cover the action="update" case in exactly the same way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update not covered at all as you see but anyway added with morph :)

<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
Loading