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..9e7ba91b --- /dev/null +++ b/test/dummy/app/models/board.rb @@ -0,0 +1,3 @@ +class Board < ApplicationRecord + 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