From 0c9c9a1ef4740427f7be956d5260f61eead37b20 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Thu, 23 Nov 2023 16:51:58 -0300 Subject: [PATCH 1/3] Fix sending created broadcasts refreshes to channels where no one listens --- app/models/concerns/turbo/broadcastable.rb | 8 +++-- test/dummy/app/models/board.rb | 5 +++ .../migrate/20231123180958_create_boards.rb | 9 +++++ test/dummy/db/schema.rb | 8 ++++- test/streams/broadcastable_test.rb | 36 +++++++++++++++++++ 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 test/dummy/app/models/board.rb create mode 100644 test/dummy/db/migrate/20231123180958_create_boards.rb diff --git a/app/models/concerns/turbo/broadcastable.rb b/app/models/concerns/turbo/broadcastable.rb index 52f3dbc9..54906b3c 100644 --- a/app/models/concerns/turbo/broadcastable.rb +++ b/app/models/concerns/turbo/broadcastable.rb @@ -142,9 +142,11 @@ def broadcasts_refreshes_to(stream) end # Same as #broadcasts_refreshes_to, 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 stream. + 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 model_name.plural. diff --git a/test/dummy/app/models/board.rb b/test/dummy/app/models/board.rb new file mode 100644 index 00000000..397e23fc --- /dev/null +++ b/test/dummy/app/models/board.rb @@ -0,0 +1,5 @@ +class Board < ApplicationRecord + has_many :tasks + + broadcasts_refreshes +end diff --git a/test/dummy/db/migrate/20231123180958_create_boards.rb b/test/dummy/db/migrate/20231123180958_create_boards.rb new file mode 100644 index 00000000..81fad905 --- /dev/null +++ b/test/dummy/db/migrate/20231123180958_create_boards.rb @@ -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 diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index d5b87495..e152fd5b 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # 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 @@ -18,6 +18,12 @@ 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 diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index 52c736ab..04a6e6c1 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -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 From d0e837a6a2362ea5c2c93ee4aa8422cf8639f1b3 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Thu, 23 Nov 2023 16:59:28 -0300 Subject: [PATCH 2/3] Fix indent --- test/dummy/app/models/board.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/dummy/app/models/board.rb b/test/dummy/app/models/board.rb index 397e23fc..0af865d2 100644 --- a/test/dummy/app/models/board.rb +++ b/test/dummy/app/models/board.rb @@ -1,5 +1,5 @@ class Board < ApplicationRecord - has_many :tasks + has_many :tasks - broadcasts_refreshes + broadcasts_refreshes end From 9fb57c46dbf8fb7bc97935029145083f0f7fdb70 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Thu, 23 Nov 2023 17:45:48 -0300 Subject: [PATCH 3/3] Remove unused has_many relation --- test/dummy/app/models/board.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/dummy/app/models/board.rb b/test/dummy/app/models/board.rb index 0af865d2..9e7ba91b 100644 --- a/test/dummy/app/models/board.rb +++ b/test/dummy/app/models/board.rb @@ -1,5 +1,3 @@ class Board < ApplicationRecord - has_many :tasks - broadcasts_refreshes end