Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created November 4, 2024 20:08
Show Gist options
  • Save scottwater/e0e3ba71e50fdab8cd3ba74cdb8deea6 to your computer and use it in GitHub Desktop.
Save scottwater/e0e3ba71e50fdab8cd3ba74cdb8deea6 to your computer and use it in GitHub Desktop.
Render Components from TurboStreams
module TurboStreamsBroadcastExtensions
def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering)
render_component_to_html(rendering)
super
end
def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering)
render_component_to_html(rendering)
super
end
private
def render_component_to_html(rendering)
if rendering&.dig(:component)&.respond_to?(:render_in) && rendering&.dig(:html).blank?
rendered_component = ApplicationController.render(rendering.delete(:component), layout: false)
rendering[:html] = rendered_component
end
end
end
Turbo::Streams::Broadcasts.prepend TurboStreamsBroadcastExtensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment