Skip to content

Accept symbol, proc, and nil arguments for broadcasts_refreshes #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mrrooijen
Copy link

Currently, broadcasts_refreshes only accepts values in the form of a string or nil, where explicitly passing nil bypasses broadcasting but still triggers the after_create_commit callback.

This change allows you to pass a symbol (method name to call) or a proc to evaluate to broadcasts_refreshes to produce a stream name dynamically at the instance level after_create_commit.

This allows us to, for example, specify a parent (Board) to broadcast refreshes to on create, while broadcasting to itself (Column) on update and destroy:

class Column < ApplicationRecord
  belongs_to :board
  broadcasts_refreshes :board
end

Listen for new Column creations for @board:

<%= turbo_stream_from @board %> 

Listen for updates and deletion of existing @column:

<%= turbo_stream_from @column %> 

This changes also disables the entire after_create_commit hook if nil is passed in explicitly, rather than allowing it to run without broadcasting anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant