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

WIP: fix morph #651

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 8 additions & 9 deletions app/helpers/turbo/streams/action_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ module Turbo::Streams::ActionHelper
# message = Message.find(1)
# turbo_stream_action_tag "remove", target: [message, :special]
# # => <turbo-stream action="remove" target="special_message_1"></turbo-stream>
def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes)
template = action.to_sym.in?(%i[ remove refresh ]) ? "" : tag.template(template.to_s.html_safe)
def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, method: nil)
template = action.to_sym.in?([:remove, :refresh]) ? "" : tag.template(template.to_s.html_safe)

if target = convert_to_turbo_stream_dom_id(target)
tag.turbo_stream(template, **attributes, action: action, target: target)
elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
tag.turbo_stream(template, **attributes, action: action, targets: targets)
else
tag.turbo_stream(template, **attributes, action: action)
end
attributes = { action: action }
attributes[:target] = convert_to_turbo_stream_dom_id(target) if target
attributes[:targets] = convert_to_turbo_stream_dom_id(targets) if targets
attributes[:method] = method if method

tag.turbo_stream(**attributes) { template }
end

# Creates a `turbo-stream` tag with an `action="refresh"` attribute. Example:
Expand Down
9 changes: 6 additions & 3 deletions app/models/turbo/streams/tag_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def prepend_all(targets, content = nil, **rendering, &block)
# <div id='clearance_5'>Morph the dom target identified by clearance_5</div>
# <% end %>
def morph(target, content = nil, **rendering, &block)
action :morph, target, content, **rendering, &block
action :update, target, content, method: "morph", **rendering, &block
end

# Morph the <tt>targets</tt> in the dom with either the <tt>content</tt> passed in or a rendering result determined
Expand All @@ -255,10 +255,13 @@ def morph_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, allow_inferred_rendering: true, method: nil, **rendering, &block)
template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block)

turbo_stream_action_tag name, target: target, template: template
attributes = { target: target, template: template }
attributes[:method] = method if method

turbo_stream_action_tag name, **attributes
end

# Send an action of the type <tt>name</tt> to <tt>targets</tt>. Options described in the concrete methods.
Expand Down