Skip to content

Commit

Permalink
Add method to TagBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKovynev committed Aug 17, 2024
1 parent 01ff576 commit 836842c
Show file tree
Hide file tree
Showing 4 changed files with 21 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,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, method = nil, **rendering, &block)
action :replace, target, content, 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 +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, method = nil, **rendering, &block)
action_all :replace, targets, content, 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 +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, method = nil, **rendering, &block)
action :update, target, content, 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 +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, method = nil, **rendering, &block)
action_all :update, targets, content, 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,20 +229,24 @@ 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, **attributes_for_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, **attributes_for_method(method)
end

private
def attributes_for_method(method)
{ method: method }.compact
end

def render_template(target, content = nil, allow_inferred_rendering: true, **rendering, &block)
case
when content.respond_to?(:render_in)
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", :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/dummy/app/views/messages/update.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<%= 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", :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
4 changes: 3 additions & 1 deletion 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 All @@ -37,7 +38,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 +47,7 @@ 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 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 836842c

Please sign in to comment.