diff --git a/app/models/concerns/turbo/broadcastable.rb b/app/models/concerns/turbo/broadcastable.rb index 41014a6f..ce2cc172 100644 --- a/app/models/concerns/turbo/broadcastable.rb +++ b/app/models/concerns/turbo/broadcastable.rb @@ -218,6 +218,12 @@ def broadcasts_refreshes(stream = model_name.plural) after_destroy_commit -> { broadcast_refresh } end + # All instances will use the return of this method for the key in the locals. Overwrite if you want something else than model_name.element.to_sym. + # This default parallels how the ActionView::ObjectRenderer would create a local variable. + def broadcast_locals_instance_key_default + model_name.element.to_sym + end + # All default targets will use the return of this method. Overwrite if you want something else than model_name.plural. def broadcast_target_default model_name.plural @@ -504,6 +510,10 @@ def broadcast_render_later_to(*streamables, **rendering) end private + def broadcast_locals_instance_key_default + self.class.broadcast_locals_instance_key_default + end + def broadcast_target_default self.class.broadcast_target_default end @@ -518,7 +528,7 @@ def broadcast_rendering_with_defaults(options) options.tap do |o| # 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).compact + o[:locals] = (o[:locals] || {}).reverse_merge(broadcast_locals_instance_key_default => self).compact if o[:html] || o[:partial] return o