Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Turbo Broadcasts
Links:
https://www.rubydoc.info/gems/turbo-rails/Turbo/Broadcastable
https://www.hotrails.dev/turbo-rails/turbo-streams
Task 1: Adding broadcasts to columns
1. Update
app/views/boards/show.html.erb
Goal: Connect the user to a WebSocket channel to receive real-time updates.
Action: Add a turbo stream tag at the top of the file.
New line:
Explanation:
This line of code sets up a Turbo Stream subscription to a WebSocket channel identified by the name created by
dom_id(@board)
. Whenever something is broadcasted to this channel, the updates will be automatically reflected in the user's view.2. Update
app/models/board_column.rb
Goal: Enable the BoardColumn model to broadcast changes to the respective WebSocket channel.
Actions:
Updated file:
Explanation:
The
broadcasts_to
method sets up the broadcasting for the BoardColumn model. It specifies that changes to BoardColumn records will be broadcasted to a channel named after the associated board_id ({ "board_#{board_column.board_id}" }
). The target specifies where in the DOM the updates will be inserted (it will look for html id), andinserts_by: :append
means new columns will be appended to the list.Task 2: Triggering columns broadcasts on card changes
1. Update
app/models/card.rb
Goal: Ensure that updates to cards trigger broadcasts for the associated columns, keeping the UI in sync.
Action: Add a callback to touch and update associated columns when cards are modified.
Updated file:
Explanation:
The
after_commit
callback is used to trigger thetouch_affected_board_columns
method after any changes to aCard
record are committed to the database. This method checks if theboard_column_id
has changed, and if so, it touches the old and new columns to update their timestamps. This, in turn, triggers the broadcast, ensuring that any changes to cards are reflected in the columns in real-time.