Skip to content
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

Fix sending created broadcasts refreshes to channels where no one listens #521

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/models/concerns/turbo/broadcastable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ def broadcasts_refreshes_to(stream)
end

# Same as <tt>#broadcasts_refreshes_to</tt>, but the designated stream for page refreshes is automatically set to
# the current model.
def broadcasts_refreshes
after_commit -> { broadcast_refresh_later }
# the current model, for creates - to the model plural name, which can be overriden by passing <tt>stream</tt>.
def broadcasts_refreshes(stream = model_name.plural)
after_create_commit -> { broadcast_refresh_later_to(stream) }
after_update_commit -> { broadcast_refresh_later }
after_destroy_commit -> { broadcast_refresh }
end

# All default targets will use the return of this method. Overwrite if you want something else than <tt>model_name.plural</tt>.
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/models/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Board < ApplicationRecord
broadcasts_refreshes
end
9 changes: 9 additions & 0 deletions test/dummy/db/migrate/20231123180958_create_boards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateBoards < ActiveRecord::Migration[6.1]
def change
create_table :boards do |t|
t.string :name

t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_09_23_150403) do
ActiveRecord::Schema.define(version: 2023_11_23_181306) do

create_table "articles", force: :cascade do |t|
t.text "body", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "boards", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "comments", force: :cascade do |t|
t.text "body", null: false
t.integer "article_id", null: false
Expand Down
36 changes: 36 additions & 0 deletions test/streams/broadcastable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,42 @@ class Turbo::BroadcastableCommentTest < ActionCable::Channel::TestCase
end
end

class Turbo::BroadcastableBoardTest < ActionCable::Channel::TestCase
include ActiveJob::TestHelper, Turbo::Streams::ActionHelper

test "creating a board broadcasts refreshes to a channel using models plural name when creating" do
assert_broadcast_on "boards", turbo_stream_action_tag("refresh") do
perform_enqueued_jobs do
board = Board.create!(name: "Board")
Turbo::StreamsChannel.refresh_debouncer_for(["boards"]).wait
end
end
end

test "updating a board broadcasts to the models channel" do
board = Board.suppressing_turbo_broadcasts do
Board.create!(name: "Hey")
end

assert_broadcast_on board.to_gid_param, turbo_stream_action_tag("refresh") do
perform_enqueued_jobs do
board.update!(name: "Ho")
Turbo::StreamsChannel.refresh_debouncer_for(board).wait
end
end
end

test "destroying a board broadcasts refreshes to the model channel" do
board = Board.suppressing_turbo_broadcasts do
Board.create!(name: "Hey")
end

assert_broadcast_on board.to_gid_param, turbo_stream_action_tag("refresh") do
board.destroy!
end
end
end

class Turbo::SuppressingBroadcastsTest < ActionCable::Channel::TestCase
include ActiveJob::TestHelper, Turbo::Streams::ActionHelper

Expand Down