Skip to content

Commit

Permalink
add morph stream action tag builders
Browse files Browse the repository at this point in the history
  • Loading branch information
omarluq committed Mar 13, 2024
1 parent 36cba12 commit da14d4e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/models/concerns/turbo/broadcastable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,23 +495,23 @@ def broadcast_render_later_to(*streamables, **rendering)
# sends <turbo-stream action="morph" target="clearance_5"><template><div id="clearance_5">My Clearance</div></template></turbo-stream>
# to the stream named "identity:2:clearances"
# clearance.broadcast_morph_to examiner.identity, :clearances
def broadcast_morph_to(*streamables, target: broadcast_target_default, **rendering)
Turbo::StreamsChannel.broadcast_morph_to(*streamables, target: target, **broadcast_rendering_with_defaults(rendering)) unless suppressed_turbo_broadcasts?
def broadcast_morph_to(*streamables, **rendering)
Turbo::StreamsChannel.broadcast_morph_to(*streamables, target: self, **broadcast_rendering_with_defaults(rendering)) unless suppressed_turbo_broadcasts?
end

# Same as <tt>broadcast_morph_to</tt> but the designated stream is automatically set to the current model.
def broadcast_morph(target: broadcast_target_default, **rendering)
broadcast_morph_to(self, target: target, **rendering)
def broadcast_morph(**rendering)
broadcast_morph_to(self, target: self, **rendering)
end

# Same as <tt>broadcast_morph_to</tt> but run asynchronously via a <tt>Turbo::Streams::BroadcastJob</tt>.
def broadcast_morph_later_to(*streamables, target: broadcast_target_default, **rendering)
Turbo::StreamsChannel.broadcast_morph_later_to(*streamables,target: target, **broadcast_rendering_with_defaults(rendering)) unless suppressed_turbo_broadcasts?
def broadcast_morph_later_to(*streamables, **rendering)
Turbo::StreamsChannel.broadcast_morph_later_to(*streamables, target: self, **broadcast_rendering_with_defaults(rendering)) unless suppressed_turbo_broadcasts?
end

# Same as <tt>broadcast_morph_later_to</tt> but the designated stream is automatically set to the current model.
def broadcast_morph_later(target: broadcast_target_default, **rendering)
broadcast_morph_later_to(self, target: target, **rendering)
broadcast_morph_later_to self, **rendering
end

private
Expand Down
26 changes: 26 additions & 0 deletions app/models/turbo/streams/tag_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,32 @@ def prepend_all(targets, content = nil, **rendering, &block)
action_all :prepend, targets, content, **rendering, &block
end

# Morph the <tt>target</tt> in the dom with either the <tt>content</tt> passed in or a rendering result determined
# by the <tt>rendering</tt> keyword arguments, the content in the block, or the rendering of the target as a record. Examples:
#
# <%= turbo_stream.morph "clearance_5", "<div id='clearance_5'>Morph the dom target identified by clearance_5</div>" %>
# <%= turbo_stream.morph clearance %>
# <%= turbo_stream.morph clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
# <%= turbo_stream.morph "clearance_5" do %>
# <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
end

# Morph the <tt>targets</tt> in the dom with either the <tt>content</tt> passed in or a rendering result determined
# by the <tt>rendering</tt> keyword arguments, the content in the block, or the rendering of the targets as a record. Examples:
#
# <%= turbo_stream.morph_all ".clearance_item", "<div class='clearance_item'>Morph the dom target identified by the class clearance_item</div>" %>
# <%= turbo_stream.morph_all clearance %>
# <%= turbo_stream.morph_all clearance, partial: "clearances/clearance", locals: { title: "Hello" } %>
# <%= turbo_stream.morph_all ".clearance_item" do %>
# <div class='clearance_item'>Morph the dom target identified by the class clearance_item</div>
# <% end %>
def morph_all(targets, content = nil, **rendering, &block)
action_all :morph, targets, content, **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)
template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block)
Expand Down

0 comments on commit da14d4e

Please sign in to comment.