Created
November 4, 2024 20:08
-
-
Save scottwater/e0e3ba71e50fdab8cd3ba74cdb8deea6 to your computer and use it in GitHub Desktop.
Render Components from TurboStreams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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