File tree Expand file tree Collapse file tree 3 files changed +59
-7
lines changed
Expand file tree Collapse file tree 3 files changed +59
-7
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,16 @@ module Card::Broadcastable
33
44 included do
55 broadcasts_refreshes
6+
7+ before_update :remember_if_preview_changed
68 end
9+
10+ private
11+ def remember_if_preview_changed
12+ @preview_changed ||= title_changed? || column_id_changed? || board_id_changed?
13+ end
14+
15+ def preview_changed?
16+ @preview_changed
17+ end
718end
Original file line number Diff line number Diff line change @@ -4,13 +4,7 @@ module Card::Pinnable
44 included do
55 has_many :pins , dependent : :destroy
66
7- after_update_commit :broadcast_pin_updates
8- end
9-
10- def broadcast_pin_updates
11- pins . each do |pin |
12- pin . broadcast_replace_to [ pin . user , :pins_tray ] , partial : "my/pins/pin"
13- end
7+ after_update_commit :broadcast_pin_updates , if : :preview_changed?
148 end
159
1610 def pinned_by? ( user )
@@ -28,4 +22,11 @@ def pin_by(user)
2822 def unpin_by ( user )
2923 pins . find_by ( user : user ) . tap { it . destroy }
3024 end
25+
26+ private
27+ def broadcast_pin_updates
28+ pins . find_each do |pin |
29+ pin . broadcast_replace_later_to [ pin . user , :pins_tray ] , partial : "my/pins/pin"
30+ end
31+ end
3132end
Original file line number Diff line number Diff line change 1+ require "test_helper"
2+
3+ class Card ::PinnableTest < ActiveSupport ::TestCase
4+ setup do
5+ Current . session = sessions ( :david )
6+ end
7+
8+ test "broadcasts pin update when title changes" do
9+ assert_broadcasted_pin_update do
10+ cards ( :logo ) . update! ( title : "New title" )
11+ end
12+ end
13+
14+ test "broadcasts pin update when column changes" do
15+ assert_broadcasted_pin_update do
16+ cards ( :logo ) . update! ( column : columns ( :writebook_in_progress ) )
17+ end
18+ end
19+
20+ test "broadcasts pin update when board changes" do
21+ assert_broadcasted_pin_update do
22+ cards ( :logo ) . update! ( board : boards ( :private ) , column : nil )
23+ end
24+ end
25+
26+ test "does not broadcast pin update when other properties change" do
27+ perform_enqueued_jobs do
28+ assert_turbo_stream_broadcasts ( [ pins ( :logo_kevin ) . user , :pins_tray ] , count : 0 ) do
29+ cards ( :logo ) . update! ( last_active_at : Time . current )
30+ end
31+ end
32+ end
33+
34+ private
35+ def assert_broadcasted_pin_update ( &block )
36+ perform_enqueued_jobs do
37+ assert_turbo_stream_broadcasts ( [ pins ( :logo_kevin ) . user , :pins_tray ] , &block )
38+ end
39+ end
40+ end
You can’t perform that action at this time.
0 commit comments