Skip to content

Commit

Permalink
Allow passing a :template option to Broadcastable methods (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalejandroaguilar authored Feb 3, 2023
1 parent e4b1e97 commit 6c8126a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app/models/concerns/turbo/broadcastable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# (which is derived by default from the plural model name of the model, but can be overwritten).
#
# You can also choose to render html instead of a partial inside of a broadcast
# you do this by passing the html: option to any broadcast method that accepts the **rendering argument
# you do this by passing the `html:` option to any broadcast method that accepts the **rendering argument. Example:
#
# class Message < ApplicationRecord
# belongs_to :user
Expand All @@ -40,6 +40,20 @@
# end
# end
#
# If you want to render a template instead of a partial, e.g. ('messages/index' or 'messages/show'), you can use the `template:` option.
# Again, only to any broadcast method that accepts the `**rendering` argument. Example:
#
# class Message < ApplicationRecord
# belongs_to :user
#
# after_create_commit :update_message
#
# private
# def update_message
# broadcast_replace_to(user, :message, target: "message", template: "messages/show", locals: { message: self })
# end
# end
#
# There are four basic actions you can broadcast: <tt>remove</tt>, <tt>replace</tt>, <tt>append</tt>, and
# <tt>prepend</tt>. As a rule, you should use the <tt>_later</tt> versions of everything except for remove when broadcasting
# within a real-time path, like a controller or model, since all those updates require a rendering step, which can slow down
Expand Down Expand Up @@ -335,8 +349,13 @@ def broadcast_rendering_with_defaults(options)
# Add the current instance into the locals with the element name (which is the un-namespaced name)
# as the key. This parallels how the ActionView::ObjectRenderer would create a local variable.
o[:locals] = (o[:locals] || {}).reverse_merge!(model_name.element.to_sym => self)
# if the html option is passed in it will skip setting a partial from #to_partial_path
unless o.include?(:html)

if o[:html] || o[:partial]
return o
elsif o[:template]
o[:layout] = false
else
# if none of these options are passed in, it will set a partial from #to_partial_path
o[:partial] ||= to_partial_path
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/streams/broadcastable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Turbo::BroadcastableTest < ActionCable::Channel::TestCase
end
end

test "broadcasting update to stream now with template option" do
assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: render("messages/index", layout: false)) do
@message.broadcast_update_to "stream", template: "messages/index"
end
end

test "broadcasting update now" do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("update", target: "message_1", template: render(@message)) do
@message.broadcast_update
Expand Down

0 comments on commit 6c8126a

Please sign in to comment.